A GraphQL helper library for constructing queries and accumulating fragments. This library is meant for statically determined queries, and encourages the use of variables and fragments over string concatenation.
This library is fairly short and written in a literate style, it is encouraged to take the time to read through the source code.
yarn add graphql-helper
Example:
const Contributor = `{ name slug}` const PostPage = `{ title body contributors { }}` const PostQuery = `{ post(id: $postId) { id }}` // Write your own app-specific dispatcher.// In this case, we just have a simple function, but this could live in// a react library, an elm effects module, an ember service... : Promise<Result> { return errors ? Promise : Promise) })} // Usage
Fragments
A fragment represents the data requirements of some component or aspect of an application.
Consider the graphql fragment:
fragment FullPost on Post { id slug title body contributors { ...Contributor } author { name ...Author }}
Suppose Author
and Contributor
are fragment definitions that we have already defined, then we can define FullPost
as follows:
const FullPost = `{ id slug title body contributors { } author { name }}`
Queries
A query represents some operation that fetches data.
Consider the GraphQL query:
query GetPost($id: ID!) { post(id: $id) { __typename id ...FullPost }}
Suppose FullPost
is already defined above. Then we can define this query as follows:
const GetPost = `{ post(id: $id) { __typename id }}`
And we can open up the resulting query object yields the following:
GetPost__GRAPHQL_QUERY__// => true GetPostname// => 'GetPost' GetPostdefinition// => `query GetPost($id: ID!) { { __typename id ...FullPost }}` GetPost.fragments// => { FullPost, Author, Contributor } // the following are equivalent:GetPost.definitionGetPost.toString()// => `query { { __typename id ...FullPost }} fragment FullPost on Post id slug id slug title body contributors ...Contributor author name ...Author fragment Contributor on User ...etc`
Mutations
A mutation represents some operation which changes data.
Consider a relay-compatible mutation createPost
:
type RootMutation { ... createPost(input: CreatePostInput): CreatePostPayload ...} input CreatePostInput { clientMutationId: String! title: String! body: String!} type CreatePostPayload { clientMutationId: String! post: Post!}
Then there exists a natural, "free mutation" that performs just that mutation:
mutation CreatePost($input: CreatePostInput) { payload: createPost(input: $Input) { # application decides which fields to fetch clientMutationId post { ...FullPost } }}
Noting that the clientMutationId
is a special field, we provide a condensed syntax for such a query as follows:
const CreatePost = GraphQL `{ clientMutationId post { }}`
And we can open up the resulting query object yields the following:
CreatePost__GRAPHQL_MUTATION__// => true CreatePostname// => 'CreatePost' CreatePostdefinition// => `mutation CreatePost($input: CreatePostInput!) { payload: { clientMutationId post ...FullPost }}` CreatePost.fragments// => { FullPost, Author, Contributor } // the following are equivalent:CreatePost.definitionCreatePost.toString()// => `mutation { payload: { clientMutationId post ...FullPost }} fragment FullPost on Post id slug id slug title body contributors ...Contributor author name ...Author fragment Contributor on User ...etc`
Document
The GraphQL.document([ QueryOne, QueryTwo, MutationOne, ... ])
method generates a complete document object which is useful for persisted queries.
This should never appear in your application logic, although build tools may use this to heavily optimize a production build by persisting a document at build time.
See source code for type declaration and implementation.
TODO
- Support for static analysis and pre-compilation of queries