Support Client Edge to Server for observeFragment
Motivation
We have a feature called Relay Resolvers which lets you define additional GraphQL fields which get evaluated on the client. We even allow you to define a field which points back to a type on the server. In that case we generate a query for each place the edge is read, and when you try to read that edge, we fetch the query. This logic is currently implemented for useFragment and other hook APIs but is not yet implemented for our new observeFragment API which powers some non-React APIs.
This is the only gap that is currently preventing observeFragment and waitForFragmentData from being promoted from experimental to stable.
Docs links
- [**https://relay.dev/docs/next/guides/relay-resolvers/return-types/#server-types**](https://relay.dev/docs/next/guides/relay-resolvers/return-types/#server-types)
- [**https://relay.dev/docs/next/api-reference/relay-runtime/api-reference/observe-fragment/**](https://relay.dev/docs/next/api-reference/relay-runtime/api-reference/observe-fragment/)
Code Pointers
- Would need to be handled [here](https://github.com/facebook/relay/blob/ba09a88925c05bfc40ba6eaa4fb0d9ff37a5d4f4/packages/relay-runtime/store/observeFragmentExperimental.js#L232) in
observeFragment. - Will need to remove [this invariant](https://github.com/facebook/relay/blob/ba09a88925c05bfc40ba6eaa4fb0d9ff37a5d4f4/packages/relay-runtime/store/observeFragmentExperimental.js#L122).
- The equivalent logic in
useFragmentis implemented [here](https://github.com/facebook/relay/blob/ba09a88925c05bfc40ba6eaa4fb0d9ff37a5d4f4/packages/react-relay/relay-hooks/useFragmentInternal_EXPERIMENTAL.js#L225). - Tests should be added [here](https://github.com/facebook/relay/blob/ba09a88925c05bfc40ba6eaa4fb0d9ff37a5d4f4/packages/relay-runtime/store/tests/observeFragment-test.js).
Tips for contributing can be found [here](https://github.com/facebook/relay/blob/ba09a88925c05bfc40ba6eaa4fb0d9ff37a5d4f4/.github/CONTRIBUTING.md#L4).
Broad goals:
- If missing data is discovered that needs to be fetched by a client edge query, it should be fetched and the observable be in a loading state while it’s fetching.
- Once the query response is received, we should
- While the observable is subscribed, the query should be retained (it should not be garbage collected by Relay’s GC.). When the observable is unsubscribed, it should be freed an eligible for GC. See [this doc](https://relay.dev/docs/next/guided-tour/accessing-data-without-react/retaining-queries/) for more information.
- All cases should be covered with unit tests.