X-mock-match-request-body with graphql

Hey there!

Im having an issue with tracking to mock a graphql call. So everything is working in postman, like I have the mock server running, the request, the example request, and if I use the Code -> curl I can copy that and paste into the console and it returns the correct data from the mock. Im using the "x-mock-match-request-body": "true" header.

But when I try to use it in an app Im building (flutter), it can’t find the request and I get

Network Error: 404 {"error":{"name":"mockRequestNotFoundError","message":"Double check your method and the request path and try again.","header":"No matching requests"}}

I checked the strings of the query and they both looks the same, same params, same number of spaces, etc. So Im wondering how the match algorithm works? Could some special character break the match?

Thanks

Hi @mustela,

In addition to the GraphQL query, make sure that your GraphQL variables are also the same. Note: There’s a difference in the matching algorithm between not listing any variables at all vs having "variables":{} in the request body. I believe Postman defaults to the latter. Check your example to see what’s there.

Best,

Kevin

Hey @kevin.swiber thanks for replying me back. I have checked and everything seems to be exactly the same, but for some reason it is not matching. The only difference I see is the “operationName” is not listed in the query that matches. But it’s part of the query.

Here you can see both requests the one that matches, using curl, and the one that don’t using flutter.

Query/variables/etc are exactly the same.

Hey @mustela,

Unfortunately, operationName isn’t currently supported in the GraphQL UI in Postman. I would recommend posting an issue here: https://github.com/postmanlabs/postman-app-support/issues

In the meantime, one workaround is to use a different request body style in the example editor. Right now, your example request body probably looks something like this with GraphQL selected:

If you select raw and type in the request body in JSON-style, you can add in operationName. This should match on your mock request. It should look something like this:

If you need to support both (with and without operationName), you can create another example.

Hope this helps!

Best,

Kevin

Kevin, thanks again! I did try to do that, now the body is exactly the same as the request but it is still failing, it’s like it tries to also match all the headers, which makes sense, but do you know how that works? like if in my example I have defined only Content-Type: application/json will it check if that key is present and won’t care about all the others or does that mean that that key should be the only present header?

Thanks

Request headers should only be checked for matching when x-mock-match-request-headers is set and includes a whitelisted set of headers upon which to check. See the docs here:

Mock requests also accepts optional headers x-mock-match-request-body for request body matching and x-mock-match-request-headers for matching incoming mock request headers. You must set x-mock-match-request-body header to true to enable request body matching. To enable incoming mock request headers matching, you must ensure that x-mock-match-request-headers header is present in the request and its value is a comma separated string of header keys that you want to match against in the saved examples.

So then, the body it’s not matching :frowning: somehow.
I’ve opened an issue, since Im not sure what else to do.

Thanks a lot for your help!

Hey @mustela,

One last thing. Make sure your request is sending the header Content-Type: application/json. I believe that’s required to match the example.

Thanks @kevin.swiber but that isn’t working neither.

@mustela,

Oh, also make sure you have a Content-Type on the example response set to application/json.

See docs here: https://learning.postman.com/docs/postman/mock-servers/mocking-with-examples/#mocking-graphql-queries