A simple GraphQL parser written in JavaScript.
- Write some tests
- Provided better syntax/parsing errors
$ npm install --save graphql
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
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"
}
]
}
}
]
}
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.