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

graphql-leveler

v1.1.1

Published

Allows GraphQL objects to be queried with mutated shapes.

Downloads

7

Readme

graphql-leveler

graphql-leveler makes it possible for GraphQL servers to allow client queries to control the shape of response objects. It does this by exposing a new type called LevelerObjectType which is a drop-in replacement for GraphQLObjectType with the addition of two convenient fields: _get and _root.

Installation

yarn add graphql-leveler or npm install --save graphql-leveler

Client Usage

_get

_get allows clients to query the object by path, much like lodash's get() method. In fact, it uses _.get() under the hood! Using _get in combination with an alias, a client can access deeply nested properties at the top level of an object with whatever property name the client desires.

Basic Queries

If the object's resolver on the server returns an object which looks like this:

{
  "some": {
    "deeply": {
      "nested": {
        "property": "I am nested!"
      }
    }
  }
}

— normally the client would have to query for property like this:

{
  some: { deeply: { nested: { property } } }
}

— and receive something that would look like this in return:

{
  "data": {
    "some": {
      "deeply": {
        "nested": {
          "property": "I am nested!"
        }
      }
    }
  }
}

— regardless of whether that is the shape desired by the client.

However, with _get, now the client can query for the same property like this:

{
  myProperty: _get(path: "some.deeply.nested.property")
}

— flattening the response to something that looks like this:

{
  "data": {
    "myProperty": "I am nested!"
  }
}

Nested LevelerObjectType's

You aren't limited to completely flat shapes either! Since LevelerObjectType is a drop-in replacement for GraphQLObjectType, you can make any GraphQLObjectType a LevelerObjectType, allowing clients extreme flexibility over the response shape. For example, let's say we've changed every object type in the above example to LevelerObjectType. Clients can now also get to the same property with the following query:

{
  iCanControl: some {
    theShape: _get(path: "deeply.nested.property")
  }
}

— which will result in the following response shape:

{
  "data": {
    "iCanControl": {
      "theShape": "I am nested!"
    }
  }
}

Optional arguments

In addition to the required path argument, the _get field provides two optional arguments:

defaultValue

Define the default value returned if there is no property found at the given path.

{
  sometimesMissing: _get(path: "some.occasionally.existing.property", defaultValue: "missing")
}
allowUndefined

The default behavior is to report an error if the property at the given path is missing and no default value is provided. This argument allows undefined values so that the result will be null in the response for that field.

{
  sometimesMissing: _get(path: "some.occasionally.existing.property", allowUndefined: true)
}

_root

_root allows clients access the the root of the closest LevelerObjectType in places where it would usually be out of scope. This can be used in combination with aliases to construct arbitrary objects in the response object. For example:

If the object's resolver on the server returns this:

{
  "attributes": {
    "episodeNumber": 42,
    "seasonNumber": 1
  }
}

— then a client can query it like this:

{
  episode: attributes {
    episodeNumber
    season: _root {
      seasonNumber
    }
  }
}

— producing a shape like this:

{
  "data": {
    "episode": {
      "episodeNumber": 42,
      "season": {
        "seasonNumber": 1
      }
    }
  }
}

In the above example, the seasonNumber field is nested inside episode.season which is an arbitrary object. Without _root that field would need to be queried and returned at the root of the episode object.

Server Usage

LevelerObjectType is a drop-in replacement for GraphQLObjectType. For every object you desire to be queryable with _get, simply replace GraphQLObjectType with LevelerObjectType.

const {
  GraphQLString,
  GraphQLInt,
  GraphQLObjectType,
} = require('graphql');
const {
  LevelerObjectType,
} = require('graphql-leveler');

// This would have been GraphQLObjectType before.
const PersonType = new LevelerObjectType({
  name: 'person',
  fields: () => ({
    attributes: {
      // This would have been GraphQLObjectType before.
      type: new LevelerObjectType({
        name: 'personAttributes',
        fields: () => ({
          name: { type: GraphQLString },
          height: { type: GraphQLInt },
          eye_color: { type: GraphQLString },
        }),
      }),
    },
  }),
});

What are the limitations?

Only scalar leaf values can be retrieved with _get.

As of right now, because of the complexity of allowing multiple types in the response for a single field, no complex values like objects or arrays can be returned by the _get field resolver.

Nested field resolvers are not invoked with _get.

graphql-leveler does not invoke field resolvers within the object currently being queried, and so it is limited to properties which exist in the raw object returned by the resolver.

So, for example, if your raw object looks like this:

{
  "id": "123",
  "type": "person",
  "attributes": {
    "name": "Luke Skywalker"
  },
  "relationships": {
    "father": {
      "data": {
        "id": "122",
        "type": "person"
      }
    }
  }
}

— where the father relationship data resolves to (spoiler alert):

{
  "id": "122",
  "type": "person",
  "attributes": {
    "name": "Darth Vader"
  }
}

You cannot write a query like this:

{
  Luke: person(id: "123") {
    father: _get(path: "relationships.father.data.attributes.name")
  }
}

— since the raw data for person "123" does not contain the attributes of its father relationship. In this case you would either need to adjust your person resolver to include the full entity in the relationship or adjust the client query to something like this:

{
  Luke: person(id: "123") {
    relationships {
      father: {
        name: _get(path: "attributes.name")
      }
    }
  }
}