I'm using Pact to mock our server side with graphql(Apollo) client. After trying some examples manually I did notice that majority of our current code performs exports like this:
import { gql } from '@apollo/client';
export const GET_USER_DETAILS = gql`
query GetUserDetails {
current_user {
id
first_name
last_name
username
email
gender
birth_date
home_phone
mobile_phone
}
}
`;
My question is how can I convert this back to string representation to give to withQuery
method of Pact ?
Extra complexity is that we are using Apollo cache which means that I need to add __typename
to each document which I did using
import { addTypenameToDocument } from '@apollo/client/utilities';
addTypenameToDocument(GET_USER_DETAILS)
I tried addTypenameToDocument(GET_USER_DETAILS).loc.source.body
- no __typename
present here which is causing Pact to fail mocking due to difference on expectation and actual query.
Use print
from the graphql
package (which you should probably already have installed).
import { print } from 'graphql';
console.log(print(GET_USER_DETAILS));