How do I use GraphQLMatcher for file mapping #705

Open
opened 2025-12-29 08:32:45 +01:00 by adam · 5 comments
Owner

Originally created by @noxfate on GitHub (Jul 18, 2025).

Originally assigned to: @StefH on GitHub.

Hi, I want to ask question about GraphQLMatcher and how to use them.

I've been trying to use them in both format, both on C# and Json Mapping but found no success. Basically, I don't understand the feature in overall.

Do I have to provided schema first ? then start mocking the query? and how would that look like especially in Json Mapping. To clarify my use case, let's say I have this as my schema for entire application.

var TestSchema = @"
  type Query {
   greeting:String
   students:[Student]
   studentById(id:ID!):Student
  }

  type Student {
   id:ID!
   firstName:String
   lastName:String
   fullName:String 
  }";

How do I match with query for /graphql with and stub the response

query MyNewQuery($studentId: Int!) {
    studentById(id: $studentId) {
        fullName
    }
}

Currently, my work around is to use JsonPartialMatcher but proven to not effective and I really want to try this feature.

I can help PR to update the docs once I can understand how its work. Thanks in advance

Originally created by @noxfate on GitHub (Jul 18, 2025). Originally assigned to: @StefH on GitHub. Hi, I want to ask question about [GraphQLMatcher](https://github.com/wiremock/WireMock.Net/wiki/Request-Matching-GraphQLMatcher) and how to use them. I've been trying to use them in both format, both on C# and Json Mapping but found no success. Basically, I don't understand the feature in overall. Do I have to provided schema first ? then start mocking the query? and how would that look like especially in Json Mapping. To clarify my use case, let's say I have this as my schema for entire application. ``` gql var TestSchema = @" type Query { greeting:String students:[Student] studentById(id:ID!):Student } type Student { id:ID! firstName:String lastName:String fullName:String }"; ``` How do I match with query for /graphql with and stub the response ``` gql query MyNewQuery($studentId: Int!) { studentById(id: $studentId) { fullName } } ``` Currently, my work around is to use `JsonPartialMatcher` but proven to not effective and I really want to try this feature. I can help PR to update the docs once I can understand how its work. Thanks in advance
adam added the question label 2025-12-29 08:32:45 +01:00
Author
Owner
@StefH commented on GitHub (Jul 18, 2025): You can take a look here:: - https://github.com/wiremock/WireMock.Net/wiki/Request-Matching-GraphQLMatcher - https://github.com/wiremock/WireMock.Net/blob/master/test/WireMock.Net.Tests/Matchers/GraphQLMatcherTests.cs - https://github.com/wiremock/WireMock.Net/blob/master/test/WireMock.Net.Tests/RequestMatchers/RequestMessageGraphQLMatcherTests.cs
Author
Owner

@noxfate commented on GitHub (Jul 19, 2025):

looks at the test file and documentation, still have questions. I see that in test requires 2 input TestSchema and the actual query input. The test is isolated to only focus on matcher, but Im not sure what will it looks like for the C# and Json Mapping file config. The documentation only show how to register the TestSchema part.

@noxfate commented on GitHub (Jul 19, 2025): looks at the test file and documentation, still have questions. I see that in test requires 2 input `TestSchema` and the actual query `input`. The test is isolated to only focus on matcher, but Im not sure what will it looks like for the C# and Json Mapping file config. The documentation only show how to register the `TestSchema` part.
Author
Owner

@StefH commented on GitHub (Jul 19, 2025):

I see your point, and I must admin the functionality is hard to understand, and not all is implemented, and the examples are not complete.

However I did update the WIKI - GraphQLMatcher page.

In short:

You can define 1 schema for the entire application, but that means that you need also another Body matcher, like JsonPartialMatcher or JsonPartialWildcardMatcher to further refine the matching.
The WithGraphQLSchema is mostly used to verify that the json request is a valid request according to the schema.

But can also define small schemas which should only match 1 request.

In general I think some more logic can be defined in the GraphQLMatcher, however I do not know yet how.
And I noticed that Mutations are not supported at all.

@StefH commented on GitHub (Jul 19, 2025): I see your point, and I must admin the functionality is hard to understand, and not all is implemented, and the examples are not complete. However I did update the [WIKI - GraphQLMatcher](https://github.com/wiremock/WireMock.Net/wiki/Request-Matching-GraphQLMatcher) page. ### In short: You can define 1 schema for the entire application, but that means that you need also another Body matcher, like JsonPartialMatcher or JsonPartialWildcardMatcher to further refine the matching. The `WithGraphQLSchema` is mostly used to verify that the json request is a valid request according to the schema. But can also define small schemas which should only match 1 request. In general I think some more logic can be defined in the GraphQLMatcher, however I do not know yet how. And I noticed that Mutations are not supported at all.
Author
Owner

@StefH commented on GitHub (Jul 19, 2025):

See also
https://github.com/wiremock/WireMock.Net/pull/1335

@StefH commented on GitHub (Jul 19, 2025): See also https://github.com/wiremock/WireMock.Net/pull/1335
Author
Owner

@noxfate commented on GitHub (Jul 21, 2025):

I see and noted on the mutation. So I assume that it should work like this on Json Mapping ? or this feature isn't support at the moment?

{
    "Guid": "67ae335b-5d79-42dc-8ca7-236280ab91ec",
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/graphql"
                }
            ]
        },
        "Body": {
            "Matcher": {
                "Name": "JsonPartialWildcardMatcher",
                "Patterns": "{ \"variables\": { \"sid\": \"1\" } }"
            }
        },
        "GraphQLSchema": "<your schema here>", // is this correct?
        "Methods": [
            "POST"
        ]
    },
    "Response": {
        "Body": "<your response>"
    }
}
@noxfate commented on GitHub (Jul 21, 2025): I see and noted on the mutation. So I assume that it should work like this on Json Mapping ? or this feature isn't support at the moment? ```jsonc { "Guid": "67ae335b-5d79-42dc-8ca7-236280ab91ec", "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/graphql" } ] }, "Body": { "Matcher": { "Name": "JsonPartialWildcardMatcher", "Patterns": "{ \"variables\": { \"sid\": \"1\" } }" } }, "GraphQLSchema": "<your schema here>", // is this correct? "Methods": [ "POST" ] }, "Response": { "Body": "<your response>" } } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#705