imranolas / graphql-paths-to-ast

Transforms a list of object get paths to a GraphQL query

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

graphql-paths-to-ast

A function to transform a list of paths to a GraphQL AST

$ yarn add graphql-paths-to-ast

Usage

const pathsToAst = require('graphql-paths-to-ast');

pathsToAst([
  'name',
  'address.city',
  'address.country'
]);

/*
{
  "selections": [
    {
      "name": {
        "value": "name",
        "kind": "Name"
      },
      "kind": "Field"
    },
    {
      "selectionSet": {
        "selections": [
          {
            "name": {
              "value": "city",
              "kind": "Name"
            },
            "kind": "Field"
          },
          {
            "name": {
              "value": "country",
              "kind": "Name"
            },
            "kind": "Field"
          }
        ],
        "kind": "SelectionSet"
      },
      "name": {
        "value": "address",
        "kind": "Name"
      },
      "kind": "Field"
    }
  ],
  "kind": "SelectionSet"
}
*/

const pathsToAst = require('graphql-paths-to-ast');
const {print} = require('graphql')

print(
  pathsToAst([ 'name', 'address.city', 'address.country' ])
);

/*
{
  name
  address {
    city
    country
  }
}
/*

About

Transforms a list of object get paths to a GraphQL query

License:MIT License


Languages

Language:JavaScript 100.0%