GiraphQL
Search…
Mocks
A simple plugin for adding resolver mocks to a graphQL schema.

Usage

Install

1
yarn add @giraphql/plugin-mocks
Copied!

Setup

1
import MocksPlugin from '@giraphql/plugin-mocks';
2
const builder = new SchemaBuilder({
3
plugins: [MocksPlugin],
4
});
Copied!

Adding mocks

You can mock any field by adding a mock in the options passed to builder.builSchema under mocks.{typeName}.{fieldName}.
1
builder.queryType({
2
fields: (t) => ({
3
someField: t.string({
4
resolve: () => {
5
throw new Error('Not implemented');
6
},
7
}),
8
}),
9
});
10
11
builder.toSchema({
12
mocks: {
13
Query: {
14
someField: (parent, args, context, info) => 'Mock result!',
15
},
16
},
17
});
Copied!
Mocks will replace the resolve functions any time a mocked field is executed. A schema can be build multiple times with different mocks.

Adding mocks for subscribe functions

To add a mock for a subscriber you can nest the mocks for subscribe and resolve in an object:
1
builder.subscriptionType({
2
fields: (t) => ({
3
someField: t.string({
4
resolve: () => {
5
throw new Error('Not implemented');
6
},
7
subscribe: () => {
8
throw new Error('Not implemented');
9
},
10
}),
11
}),
12
});
13
14
builder.toSchema({
15
mocks: {
16
Subscription: {
17
someField: {
18
resolve: (parent, args, context, info) => 'Mock result!',
19
subscribe: (parent, args, context, info) => {
20
/* return a mock async iterator */
21
},
22
},
23
},
24
},
25
});
Copied!
Last modified 6mo ago