Relay Resolvers are a replacement for client schema extensions
Opened this issue · 0 comments
Client scheme extensions are powerful, but present a number of challenges and pitfalls. These can be avoided by replacing client schema extensions with Relay Resolvers. In general, we believe Relay Resolvers should be able to replace client schema extensions. If there are places where Resolvers are not a replacement, we should see that as a gap in Relay’s features.
Below are a list of reasons why Resolvers are superior to client schema extensions.
Garbage collection
Relays garbage collector works at a query granularity meaning data is retained in the store as long as some query that reads that data is currently mounted. This means that client schema extension data written into the store will only live as long as it’s being observed by one or more queries. When the last query stop observing that field, the value will get GCed. This makes sense for server data, which can be refetched from the server, but is generally not what users want for client state which may be ultimate source of truth for data data. For example, it might be user supplied state which cannot be reconstructed without
With relay resolvers, specifically @live
resolvers, the source of truth for state is external to Relay’s store, which gives it an explicit lifetime.
Safety
With client schema extensions there is no way to statically guarantee that you have written a field’s value before you read it. This means we must assume all client scheme extension fields, maybe undefined.
With resolvers fields on the other hand Relay is in charge of evaluating the function, and thus can guarantee that the value will be computed by the time the value is read.
Update APIs
Our current APIs for updating client state are a combination of awkward to use or not typesafe. Relay Resolvers, on the other hand, are both typesafe and relatively intuitive to write.
What’s Next?
- Convert this issue into a documentation page, with links for any other documentation page which references client schema extensions.
- Provide examples of how to model client state using Relay Live Resolves and a Redux store (or similar).