graphql
Complete API reference for the graphql namespace in Mock Config.
The graphql namespace provides methods for registering GraphQL operation handlers.
graphql.query
Intercepts GraphQL query operations matching the given identifier.
graphql.query<Options>(identifier, config, settings?)Prop
Type
graphql.mutation
Intercepts GraphQL mutation operations. Identical signature to graphql.query.
graphql.mutation<Options>(identifier, config, settings?)graphql.subscription
Intercepts GraphQL subscription operations over graphql-transport-ws WebSocket.
graphql.subscription(identifier, config, settings?)Prop
Type
GraphQLConfig
The config argument for graphql.query and graphql.mutation accepts one of the following forms.
Inline value
Pass a response object directly:
graphql.query('GetUsers', { data: { users: [] } });Matching A Response
Pass match in the operation settings argument when a response should only apply to specific request conditions:
graphql.query(
'GetUsers',
{ data: { users: [] } },
{
match: { variables: { role: 'admin' } }
}
);Prop
Type
Handler function
Pass a function to compute the response dynamically. The function receives resolver params.
graphql.query('GetUsers', ({ entities }) => ({
data: { users: [{ role: entities.variables?.role }] }
}));Matching A Handler
Use the same settings argument to match dynamic handlers:
graphql.query('GetUsers', ({ entities }) => ({ data: { users: [] } }), {
match: { variables: { role: 'admin' } }
});Prop
Type
Generator function
Pass a generator function when each request should receive the next generated value. When the generator finishes, Mock Config starts it again.
graphql.query('GetStatus', function* () {
yield { data: { status: 'pending' } };
yield { data: { status: 'processing' } };
return { data: { status: 'done' } };
});Polling helper
Cycle through a sequence of responses over time. The same polling model as REST. See Polling.
import { graphql } from 'mock-config-server';
graphql.query(
'GetStatus',
graphql.polling([
{ response: { data: { status: 'pending' } }, time: 2000 },
{ response: { data: { status: 'done' } } }
])
);Prop
Type
GraphQLSubscriptionConfig
The config argument for graphql.subscription accepts one of the following forms.
Inline value
graphql.subscription('OnUserCreated', { data: { userCreated: { id: 1 } } });Sends the value as a single next event and immediately closes the subscription with complete.
Use the settings argument for subscription matchers:
graphql.subscription(
'OnUserCreated',
{ data: { userCreated: { id: 1 } } },
{ match: { variables: { teamId: '1' } } }
);Handler function
The handler receives subscription params with next, complete, and setDelay:
graphql.subscription('OnUserCreated', async ({ next, complete, setDelay }) => {
next({ data: { userCreated: { id: 1 } } });
await setDelay(300);
complete();
});Prop
Type
Matching A Handler
graphql.subscription(
'OnUserCreated',
async ({ next, complete }) => {
next({ data: { userCreated: { id: 1 } } });
complete();
},
{ match: { variables: { teamId: '1' } } }
);GraphQLSettings
The optional third argument for graphql.query and graphql.mutation.
Prop
Type
GraphQLExecutionResult
The shape of every GraphQL response:
type GraphQLExecutionResult = {
data?: Record<string, unknown> | null;
errors?: Array<{ message: string; [key: string]: unknown }>;
};Generic Options
graphql.query and graphql.mutation accept an optional generic Options to type the request and response:
graphql.query<{
response: { data: { users: User[] } };
}>('GetUsers', ({ entities }) => {
return { data: { users: [] } };
});Prop
Type