GraphQL Query Generator
The GraphQL Query Generator plugin does exactly as its name suggests: it automatically builds GraphQL queries, variables and responses from a root query type.
Generating GraphQL queries requires complex logic that is abstracted by this library. It supports parameter generation, duplicate names, union types and much more.
Example
#
Here is a sample query generated by the plugin.
Query
Variables
Response
Install
#
This plugin requires GraphQL.js as a peer dependency.
Usage
#
Here is a sample usage with TypeScript that would generate the above query .
Factories
#
Factories are the most important part of the configuration. It allows customization of how the plugin generates the random values used as query variables . The plugin offers many default factories . Each of these factories can be overwritten and custom ones can be added.
Factories can be used to provide custom generators for scalar types and input values .
Custom scalar types
#
Many GraphQL APIs implement custom scalars . Since the plugin cannot automatically determine what is backing these scalars, a custom factory is required for them.
For instance, given schema has a scalar named OddNumber
, representing any odd number.
The factory below could be provided.
Factory key
#
The key for the GraphQL factory object uses glob syntax to determine which factory to use. The factories are used in order of specificity, from the most specific to the least specific. Only the most specific factory will be used to generate the value.
Here are examples of factories going from the most specific to the least specific.
Type unwrapping
#
The query generator uses type unwrapping to try to find a factory in all the provided values. This means that you generally don't need to provide a factory for a [OddNumber!]!
, because a factory OddNumber
will be used for all possible wrappings of the type: OddNumber
, OddNumber!
, [OddNumber!]
and [OddNumber!]!
.
Factory context
#
Some GraphQL types, like String
, are used widely within most GraphQL APIs. You may desire to return a different value for these types based on the context where they are used. This can be achieved using the context
parameter provided in the factory function.
The parameters found in the context object and their description are described below.
[String!]!
, then the default factory will return a String, not an array of strings. You will be required to return an array yourself. This property is only available when overriding the default generators. Introduction