Skip to content

Latest commit

 

History

History
106 lines (88 loc) · 1.81 KB

README.md

File metadata and controls

106 lines (88 loc) · 1.81 KB

GraphQL JS

A simple GraphQL parser written in JavaScript.

TODO

  • Write some tests
  • Provided better syntax/parsing errors

Usage

Install

$ npm install --save graphql

Parser API

The parser is generated by PEGjs, I'd recommend reading their docs on parser usage. But it's pretty simple:

var parser = require('graphl');

var query = `
  node(1) {
    id
    name
    birthdate {
      day,
      month,
      year
    },
    friends.first(1) {
      id
      name
    }
  }
`;

var ast = parser.parse(query);
// ast is a plain JS object

Example output

Going off the above query, the return AST would look like this:

  {
    "call": "node",
    "parameter": "1",
    "root": true,
    "properties": [
      {
        "name": "id"
      },
      {
        "name": "name"
      },
      {
        "name": "birthdate",
        "properties": {
          "properties": [
            {
              "name": "day"
            },
            {
              "name": "month"
            },
            {
              "name": "year"
            }
          ]
        }
      },
      {
        "name": "friends",
        "calls": [
          {
            "call": "first",
            "parameter": "1"
          }
        ],
        "properties": {
          "properties": [
            {
              "name": "id"
            },
            {
              "name": "name"
            }
          ]
        }
      }
    ]
  }

Demo

After cloning, install deps with npm install.

You can re-build the parser by running npm run build

Or see a demo of its output by running npm run demo, this runs the parser over ./examples/friends.gql and dumps the parsed object to the console.