SurveyMonkey / graphql-ergonomock

Automatic mocking of GraphQL queries
MIT License
19 stars 7 forks source link

feat: allow users to provider default mock resolvers #113

Closed victor-guoyu closed 3 years ago

victor-guoyu commented 3 years ago

Issue: https://github.com/SurveyMonkey/graphql-ergonomock/issues/104

Allows users to provide default mock resolver.

In the case of custom scalar types, we only need to provide a default mock resolver function at the mock provider level and all the occurrences within the query should automatically using the value provided by the mock resolver.

example test case

// setup
render(
    <MockedProvider
      schema={schema}
      resolvers={{
        Shape: (_, args) => ({
          returnString: `John Doe ${args.id}`,
        }),
      }}
    >
      <Parent shapeId="123" />
    </MockedProvider>
  );

  // assert
   expect(response).toMatchObject({
    data: {
      queryShape: {
        __typename: "Shape",
        returnString: "John Doe 123",
        returnInt: expect.toBeNumber(),
      },
    },
  });
joual commented 3 years ago

This looks good. Can you amend the README.md with the API changes?

jfulse commented 3 years ago

I tried testing this with npm link to see if it solves https://github.com/SurveyMonkey/graphql-ergonomock/issues/104. Unfortunately I run into an Could not find "client" in the context or passed in as an option error. I tried making sure the relevant libraries are the same versions in my own repo and in graphql-ergonomock; can you think of another reason why this might be?

victor-guoyu commented 3 years ago

@jfulse sorry for the late reply! I think that error usually occurs when the test component is not wrapped by the ApolloProvider or the client is actually missing.

I might able to help more if you can share a code snippet

joual commented 3 years ago

:tada: This PR is included in version 1.2.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

jfulse commented 3 years ago

@jfulse sorry for the late reply! I think that error usually occurs when the test component is not wrapped by the ApolloProvider or the client is actually missing. I might able to help more if you can share a code snippet

I tested now with version 1.2.0 instead of npm link and it works, with custom scalars and everything. Thank you so much for all the help and the new feature!