Skip to content

GraphQL

GraphQL is not a separate transport in Mockifyr. A GraphQL stub is an ordinary POST /graphql mapping whose request pattern uses the built-in graphql-body-matcher custom matcher. Everything else — templating, scenarios, delays, tenants — is unchanged.

{
"request": {
"method": "POST",
"urlPath": "/graphql",
"customMatcher": {
"name": "graphql-body-matcher",
"parameters": {
"query": "query GetOrder($id: ID!) { order(id: $id) { id state } }",
"variables": { "id": "A-1" },
"operationName": "GetOrder"
}
}
},
"response": {
"status": 200,
"jsonBody": { "data": { "order": { "id": "A-1", "state": "SHIPPED" } } }
}
}

Parameters

ParameterTypeRequired
querystring — the GraphQL documentYes
variablesJSON objectNo
operationNamestringNo

Query normalization

The query is not compared as text. Both the stub’s query and the incoming request’s query are parsed, their selections, arguments and directives are sorted, and each is printed canonically before comparison.

That means whitespace, indentation, and the order of fields, arguments and directives are all irrelevant to matching. These two match each other:

query { order(id: "A-1") { state id } }
query {
order(id: "A-1") {
id
state
}
}

Omitted means absent, not “any”

This is the semantic that catches people out, so it is worth stating flatly:

If your client sends variables, declare them in the stub:

"parameters": {
"query": "query GetOrder($id: ID!) { order(id: $id) { id state } }",
"variables": { "id": "A-1" }
}

Templating the response

Response templating uses the standard response-template transformer, with one thing to know: request.body is the original GraphQL POST body — the full JSON envelope with query, variables and operationName — not the parsed document.

{
"request": {
"method": "POST",
"urlPath": "/graphql",
"customMatcher": {
"name": "graphql-body-matcher",
"parameters": {
"query": "query GetOrder($id: ID!) { order(id: $id) { id state } }",
"variables": { "id": "A-1" }
}
}
},
"response": {
"status": 200,
"transformers": ["response-template"],
"jsonBody": {
"data": {
"order": {
"id": "{{jsonPath request.body '$.variables.id'}}",
"state": "SHIPPED"
}
}
}
}
}

Read variables with $.variables.…, the operation name with $.operationName, and the raw document with $.query. See templating and the template helper reference.