<#1167 [GraphQL] The consumer contract test fails ...
# pact-js-development
g
#1167 [GraphQL] The consumer contract test fails always when the query has an input on the third-level Issue created by OlgaLa Software versions Please provide at least OS and version of pact-js • Consumer Pact library: @pact-foundation/pact: 11.0.2 • Node Version: v18.17.0 Issue Checklist Please confirm the following: • [+] I have upgraded to the latest • [+] I have the read the FAQs in the Readme • [+] I have triple checked, that there are no unhandled promises in my code and have read the section on intermittent test failures • [+] I have set my log level to debug and attached a log file showing the complete request/response cycle ☐ For bonus points and virtual high fives, I have created a reproduceable git repository (see below) to illustrate the problem Expected behaviour I have a consumer contract test for a query with specific input. Please see the query example in the Steps. And the test fails always with 500 error code:
Copy code
GraphQL Error (Code: 500): {"response":{"error":"Request-Mismatch : HttpRequest { method: \"POST\", path: \"/graphql\", query: None, headers: Some({\"content-length\": [\"559\"], \"accept\": [\"*/*\"], \"user-agent\": [\"node-fetch/1.0 (+<https://github.com/bitinn/node-fetch>)\"], \"accept-encoding\": [\"gzip\", \"deflate\"], \"connection\": [\"close\"], \"host\": [\"127.0.0.1:3200\"], \"content-type\": [\"application/json\"]}), body: Present(b\"{\\\"query\\\":\\\"query getItems($id: ID!, $startTime: Time!, $endTime: Time!, $status: Status) {\\\\n  element(id: $id) {\\\\n    config {\\\\n      items(\\\\n        filter: {timeRange: {start: $startTime, end: $endTime}, status: $status}\\\\n      ) {\\\\n        edges {\\\\n          node {\\\\n            id\\\\n          }\\\\n        }\\\\n      }\\\\n    }\\\\n  }\\\\n}\\\",\\\"variables\\\":{\\\"id\\\":\\\"123\\\",\\\"startTime\\\":\\\"2024-01-01T00:00:00Z\\\",\\\"endTime\\\":\\\"2024-01-01T07:09:04Z\\\",\\\"status\\\":\\\"PAYED\\\"},\\\"operationName\\\":\\\"getItems\\\"}\", Some(ContentType { main_type: \"application\", sub_type: \"json\", attributes: {}, suffix: None }), None), matching_rules: MatchingRules { rules: {} }, generators: Generators { categories: {} } }","status":500,"headers":{}},"request":{"query":"query getItems($id: ID!, $startTime: Time!, $endTime: Time!, $status: Status) {\n  element(id: $id) {\n    config {\n      items(\n        filter: {timeRange: {start: $startTime, end: $endTime}, status: $status}\n      ) {\n        edges {\n          node {\n            id\n          }\n        }\n      }\n    }\n  }\n}","variables":{"id":"123","startTime":"2024-01-01T00:00:00Z","endTime":"2024-01-01T07:09:04Z","status":"PAYED"}}}
Actual behaviour The test should pass. I added tests for another queries but they do not have the same input. Steps to reproduce I want to add a consumer contract test for graphql query. Here is the example of the query:
Copy code
query getItems($id: ID!, $startTime: Time!, $endTime: Time!, $status: Status) {
  element(id: $id) {
    config {
      items(filter: { timeRange: { start: $startTime, end: $endTime }, status: $status }) {
        edges {
          node {
            id
          }
        }
      }
    }
  }
}
I added the following tests:
Copy code
describe("description", () => {
        beforeEach(() => {
          const graphqlQuery = new GraphQLInteraction()
            .given("given")
            .uponReceiving("items")
            .withOperation("getItems")
            .withQuery(
                `query getItems($id: ID!, $startTime: Time!, $endTime: Time!, $status: Status) {
                      element(id: $id) {
                        config {
                          items(filter: { timeRange: { start: $startTime, end: $endTime }, status: $status }) {
                            edges {
                              node {
                                id
                              }
                            }
                          }
                         }
                        }
                      }`
            )
            .withRequest({
              path: "/graphql",
              method: "POST"
            })
            .withVariables({
              id: "123",
              startTime: "2024-01-01T00:00:00Z",
              endTime: "2024-01-01T07:09:04Z",
              status: Status.PAYED, 
            })
            .willRespondWith({
              status: 200,
              headers: {
                "Content-Type": "application/json; charset=utf-8"
              },
              body: {
                data: {
                    element: {
                        config: [
                            {
                              items: {
                                edges: [
                                  {
                                    node: {
                                      id: Matchers.somethingLike("456987")
                                    }
                                  }
                                ]
                              }
                            }
                          ]
                    }
                }
              }
            });
          return provider.addInteraction(graphqlQuery);
        });
    
        it("returns the correct response", async () => {
          const pactUrl = provider.mockService.baseUrl;
          const sdk = getSdk(new GraphQLClient(`${pactUrl}/graphql`));
          const resp = JSON.stringify(
            await sdk.getItems({
                id:"123",
                startTime: "2024-01-01T00:00:00Z",
                endTime: "2024-01-01T07:09:04Z",
                status: Status.PAYED
            })
          );
          expect(resp).toContain("456987");
        });
    });
The test fails always with 500 error. pact-foundation/pact-js