Socket
Socket
Sign inDemoInstall

graphql-parse-fields

Package Overview
Dependencies
2
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    graphql-parse-fields

Parse fields from AST (GraphQLResolveInfo) into a JSON tree


Version published
Weekly downloads
1.9K
decreased by-12.51%
Maintainers
1
Install size
27.2 kB
Created
Weekly downloads
 

Readme

Source

graphql-parse-fields Build Status js-standard-style

Parse fields from AST (GraphQLResolveInfo) into a JSON tree

Installation

npm i --save graphql-parse-fields

Usage

Example1: Parse GraphQL resolve info

  • @param {Object} info - graphql resolve info
  • @param {Boolean} [ignoreRoot] - default: true
  • @return {Object} fieldTree
var parseFields = require('graphql-parse-fields')

var GraphQLObjectType = //...
var UserType = //...

var queryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    user: {
      type: UserType,
      resolve: function (root, args, ctx, info) {
        var fields = parseFields(info)
        /*
        Fields parsed from query (at bottom of this example):
          note: since keepRoot was not passed, parseFields ignores the root key "user" for convenience
        {
          id: true,
          name: true,
          widgets: {
            edges {
              node {
                id: true
              }
            }
          }
        }
        */

       var fieldsWithRoot = parseFields(info, true) // keepRoot: true
       /*
        Fields parsed from query (at bottom of this example):

        {
          user {
            id: true,
            name: true,
            widgets: {
              edges {
                node {
                  id: true
                }
              }
            }
          }
        }
        */
      },
    }
  }
})

/*
Query:

query userQuery {
  user {
    id
    name
    widgets {
      edges {
        node {
          id
        }
      }
    }
  }
}
*/

Example2: Parse GraphQL ASTs

  • @param {Array} asts - ast array
  • @param {Object} [fragments] - optional fragment map
  • @param {Object} [fieldTree] - optional initial field tree
  • @return {Object} fieldTree
var ast = {
  "kind": "Field",
  "alias": null,
  "name": {
    "kind": "Name",
    "value": "user"
  },
  "selectionSet": {
    "kind": "SelectionSet",
    "selections": [
      {
        "kind": "Field",
        "name": {
          "kind": "Name",
          "value": "id"
        }
        selectionSet: null
        //...
      }
    ]
    //...
  }
  //...
}
parseAst(ast)
parseAst([ast])
/*
Both result in:
{
  user: {
    id: true
  }
}
*/

Changelog

CHANGELOG.md

License

MIT

Keywords

FAQs

Last updated on 22 Nov 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc