Skip to content
This repository has been archived by the owner. It is now read-only.
GraphQL parser written in JavaScript
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin
examples
.gitignore
README.md
graphql-parser.js
graphql.js
graphql.pegjs
package.json

README.md

GraphQL JS

A simple GraphQL parser written in JavaScript.

TODO

  • Write some tests
  • Provided better syntax/parsing errors

Usage

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('graphql');

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.

You can’t perform that action at this time.