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

react-graphql improvements #726

Merged
merged 5 commits into from
May 31, 2019
Merged

react-graphql improvements #726

merged 5 commits into from
May 31, 2019

Conversation

lemonmade
Copy link
Member

This PR makes a couple of small improvements to react-graphql:

  1. When you pass skip to useQuery on the server, it no longer attempts to fetch anything at all (before, it would try to resolve an async GraphQL document, mark it as used, and actually run the query)
  2. When variables stay deep equal, the result of useQuery is referentially equal (it always returned the same data, but the object itself was changed between renders, which is a bummer because it's a common one to use as an input to other useMemos)

In fixing (2), I also made an improvement to react-testing. Previously, calling setProps on a custom mounted element would set props on whatever the component was wrapped in, not the component itself. The changes to react-testing makes it so that only the element itself is updated with new props, which removes the need for hacks like https://github.com/Shopify/web/blob/master/tests/modern/AppContext.tsx#L63-L66.

@@ -52,10 +53,11 @@ export default function useGraphQLDocument<
[document, loadDocument],
);

return [
document,
useAsyncAsset(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also moved this back into this hook, where it feels more appropriate than the useQuery hook

useAsyncAsset(id);
if (typeof window === 'undefined' && skip) {
return createDefaultResult(client, variables);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the early bailout

[queryObservable, client, variables],
() => createDefaultResult(client, variables, queryObservable),
// eslint-disable-next-line react-hooks/exhaustive-deps
[queryObservable, client, serializedVariables],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix for the result changing when variables were deep equal

afterEach(() => {
teardownAsyncReactTasks();
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed

const wrapper = new CustomRoot(element, context, {
render: element => render(element, context, options),
resolveRoot: root => root.find(element.type),
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic gist of this fix is that instead of a Root around the result of render(element), the Root just gets the raw element under test, and we thread through the render to the component that eventually renders the component to the DOM, since it is in charge of handling updated props.

@lemonmade lemonmade merged commit 9324cff into master May 31, 2019
@lemonmade lemonmade deleted the graphql-fixes branch May 31, 2019 15:07
@michenly michenly mentioned this pull request Jun 20, 2019
5 tasks
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