When the GraphQL API you depend on isn't ready, or you want answers you control in tests, Mockhop can stand in for it. You define how each query and mutation is answered, and your client calls a normal GraphQL URL.
How it works
Add a GraphQL endpoint to a project. Its path is
/graphqlunless you choose another one. Its URL looks like this:POST $URL/graphql/payments/graphql$URLis your organization's mock address andpaymentsis the project's base path.Add a resolver for each operation. A resolver has an operation type (query, mutation or subscription), an operation name and the JSON it returns as
data.How Mockhop finds the operation name, in this order:
- the request's
operationName - the name in the query (
query GetOrder) - the first field (
{ order { … } }→order)
A resolver named
*answers any operation of its type that has no resolver of its own.- the request's
Use variables in the response
Write {{variableName}} in the resolver's response, and Mockhop inserts the value from the request's variables.
Request:
query GetOrder($id: ID!) {
order(id: $id) {
id
status
}
}
Variables: { "id": "48213" }
Resolver (query, GetOrder):
{
"order": {
"id": "{{id}}",
"status": "PENDING"
}
}
Response:
{
"data": {
"order": {
"id": "48213",
"status": "PENDING"
}
}
}
Delays and errors
Each resolver can wait a set number of milliseconds before answering. With an error probability, a share of calls returns an errors response instead, either your own error JSON or a default message. The HTTP status stays 200, as GraphQL clients expect. More on failure cases: Simulate API errors.
What to keep in mind
- No query execution: Mockhop returns the resolver's JSON as you wrote it. It doesn't run the query against a schema and doesn't remove fields the client didn't ask for, so keep resolver responses close to what your queries select. The endpoint's schema field is for your reference.
- Missing resolver: if no resolver matches, the response is an
errorsentry saying which operation had none. - Plans and counting: GraphQL mocks are part of every plan, including Free. Each HTTP request counts as one request, however many fields it asks for.
New to Mockhop? Start with the quickstart. Plans: pricing.