pact-foundation / pact-js

JS version of Pact. Pact is a contract testing framework for HTTP APIs and non-HTTP asynchronous messaging systems.
https://pact.io
Other
1.63k stars 346 forks source link

Pact verification fails when using graphql-request package for actual request #592

Closed dkulezich closed 3 years ago

dkulezich commented 3 years ago

Software versions

Issue Checklist

Description

We switched from Apollo to graphql-request in our project. Since then we use graphqlClient to do the requests to the Pact Mock server. Once we did that our pact tests started failing. We didn't change anything else and the tests were passing before.

Expected behaviour

Pact tests pass successfully when using graphql-request package

Actual behaviour

Tests are failing

Steps to reproduce

Replace ApolloClient with GraphQLClient

Relevant log files

Rejected to value: [Error: GraphQL Error (Code: 500): {"response":{"message":"No interaction found for POST /","interaction_diffs":[{"description":"stations query","body":{"operationName":{"EXPECTED":"StationsQuery","ACTUAL":"<key not found>"}}}],"status":500,"headers":{}},"request":{"query":"query StationsQuery {\n  stations {\n    id\n    name\n    logo {\n      url\n    }\n    stream {\n      url\n    }\n  }\n}\n","variables":{}}}]
mefellows commented 3 years ago

That error suggests that operationName should be StationsQuery but instead is not coming through at all.

That implies:

  1. You have operation name set in the graphql interaction
  2. Your code is not sending that interaction

If the behaviour is correct, then you need to update your expected interaction to not expect your code to send that operation name. Otherwise, you've just found a bug.

dkulezich commented 3 years ago

Thank you for your answer, Matt. I've removed the operation name when setting the graphql interaction and from the query and I still get the same error as described above. This behaviour is observed only when using graphql-request package, if I execute the same code with apollo client package it runs without errors and the contract is generated.

mefellows commented 3 years ago

Could you please attach the log file so we can see what's going on?

dkulezich commented 3 years ago

Sure, here are both logs with and without Operation name pact-noOperationName.log pact.log

mefellows commented 3 years ago

Thanks. So the pact.log file is the one failing, because as you say, it's expecting operationName but is not receiving it. The pact-noOperationName.log seems to be a working pact.

Can I assume that pact-noOperationName.log is the one where you were still using the apollo client? The user-agent looks the same, so I'm a bit confused.

mefellows commented 3 years ago

If you have a minute to chat in slack, i'll see you in #graphql, otherwise, I think I'm going to need to see your code to be able to help diagnose further.

dkulezich commented 3 years ago

Thanks, Mat, for your help. As you suggested I have removed the operationName from the test (deleted .withOperation("StationsQuery")) and added the headers: {}property to the the response ( to .willRespondWith()) and it did the job! Our tests are passing now and the contracts are successfully generated.

TimothyJones commented 3 years ago

Sounds like this got sorted out, so I'll close it. Feel free to reopen if there are any changes required.