Mocking GraphQL responses
Mocks refers to data that is used when we don’t have the system implementation yet. Their purpose is to closely resemble the data that will be returned from a functioning backend.
But how do mocks work? It’s quite simple. Each type, except for custom scalars, ultimately resolves to basic scalars such as String
, ID
, Float
, Int
, and Boolean
. By knowing the final type and the resolver path, sometimes, even with detailed documentation, we can return very accurate mock values.
Let’s take a look at our simplified Book
type:
type Book { id: ID! title: String! author: String! } type Query{ books: [Book!]! }
If the fields are built-in scalars, we are able to provide mocks just after looking at the type. In the following JSON code, we provide a response for Query.books
:
[ { "id": "123abc", "title"...