Giter Site home page Giter Site logo

graphql-utils-js's Introduction

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);
});

graphql-utils-js's People

Contributors

arunoda avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.