americanexpress / parrot

✨ Scenario-based HTTP mocking
Apache License 2.0
137 stars 29 forks source link

Graphql MockServer Resolver Preservation #253

Open cchen9299 opened 2 weeks ago

cchen9299 commented 2 weeks ago

Motivation: With the introduction of parrot-friendly@5.0.0 and parrot-graphql@5.0.0, the underlying @graphql-tools/mock went from v2 to v8. Through this change, mocked resolver behaviors changed and for our teams, specifically the ability to key into request information to return corresponding mock data.

To elaborate, given there is a feature where the UI displays email information of multiple users and there is an operation that fetches user's email. The UI can either be making aliased calls or multiple calls to graphql with the user's ID expecting that the corresponding data is returned for each user ID. Previously we were able to mock the resolver dynamically with the first argument in the mocked resolver. But with graphql-toolsV8 by default, those resolvers are seemingly flattened and no longer provide the request information.

const { graphql } = require('parrot-friendly')
const { getUserEmail } = require('./helper');

...

it('US - CARD & BANKING (multi) - Mobile Phone - security update failure', () => {
  graphql('/graphql', schema, {
    Query: () => ({
      // resolver args are now undefine
      getEmail: ({ userID }) => getUserEmail(userID),
    }),
  });
});

The only solution that I've found so far is to update the preserveResolver value to true (the third argument passed to mockServer of @graphql-tools/mock). I am then able to pass in makeExecutableSchema({}) of the basically the same resolver to see the request information again. (Basically the same because graphql-tools have since move the request variable to the second argument ie., getEmail: (_, { userID }) => getUserEmail(userID),)

Will be opening a PR for this for review.

github-actions[bot] commented 2 weeks ago

Thanks for opening your first issue. Pull requests are always welcome!