GitHub
01/19/2024, 11:17 AMGraphQL 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:
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:
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