graphql / graphql-js

A reference implementation of GraphQL for JavaScript
http://graphql.org/graphql-js/
MIT License
20.02k stars 2.02k forks source link

[Feature request] Provide parse, compile, merge and check functions #143

Closed gyzerok closed 8 years ago

gyzerok commented 9 years ago

It would be cool if you can provide functionality for parsing, merging and compiling GraphQL queries. So not only Relay can use awesome features of GraphQL.

  1. parse function should parse GraphQL query string into structure like info object which you can get in resolve function.
  2. compile function should be an opponent for parse
  3. merge function should merge two info objects to later perform optimised query to the server.

Consider this example:

query FirstQuery {
  ... on Root {
    todos: todos(count: 5) {
      _id,
      text
    }
  }
}

query SecondQuery {
  ... on Root {
    todos: todos(count: 6) {
      _id,
      createdAt
    }
  }
}

Using suggested functions it can be merged in:

query OptimizedQuery {
  ... on Root {
    todos: todos(count: 6) {
      _id,
      text,
      createdAt
    }
  }
}
gyzerok commented 9 years ago

Also it would be nice to have check function to be able to check query against schema to know if it is valid.

Nexi commented 9 years ago

+1

nemanja-stanarevic commented 9 years ago

They expose parser and AST visitor as public API and you can import parse and visit from graphql/language. Parse will give you Abstract Syntax Tree that you can walk through with visit. In-code documentation for visitor is great (see language/visitor.js), but post if you have any questions.

Finally, you can import validate and specifiedRules from graphql/validation and run query AST and schema definition through it to make sure everything is copacetic.

As far as query optimization goes, I defer to graphql team, but wouldn't think it make sense to have that in the reference implementation. It is perhaps a layer you have in between GraphQL endpoint and data store that caches and consolidates redundant queries, see Haxl for inspiration.