Mahesh Damavarapu
08/10/2023, 1:29 PM1) returns the correct response
Pact verification failed!
Test failed for the following reasons:
Mock server failed with the following mismatches:
0) The following request was incorrect:
POST /graphql
1.0 Mismatch with header 'Content-Type': Expected value 'application/json' at index 1 but was missing (actual has 1 value(s))
My code from consumer.js is
const { ApolloClient, InMemoryCache, HttpLink, gql } = require('@apollo/client/core');
const fetch = require('cross-fetch');
const client = new ApolloClient({
cache: new InMemoryCache({ addTypename: false }),
link: new HttpLink({
uri: '<http://127.0.0.1:5000/graphql>', fetch,
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
})
});
consumer.spec.js is
.willRespondWith({
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
},Yousaf Nabi (pactflow.io)
Mahesh Damavarapu
08/10/2023, 1:49 PMMahesh Damavarapu
08/10/2023, 1:49 PMYousaf Nabi (pactflow.io)
Mahesh Damavarapu
08/10/2023, 1:51 PMconst { ApolloClient, InMemoryCache, HttpLink, gql } = require('@apollo/client/core');
const fetch = require('cross-fetch');
const client = new ApolloClient({
cache: new InMemoryCache({ addTypename: false }),
link: new HttpLink({
uri: '<http://127.0.0.1:5000/graphql>', fetch,
//headers: {
// 'Content-Type': 'application/json; charset=utf-8'
//},
})
});
async function query() {
return await client
.query({
query: gql`
query Query($input: pageInput)
{
page(input: $input) {
id_client
id_page
similar_shops_widget {
id
widget_api_mapping
__typename
}
__typename
}
}
`,
variables: {
"input": {
"id_retailer": "****",
"id_client":"*****"
}
},
});
//.then((result) => result.data);
}
module.exports.query = queryYousaf Nabi (pactflow.io)
Mahesh Damavarapu
08/10/2023, 1:53 PMconst chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
const path = require("path");
const { query } = require("../consumer");
const { Pact } = require("@pact-foundation/pact");
const { ApolloGraphQLInteraction } = require("@pact-foundation/pact")
const expect = chai.expect
chai.use(chaiAsPromised)
describe("GraphQL example", () => {
const provider = new Pact({
port: 5000,
log: path.resolve(process.cwd(), "logs", "mockserver-integration.log"),
dir: path.resolve(process.cwd(), "pacts"),
consumer: "GraphQLConsumer",
provider: "GraphQLProvider",
logLevel: 'trace',
})
before(() => provider.setup())
after(() => provider.finalize())
describe("query graphql on /graphql", () => {
before(() => {
const graphqlQuery = new ApolloGraphQLInteraction()
.uponReceiving("a graphql request")
.withQuery(
`
query Query($input: pageInput)
{
page(input: $input) {
id_client
id_page
similar_shops_widget {
id
widget_api_mapping
__typename
}
__typename
}
}
`
)
.withOperation('Query')
.withVariables(
{
"input": {
"id_retailer": "****",
"id_client":"****"
}
}
)
.withRequest({
path: "/graphql",
method: "POST",
})
.willRespondWith({
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: {
data: {
"page": {
"id_client": "****",
"id_page": "****",
"similar_shops_widget": {
"id": "***",
"widget_api_mapping": "wtSimilarShopsGroup"
}
}
},
},
})
return provider.addInteraction(graphqlQuery)
})
it("returns the correct response", async() => {
return expect(query()).to.eventually.deep.equal({ data: {
"page": {
"id_client": "***",
"id_page": "***",
"similar_shops_widget": {
"id": "***",
"widget_api_mapping": "wtSimilarShopsGroup"
}
}
}, })
const output = await query();
console.log('output ', output);
})
// verify with Pact, and reset expectations
afterEach(() => provider.verify())
})
})Yousaf Nabi (pactflow.io)
ApolloGraphQLInteraction if that is setting a content header on the requestYousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Mahesh Damavarapu
08/10/2023, 1:57 PMYousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Mahesh Damavarapu
08/10/2023, 2:15 PM