Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Allow merging of mocked queries in GraphQL update #2676

Closed
wants to merge 1 commit into 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
@znja znja closed this Oct 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants