WireMock.Net icon indicating copy to clipboard operation
WireMock.Net copied to clipboard

How do I use GraphQLMatcher for file mapping

Open noxfate opened this issue 6 months ago • 5 comments

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

noxfate avatar Jul 18 '25 09:07 noxfate

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

StefH avatar Jul 18 '25 09:07 StefH

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 avatar Jul 19 '25 03:07 noxfate

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 avatar Jul 19 '25 07:07 StefH

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

StefH avatar Jul 19 '25 07:07 StefH

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 avatar Jul 21 '25 01:07 noxfate