Adding contrib for GraphQL Relay #1214
Draft
+807
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related to #1213.
Important
The schema used for this example comes from https://relay.dev/docs/guides/graphql-server-specification/#schema
Doc strings with examples are yet to come so here's a brief manual on how to use this:
RelayQueryType
Use the QueryType that enforces the
node
GQL logic while providing some utility:or if you have the node resolvers already:
You can map to a different
id
field if yourNode
interface specifies something else thanid
, likebid
:RelayObjectType and RelayConnection
There's the
RelayObjectType
which is similar to the standardObjectType
but has the relay "pagination" logic encapsulated in theconnection
decorator. The decorator takes a resolver that will later be invoked with an additionalconnection_arguments
argument that holds yourfirst
,last
,after
,before
. The resolver is expected to fetch a slide of data from the resource's storage and return it, encapsulated in aRelayConnection
instance with additional pagination data - in gist - the resolver is responsible for the page calculations.RelayConnection
is something one would want to overload to provide some repeatable utility, maybe you'd like aDjangoRelayConnection
to operate on a Django ORM QuerySet and maybe calculate the paging there to reduce the work that's needed to be done in the resolver. Overloadingget_cursor
,get_page_info
,get_edges
onRelayConnection
should enable one to achieve a lot but here it's important to listen to the use cases people might have.I'm open to feedback, thanks :)