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-utils

v1.0.0

Published

A set of utilities for apps building with graphql-js

Downloads

373

Readme

GraphQL Utils

This is a set of utilities for schemas building with graphql-js.

Usage

npm install graphql-utils

Picking Fields Before Resolve

With this, you can get the field names of the result type based the query. With that, you can filter which fields you need to fetch from the DB.

You can also check fields on nested fields as well.

First of all, have a look at our schema.

Picking Fields

Basically, we need to pick fields mentioned in the following query.

Here's our query:

const query = `
  {
    recentPost {
      id,
      title
    }
  }
`;

We need to get them inside the resolve function of the recentPost field. This is how we to do it.

import {getFieldsFromInfo} from 'graphql-utils';

const RootQuery = new GraphQLObjectType({
  ...
    recentPost: {
      type: Post,
      resolve(root, args, info) {
        const fieldsMap = getFieldsFromInfo(info);
        console.log(Object.keys(fieldsMap));
      }
    }
  ...
});

Now you can see [ "id", "title" ] is printed on the screen.

Picking Nested Fields

Let's say, we've a query like this:

const query = `
  {
    recentPost {
      id,
      title,
      author {
        name
      }
    }
  }
`;

Then this is how we can get the nested fields of the field author.

import {
  getFieldsFromAst,
  getFieldsFromInfo
} from 'graphql-utils';

const RootQuery = new GraphQLObjectType({
  ...
    recentPost: {
      type: Post,
      resolve(root, args, info) {
        const fieldsMapOfPost = getFieldsFromInfo(info);
        const fieldsMapOfAuthor = getFieldsFromAst(info, fieldsMapOfPost['author']);
        console.log(Object.keys(fieldsMapOfAuthor));
      }
    }
  ...
});

Now you can see [ "name" ] is printed on the screen.

Maintaining Context

Sometimes it's very important to pass a context down to child nodes in graph. There is no built in functionality in graphql-js for that. But we can add a context with the return value of the resolve function. Then we can grab it from the parent value(in resolve functions) of child nodes.

For that we can use withContext and getContext functions of graphql-utils.

Read this blog post to see how this can be done in a real app.

Setting a context to an object

This is how we can set a context to a plain object.

import {
  withContext,
  getContext
} from 'graphql-utils';

const payload = {the: "payload"};
const context = {some: "data"};
const payloadWithContext = withContext(payload, context);

// payloadWithContext has a field called __context with the context we provide
// We can get it easily with the `getContext` method
console.log(getContext(payloadWithContext));

Setting a context to an array

When setting a context to an array, we need to set it for all the items in the array. This is how to do it.

import {
  withContext,
  getContext
} from 'graphql-utils'

const payload = [{id: 10}, {id: 20}];
const rootContext = {some: 'context'}
const payloadWithContext = withContext(payload, (item) => {
  // You can use an immutable data structure to prevent costly clones like this
  let newContext = JSON.parse(JSON.stringify(rootContext));
  newContext.itemId = item.id;
  return newContext;
});

// Now each of the items in the array has it's own context. 
// You can check it by printing the new payload
payloadWithContext.forEach((item) => {
  const context = getContext(item)
  console.log(context);
});