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-compose-leveler

v0.2.1

Published

Lets GraphQL servers allow client queries to mutate the shape of response objects. A graphql-leveler adaptation for use with graphql-compose.

Downloads

10

Readme

graphql-compose-leveler

Lets GraphQL servers allow client queries to mutate the shape of response objects.

A graphql-leveler inspired util for use with graphql-compose.

It allows GraphQL servers built with graphql-compose to let clients modify the output format by flattenning deeper structures or, to the contrary, adding depth where GraphQL objects would normally return flat fields.

Table of Contents

Table of Contents generated with DocToc

Background

A fellow developer once asked for a feature that graphql-leveler provides, but our API was built using the awesome library called graphql-compose. It required replacing all GraphQLObjectTypes in the source code of the app with LevelerObjectType.

But since we used graphql-compose, the TypeComposer interface and its GraphQL-schema parser, we couldn't always replace the base type easily (in schema syntax).

On the other hand, graphql-compose allowed us to modify the types anytime before the schema is finally generated and passed to the GraphQL server. So after discovering that graphql-leveler's code is very simple, I've reimplemented its functionality using TypeComposer magic, and added a _pluck on top of its _get.

Install

npm install --save graphql-compose-leveler
const levelerize = require("graphql-compose-leveler");

// You usually start with creating a ComposeStorage
const GQC = new ComposeStorage();
// ... building API ...
// now add leveler's magic to all queries in rootQuery 
levelerize(GQC.rootQuery());
// ... and finally build schema:
const schema = GQC.buildSchema();

Usage

Now having a response that would look like this:

{
  "a": {
    "very": {
      "deeply": {
        "nested": {
          "single": "value",
          "list": [
            {
              "item": "of"
            }, {
              "item": "values"
            }
          ]
        }
      }
    }
  }
}

You can query like you used to:

query {
  a {
    very {
      deeply {
        nested {
          single
          list {
            item
          }
        }
      }
    }
  }
}

but also:

query {
  a { thing: _get(path: "very.deeply.nested.single") }
}

which returns

{
  "a": {
    "thing": "value"
  }
}

and/or

query {
  a { 
    thing: _pluck(list: "very.deeply.nested.list", path: "item") 
  }
}

which returns:

{
  "a": {
    "thing": ["of", "values"]
  }
}

Development

This helper is written in TypeScript, and lib.js is updated automatically. The actual file you want to edit is lib.ts.

Use

npm run build

to update the JS and rebuild README table of contents.

TODO

  • Unit tests, of course. @chasingmaxwell, can I reuse yours? :)

Contribute

PRs accepted. Semver versioning used.

Maintainers

Thanks