Skip to content

Parse "info" object (passed to resolver) into query tree and extract useful info from it

Notifications You must be signed in to change notification settings

alekbarszczewski/graphql-query-tree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-query-tree Build Status

Parse info object (passed to resolver) into a query tree and extract useful info from it.

Installation

$ npm install --save graphql-query-tree

Features

  • Parse info argument (passed to resolver) into GraphqlQueryTree
    • Supports Fragments and InlineFragments
  • Check if field at given path is present in query
  • Get arguments for field at given path
    • Supports variables
    • Supports default argument(s) values
  • Get child fields (keys) of field at given path

Usage

query {
  posts (limit: 20) {
    id
    title
    author {
      id
      name
    }
    tags (limit: 10, sort: "created_at") {
      id
      text
    }
  }
}
import GraphqlQueryTree from 'graphql-query-tree';

export default function (root, args, context, info) {
  const tree = new GraphqlQueryTree(info);

  // check if field is selected
  tree.isSelected('tags'); // true
  tree.isSelected('author.profilePicture'); // false

  // get field arguments
  tree.getArguments('tags'); // { limit: 10, sort: 'created_at' }
  tree.getArguments(); // { limit: 20 }
  tree.getArguments('some.invalid.path'); // null

  // get child fields
  tree.getChildFields('tags'); // ['id', 'text']
  tree.getChildFields('some.invalid.path'); // null

  tree.getParentType(); // 'Query'
  tree.getParentField(); // 'posts'
  tree.getReturnType(); // 'Post'
  tree.getType(); // 'Post'
  tree.getType('author'); // 'User'
  tree.getType('tags'); // 'Tag'
  tree.getType('some.invalid.path'); // null
}

About

Parse "info" object (passed to resolver) into query tree and extract useful info from it

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published