Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow merging of mocked queries in GraphQL update #2676

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Flufd
Copy link
Contributor

@Flufd Flufd commented Aug 14, 2023

Description

Allows applying partial changes to the mocked graphQL queries provided by the GraphQLController. This works similarly to the existing update option, but merges the existing mocked queries with the new set. This is useful in two cases that I think are relevant:

  • Reduces the amount of code required to update a single query response. We no longer need to pass all of the other queries that are not changing, in order to update the response of a single query. This improves readability of the code by making the intention more clear about what is actually being updated:
const wrapper = await mountWithAppContext(<MyComponent />, {
  graphQL: createGraphQL({
    Person: fillGraphQL(PersonQuery, initialPersonData),
    Pet: fillGraphQL(PetQuery, initialPetData),
  })
});

// ...

// Update just the Person query
// Previously, we would need to also supply the Pet query too, or it would be removed if we didn't pass one
wrapper.context.graphQL.updateWithMerge({
  Person: fillGraphQL(PersonQuery, newPersonData),
});
  • It allows composing the graphQL mocks via extending mounting functions.
export const mountWithPersonContext = mountWithAppContext.extend<
  Options,
  Context
>({
  context: ({
    graphQL,
  }) => {
    graphQL.updateWithMerge({
      Person: fillGraphQL(PersonQuery, personQueryData)
    });

    return {
      graphQL,
    };
  },
 // ...
});

Open questions

Should this be the default behaviour of the update function? I don't think many will be relying on the removal of mocks in their tests, which I think would be the only way this would break if this was the default behaviour of update.

Another approach might be to expose the mocks to the consumer so that we can reuse them. The result of createGraphQL is currently sealed, but it would be useful if we can take an existing helper that gives us a createGraphQL and be able to extend it to supply more mocks for other queries, without having to know what was passed in initially.

@Flufd Flufd requested a review from a team as a code owner August 14, 2023 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant