Magidoc

GraphQL Query Generator

standalone
plugin
node
browser

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.

Parameter
Description
targetName
Either the argument name or the nested field name.
defaultValue
The default value provided in the GraphQL Schema. You may decide to use it by providing a factory that returns the default value if it is non-null.
defaultFactory
The default factory that exists for this type. Can be useful if you want to perform custom actions and fallback to the default provider. Note that this factory is always the factory for a scalar value. Thus, if you create a factory for a [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.
randomFactory
The random factory that would be used to generate this object. This can be useful to fallback on a random object. This is only available when generating input values.
depth
The current depth in the field generation. This does not include the depth of the current parameter.
path
Path in the query to the current parameter.