npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gql-tag-objection-notation

v2.3.0

Published

Tool for converting gql-tag object to objectionjs object notation.

Downloads

5

Readme

gql-tag-objection-notation

Convert a graphql-tag object into objection relation notation.

While working on a family tree (graph) application found that querying data from backend depends on what the graphql query is requesting. This tool was developed to allow user to query relations in objection based on the graphql query.

Install

$ npm install gql-tag-objection-notation

Usage

The objection model and schema definition can be found at: https://github.com/Nodiril/family_graph_server

If the graphql query is defined as:

query Node {
  nodes {
    first_name
    parent_edge {
      id
    }
    partner_edges {
      id
      parent_node_a
      {
        first_name
      }
      children_edges
      {
        child_node
        {
          first_name
          last_name
        }
      }
    }
  }
}

In the gql resolver, you need access to the request query.

const gql = require('graphql-tag');
const getObjectNotation = require("gql-tag-objection-notation");

module.exports = {
  Query: {
    nodes: async (parent, { }, { ctx, models }) => {
      let obj = gql`${ctx.request.body.query}`;

      let selectobj = getObjectNotation(obj.definitions[0].selectionSet.selections[0])
      let nodes = await models.Node.query().modify(selectobj.$modify).where('id', 1)
        .withGraphFetched(getObjectNotation(obj.definitions[0].selectionSet.selections[0]))
      return nodes;
    }
  }
};
getObjectNotation.(obj.definitions[0].selectionSet.selections[0])

produces the object:

{
    $modify: (builder) => { builder.select(fields.map(a => builder._modelClass.tableName.concat('.', a))) },
    parent_edge: { $modify: (builder) => { builder.select(fields.map(a => builder._modelClass.tableName.concat('.', a))) } },
    partner_edges: {
        $modify: (builder) => { builder.select(fields.map(a => builder._modelClass.tableName.concat('.', a))) }
        parent_node_a: { $modify: (builder) => { builder.select(fields.map(a => builder._modelClass.tableName.concat('.', a))) } },
        children_edges: {
            $modify: (builder) => { builder.select(fields.map(a => builder._modelClass.tableName.concat('.', a))) }
            child_node: { $modify: (builder) => { builder.select(fields.map(a => builder._modelClass.tableName.concat('.', a))) } }
        }
    }
}

The modify functions are building the query to select only the fields selected by the graphql query. If there are no fields selected on a given object (not including the joins to other objects), such as in children edges in the example, it will select all fields in the table.

The resulting query returns the data (this data is based on the data in the github example app):

[
    {
        "first_name": "Robert",
        "parent_edge": null,
        "partner_edges": [
            {
                "id": 1,
                "parent_node_a": {
                    "id": 1,
                    "first_name": "Robert"
                },
                "children_edges": [
                    {
                        "id": 2,
                        "graph_id": 1,
                        "node_a": 1,
                        "node_b": 3,
                        "relationship": "pc",
                        "start": null,
                        "end": null,
                        "created_at": "2020-12-19T21:58:33.506Z",
                        "updated_at": "2020-12-19T21:58:33.506Z",
                        "child_node": {
                            "first_name": "Mary",
                            "last_name": "Lincoln"
                        }
                    }
                ]
            }
        ]
    }
]

** this does not yet support gql fragments