Hi All. I am getting this below error ```1) return...
# pact-js
m
Hi All. I am getting this below error
Copy code
1) 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
Copy code
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
Copy code
.willRespondWith({
            status: 200,
            headers: {
              'Content-Type': 'application/json; charset=utf-8'
            },
y
I believe there is a bug, if you drop the content-type header it should be automatically detected
m
I commented the headers in both consumer and consumer spec. Same issues coming up
I used const graphqlQuery = new ApolloGraphQLInteraction()
y
cool. you’ve not shared the whole test so its really hard to tell
m
consumer.js
Copy code
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'
    //},
  })
});

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 = query
y
the log message is about the request, not the response, what does your full consumer.spec.js look like
m
consumer.spec.js
Copy code
const 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())
    })
  })
y
Ahh the issue is probably in
ApolloGraphQLInteraction
if that is setting a content header on the request
I’ve not used that wrapper myself
We’ve not updated it for the V3 interface because of reasons https://github.com/pact-foundation/pact-js/issues/1093 See https://github.com/pact-foundation/pact-reference/issues/306 & https://github.com/pact-foundation/pact-js/issues/1058 for related issues with the content-type header, which I believe will be causing your issues
If this has only just occurred, you might be able to pin a version of your pact-js lib
until its fixed upstream
ty for code snippets btw
m
I changed the const graphqlQuery = new GraphQLInteraction() but still facinf the same issue
y
yes, that the apollo one is just a wrapper around the graphql one, which is a wrapper around the pact-js’s dsl
it sets the content type
you can just create your own wrapper, or modify that, to not set the content-type for now
m
Comment the code line works fine. Is there any link for creating custom wrapper around existing code and using it in our code base ?