Skip to content

GraphQL Extension

WireMock Cloud

WireMock Cloud provides rapid GraphQL mocking with advanced features like dynamic state and federation support.
Try WireMock Cloud >

The WireMock GraphQL Extension provides semantic verification of GraphQL requests, allowing you to match queries based on their meaning rather than exact text matching. This extension normalizes and sorts queries, handles whitespace differences, and compares variables intelligently.

The GraphQL extension offers:

  • Semantic query matching - Queries are normalized and sorted, so field order doesn’t matter
  • Variable comparison - Compares GraphQL variables using JSON matching logic
  • Whitespace handling - Ignores formatting differences in queries
  • GraphQL parsing - Uses the graphql-java library for accurate parsing and normalization

note

This extension requires WireMock 3.x. WireMock 2.x is not supported from version 0.6.0 onwards.

Add the GraphQL extension to your project using Maven or Gradle:

<dependency>
<groupId>io.github.nilwurtz</groupId>
<artifactId>wiremock-graphql-extension</artifactId>
<version>0.9.0</version>
<scope>test</scope>
</dependency>

The extension provides the GraphqlBodyMatcher for matching GraphQL requests. You can specify the expected query and optionally the expected variables:

import com.github.tomakehurst.wiremock.client.WireMock;
import io.github.nilwurtz.GraphqlBodyMatcher;
import java.util.Map;
var expectedQuery = """
query HeroInfo($id: Int) {
hero(id: $id) {
name
}
}
""";
var expectedVariables = Map.of("id", 1);
WireMock.stubFor(WireMock.post(WireMock.urlEqualTo("/graphql"))
.andMatching(GraphqlBodyMatcher.extensionName,
GraphqlBodyMatcher.parameters(expectedQuery, expectedVariables))
.willReturn(WireMock.okJson("""
{
"data": {
"hero": {
"name": "Luke Skywalker"
}
}
}""")));

When running WireMock as a standalone server (e.g., in Docker), you need to include the extension JAR file and register it.

Download wiremock-graphql-extension-x.y.z-jar-with-dependencies.jar from the releases page.

Terminal window
docker run -it --rm \
-p 8080:8080 \
--name wiremock \
-v /path/to/wiremock-graphql-extension-0.9.0-jar-with-dependencies.jar:/var/wiremock/extensions/wiremock-graphql-extension-0.9.0-jar-with-dependencies.jar \
wiremock/wiremock \
--extensions io.github.nilwurtz.GraphqlBodyMatcher

When using a remote WireMock server, you can register stubs programmatically:

import com.github.tomakehurst.wiremock.client.WireMock;
import io.github.nilwurtz.GraphqlBodyMatcher;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
public void registerGraphQLWiremock(String query, String response) {
WireMock wireMock = new WireMock(8080);
wireMock.register(
post(urlPathEqualTo("/graphql"))
.andMatching(GraphqlBodyMatcher.extensionName,
GraphqlBodyMatcher.parameters(query))
.willReturn(okJson(response))
);
}