Mocking your GraphQL API
GraphQL Mesh v0 documentation (superseded by v1): Learn how to use mock plugin in GraphQL Mesh to apply mocking for development usage. Replace resolvers of a specific field or type with mocks, and generate lists or individual values for queries.
The mock plugin allows you to apply mocking for development usage.
To get started with this plugin, install it:
How to use?
Add the following configuration to your Mesh config file:
The example above will replace the resolver of User.firstName with a mock that uses faker.js to
generate a random name.
Custom mock functions for fields
You can mock a specific field of a type;
Custom mock functions for types
You can mock types with custom mock functions like below;
When defined manually, properties can return values either directly or through a method. This is useful when defining static mocks because a mock property will be called as many times as there are items in an array. Here’s an example on how this could be achieved:
and in case you are using typescript:
Mocking the lists
Mesh generates two mocked items by default if the return type is a list. But this can be configured, as shown below:
Now query { users { id fullName } }{:graphql} query will return 3 of User item;
Stateful mocking
GraphQL Mesh supports GraphQL Tools’ Stateful Mocking feature. So you can have stateful mocking by
using the store provided in the context context.mockStore;
Initialize store
When having a schema that returns a list, in this case, a list of users:
Initially, populating the list of users can be done by utilizing the initializeStore property. The
store initialization will happen before the store is attached to the schema.
There is no need to provide a particular array mocking definition, like length. It will
automatically be taken based on the mock data.
Get from the store
You can implement the mock query field *ById declaratively like below:
Mutate data in the store
In the code:
CodeSandBox Example
You can check out our example that uses the JSON Schema handler with mock data.
Config API Reference
if(type:Boolean) - If this expression is truthy, mocking would be enabled You can use environment variables expression, for example:process.env.MOCKING_ENABLED != nullpreserveResolvers(type:Boolean) - Do not mock any other resolvers other than defined inmocks. For example, you can enable this if you don’t want to mock entire schema but partially.mocks(type:Array of Object) - Mock configurations:apply(type:String, required) - Resolver path Example: User.firstNameif(type:Boolean) - If this expression is truthy, mocking would be enabled You can use environment variables expression, for example:${MOCKING_ENABLED}faker(type:String) - Faker.js expression or function Read more (https://github.com/marak/Faker.js/#fakerfake) Example: faker:name.firstNamefaker:{{ name.firstName }} {{ name.lastName }}custom(type:Any) - Custom mocking It can be a module or json file. Both “moduleName#exportName” or only “moduleName” would work
You can also provide a function directly
length(type:Int) - Length of the mock list For the list types[ObjectType], how manyObjectTypeyou want to return? default: 2store(type:Object) - Get the data from the mock store:type(type:String)key(type:ID)fieldName(type:String)
updateStore(type:Array of Object) - Update the data on the mock store:type(type:String)key(type:ID)fieldName(type:String)value(type:String)
initializeStore(type:Any) - The path to the code runs before the store is attached to the schemamockGenerationBehavior(type:String (deterministic | random)) - Set to'deterministic'if the default random mock generation behavior causes flakiness.
Default: ‘random’