graphql-parser
graphql-parser copied to clipboard
Feature Request: toString()
A function to generate a GraphQL query string from an AST would be super-useful for me. Is there any chance of this landing as a feature in graphql-parser?
I no longer have time to contribute to this library but I'll be pleased to accept any PR!
@ooflorent Thanks! I had a think last night and there's a bunch of changes I want to make which would break all sorts of backwards compatibility. So I'm thinking it might make more sense to fork and create a new project to avoid confusion. That and the name overlap with https://github.com/Shopify/graphql-parser
I'm putting together a shortlist of features and I'd be thankful for any feedback you may have. What I'm looking at so far is:
Lazy Evaluated Fragments
TL;DR: change graphql
tagged template to accept functions in the style bellow as lazy evaluated fragments:
const userFragment = state => graphql`
fragment on User {
${state.includeUsername ? "username" : ""}
}
`
const state = {
includeUsername: true
}
const Query = graphql`
query {
currentUser {
${userFragment}
}
}
`
Query(state) // also changing this to returns a instance of a new `QueryWithState` class (pending a less awful name)
This will allow me to compile a query with fragments that can be changed based on the state passed to the query without needing to pass the state into the fragments as I'm composing them into the query. In my usecase the state comes from Redux and the queries are composed in a React higher order component which does not have access to the state. This allows me to decouple the two.
QueryWithState.toString()
Returns the final GraphQL query string after evaluating and composing all of the fragments.
QueryWithState.toAST()
Returns the AST. So basically this is equivalent to what the following line used to return:
(graphql`query {}`)(state)
QueryWithState.send({transport})
Make use of Lokka Transports (see: https://github.com/kadirahq/lokka#available-transports ) to send the GraphQL query to the server. Returns a promise.
Since this project is no longer maintained and graphql-js parser does the job pretty well, I may consider dropping the name or accept any breaking PR.
This library was created before the official implementation was released and does not parse the current grammar.