The strongly-typed nature of a GraphQL API lends itself extremely well to mocking. This is an important part of a GraphQL-First development process, because it enables frontend developers to build out UI components and features without having to wait for a backend implementation.
Even with a backend that is already built, mocking allows you to test your UI without waiting on slow database requests or building out a component harness with a tool like React Storybook.
Default mock example
Let's take a look at how we can mock a GraphQL schema with just one line of code, using the default mocking logic you get out of the box with
To start, let's grab the schema definition string from the
makeExecutableSchema example in the "Generating a schema" article.
Note: If your schema has custom scalar types, you still need to define the
__parseLiteralfunctions, and pass them inside the second argument to
This mocking logic simply looks at your schema and makes sure to return a string where your schema has a string, a number for a number, etc. So you can already get the right shape of result. But if you want to use the mocks to do sophisticated testing, you will likely want to customize them to your particular data model.
This is where the
mocks option comes in, it's an object that describes your desired mocking logic. This is similar to the
makeExecutableSchema, but has a few extra features aimed at mocking.
It allows you to specify functions that are called for specific types in the schema, for example:
You can also use this to describe object types, and the fields can be functions too:
Mocking Custom Scalar Types
In order to Mock Custom Scalar Types, you need to declare them in your Schema. Let's look at an example for declaring DateTime Custom Scalar in our Schema:
This will make DateTime Custom Scalar available to be used in the Schema.
The next step is to define a function that returns a value (fixed or random) for the Custom Scalar. Look at the following example, in which we're mocking a fixed value for the DateTime Custom Scalar Type:
Similarly, if you want to mock a random value for the Custom Scalar, you can use a library. We're using casual, as in the example above:
The final step is to use the
mocks object and
schema to mock the server.
Now, when you make a Query which response contains the DateTime Scalar Type, the DateTime function will return a value for it.
Using MockList in resolvers
You can also use the MockList constructor to automate mocking a list:
In more complex schemas, MockList is helpful for randomizing the number of entries returned in lists.
For example, this schema:
By default, the
summary field will always return 2 entries. To change this, we can add a mock resolver with MockList as follows:
Now the mock data will contain between zero and 12 summary entries.
Accessing arguments in mock resolvers
Since the mock functions on fields are actually just GraphQL resolvers, you can use arguments and context in them as well:
You can read some background and flavor on this approach in our blog post, "Mocking your server with one line of code".
You will need resolvers to mock interfaces. By default
addMocksToSchema will overwrite resolver functions.
By setting the property
preserveResolvers on the options object to
true, the type resolvers will be preserved.
Mocking a schema using introspection
The GraphQL specification allows clients to introspect the schema with a special set of types and fields that every schema must include. The results of a standard introspection query can be used to generate an instance of GraphQLSchema which can be mocked as explained above.
This helps when you need to mock a schema defined in a language other than JS, for example Go, Ruby, or Python.
To convert an introspection query result to a
GraphQLSchema object, you can use the
buildClientSchema utility from the
Given an instance of GraphQLSchema and a mock object,
addMocksToSchema returns a new schema that can return mock data for any valid query that is sent to the server. If
mocks is not passed, the defaults will be used for each of the scalar types. If
preserveResolvers is set to
true, existing resolvers will not be overwritten to provide mock data. This can be used to mock some parts of the server and not others.
This is an object you can return from your mock resolvers which calls the
mockFunction once for each list item. The first argument can either be an exact length, or an inclusive range of possible lengths for the list, in case you want to see how your UI responds to varying lists of data.
mockServer is just a convenience wrapper on top of
addMocksToSchema. It adds your mock resolvers to your schema and returns a client that will correctly execute
your query with variables. Note: when executing queries from the returned server,
root will both equal