A Pothos plugin for mocking resolvers
npm install @pothos/plugin-mocksA simple plugin for adding resolver mocks to a GraphQL schema.
``bash`
yarn add @pothos/plugin-mocks
`typescript`
import MocksPlugin from '@pothos/plugin-mocks';
const builder = new SchemaBuilder({
plugins: [MocksPlugin],
});
You can mock any field by adding a mock in the options passed to builder.toSchema undermocks.{typeName}.{fieldName}.
`typescript
builder.queryType({
fields: (t) => ({
someField: t.string({
resolve: () => {
throw new Error('Not implemented');
},
}),
}),
});
builder.toSchema({
mocks: {
Query: {
someField: (parent, args, context, info) => 'Mock result!',
},
},
});
`
Mocks will replace the resolve functions any time a mocked field is executed. A schema can be built
multiple times with different mocks.
To add a mock for a subscriber you can nest the mocks for subscribe and resolve in an object:
`typescript
builder.subscriptionType({
fields: (t) => ({
someField: t.string({
resolve: () => {
throw new Error('Not implemented');
},
subscribe: () => {
throw new Error('Not implemented');
},
}),
}),
});
builder.toSchema({
mocks: {
Subscription: {
someField: {
resolve: (parent, args, context, info) => 'Mock result!',
subscribe: (parent, args, context, info) => {
/ return a mock async iterator /
},
},
},
},
});
``