Static
addAdds a fields array, GraphQLBuilderModel or a query to the general GraphQLBuilder library. The general library is separated from the GraphQLBuilder models library.
Use it to store field arrays, queries and models including their arguments, variables, directive, alias. Also possible is to store a request as string.
the name under which the object gets stored
the object to be stored
Static
addAdds a GraphQLBuilderModel to the GraphQLBuilder models library.
More precise, it creates and adds a copy of the provided model. Clears possible attached arguments, variables, directive and/or alias.
If you want to store a model including arguments, variables, directive and/or alias, retrieve the model with getModel() and store it in the general library for later use:
GraphQLBuilder.add('myExtendedModel', GraphQLBuilder.getModel('someModel(limit: $limit)', ['additionalField']));
const query = GraphQLBuilder.createQuery('someQuery', GraphQLBuilder.getModel(rootModel, {
extendedModel: GraphQLBuilder.get('myExtendedModel')
}));
the name of the model
the model to add
Static
createCreates a "gql"-formatted request for the hylo-apollo-client component "call" function. The "call" function optimizes the requests done by the ApolloClient but has limitations. Only queries (requests of type "query") which do not use variables nor fragments can be converted into call requests.
The id of your call should always be unique to that specific call since it is part of the optimization process.
the request name
one or more queries sent with your request
a GraphQL document to be used with a hylo-apollo-client call
Static
createCreates a GraphQLBuilderModel and adds it to the GraphQLBuilder models library.
You can add additional fields and models when you are building a query or extend the model with getModel(). Define simple building blocks with which you can create more complex queries and models later on.
the name of the model
Optional
fields: string[]an array of field names ("primitive" types, not nested models)
Optional
models: DictionaryOfGraphQLBuilderModel<GraphQLBuilderModel>a dictionary of nested models, the "keys" are the field names of the models
Static
createCreates a query, mutation or fragment.
If it is a query or mutation, define the header with the root query field name while also including arguments, variables and/or aliases:
If it is a fragment you are creating, the header should look somehow like this:
The header is placed as provided right before your root model definition.
Since you are providing a header, possibly defined arguments, directive and alias of the given root model are ignored.
the header of your query
Optional
model: GraphQLBuilderModelthe root model of your query (optional)
Static
createCreates a request, already "gql"-formatted and ready to be sent to the GraphQL server.
In the header you can set your request name, define variables and the type of request you are creating:
The header is placed as provided in your request.
the header of your request
one or more queries sent with your request
Optional
fragments: string | string[]one or more fragments used in your queries
a GraphQL document to be used with a ApolloClient
Static
getStatic
getReturns a GraphQLBuilderModel from the GraphQLBuilder models library.
You can add additional fields and/or models to the returned model. The returned model is not the original model stored in the GraphQLBuilder models library but a copy extended by the additionally provided fields and models.
With the name you can provide arguments, variables, a directive and/or an alias:
You can create a tunnel for chained models where the additionally provided fields and models are not added to the model of the function call but tunnel through the chain to the last child model of the tunnel. To create a tunnel set the tunnel flag for each member of the chain. A tunnel can not be used directly but needs to be saved first to the GraphQLBuilder models library with addModel(). A tunnel needs to be created in one call. You can not use saved tunnels to create another tunnel:
GraphQLBuilder.addModel('tunnel', GraphQLBuilder.getModel('firstLevelModel', {
child: GraphQLBuilder.getModel('secondLevelModel', {}, {}, true)
}, {}, true));
GraphQLBuilder.getModel('tunnel', ['field'], {model: GraphQLBuilder.getModel('someModel')});
The additional "field" and "model" are passed to the "child" secondLevelModel and not added to the firstLevelModel. You can deactivate the tunnel by setting the "tunnel" flag to false when calling/getting the tunnel.
the name of the model as defined with addModel() including arguments, variables, directive, alias
Optional
fields_or_models: string[] | DictionaryOfGraphQLBuilderModel<GraphQLBuilderModel>additional fields or models
Optional
models: DictionaryOfGraphQLBuilderModel<GraphQLBuilderModel>additional models when also additional fields where provided
Optional
tunnel: booleanpass provided additional fields and models down to the last child model in a row which is also set "tunnel"
Static
stringSame as createRequest() but returns a string. Needs to be formatted with "gql" in order to be used with the ApolloClient:
gql(string_request)
the header of your request
one or more queries sent with your request
Optional
fragments: string | string[]one or more fragments used in your queries
Generated using TypeDoc
import GraphQLBuilder from '@src/hylo-components/utils/graphqlbuilder/graphqlbuilder';
For the functions "createRequest" and "createCall" which return a gql formatted request, at least 'npm install graphql-tag --save-dev' is required which is already included in 'npm install graphql --save-dev'.
Class for stitching together a GraphQL query.
You can define the field structure of models, add nested models to a model and build queries out of the defined models. You don't need to actually write a query but build a query by structuring your defined models into a tree. At any point of your tree structure you can add additional fields and models while you're building your query.
You only need to define your models once and can reuse them whenever and wherever you need to build a query.
Build your models in one file, at one place and use them everywhere in your app to create your requests. "createRequest" and "createCall" returned requests are already wrapped in gql
...
and ready to use with an ApolloClient.All methods are static.
Example:
https://graphiql-online.com is a good tool for testing.