graphql-parser
Experimental Facebook's GraphQL parser
This parser is inspired by Facebook's articles about GraphQL. It supports the latest shown syntax.
- Introducing Relay and GraphQL
- Building The Facebook News Feed With Relay
- GraphQL Introduction (current implementation)
Install
npm install --save graphql-parser
Usage
graphql-parser
exposes a tagged template function for parsing GraphQL queries. It outputs a function generating a JS object describing the query.
const IMAGE_WIDTH = 80const IMAGE_HEIGHT = 80 // Compile a fragmentconst PostFragment = graphql` { id, title, published_at }` // Compile a queryconst UserQuery = graphql` { user(id: <id>) { id, nickname, avatar(width: , height: ) { url(protocol: "https") }, posts(first: <count>) { count, edges { node as post { } } } } }` // Generate a GraphQL queryconst query =
In the above example output will be:
"user": "params": "id": 1337 "fields": "id": {} "nickname": {} "avatar": "params": "width": 80 "height": 80 "fields": "url": "params": "protocol": "https" "posts": "params": "first": 10 "fields": "count": {} "edges": "fields": "node": "alias": "post" "fields": "id": {} "title": {} "published_at": {}
AST manipulation
graphql-parser
also exposes lower level API for generating GraphQL AST and traversing it.
// Parsing// ------- const ast = // Traversal// --------- const ctx = slug: "/graphql-is-hot" const obj =