You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add Documentation for Relay Edge implementation #59
In the Relay mutation docs, performing RANGE_ADD requires that the new edge created by the mutation is provided in the payload.
I do not know how to provide the correct cursor/Edge construction within the relay.ClientIDMutation subclass.
The following code I wrote:
new_object_edge = graphene.Field('ObjectEdge')
I believed would solve my issue, with a proper resolve_new_object_edge(...) resolver.
However, my resolver cannot reference the relay.ConnectionField that is specified on one of the other object types in my mutation, since it only has the context of my (in this case, Django) objects.
I experimented with relay.Edge.for_node(SomeObjectType) as well but all of the solutions I have tried so far modify the schema correctly but cannot return the appropriate Edge.
The implementation of this scenario in javascript can be found here in the Relay examples directory.
Any idea of a best approach? I can take this question to stack overflow, but felt that people using Relay & Graphene would enjoy seeing an example solution in the docs.
While we investigate a solution to this, is there a way to incorporate an Edge as the payload and then write schema with vanilla python-graphql objects? Basically, can I write this in Python and reference it as an output field?
If not, my next intermediate solution was to produce the schema with Graphene and then introspect the produced schema, modifying the payload.
As for how Graphene could implement this: we only need a cursor_id field to be returned as well as node. I'm not sure how cursor_id is computed, but if we compute that string via the Edge constructor and pass it the relevant node, would that suffice? I was trying to use cursorForObjectInConnection to no avail.
I will try to have this working with graphene around this week.
PS: Graphene is using graphql-relay library under the hoods, but the function cursor_for_object_in_connection is not yet implemented there, so probably I will implement this in graphql-relay before anything else.
I see the cursor_for_object_in_connection and it appears functionally equivalent to the JS repo (and passes on the corresponding tests). Until it is working(?), however, using a graphQLCoreType is tough since the aforementioned method is used to generate the opaque cursor string. I'm assuming I can just generate a naive cursor in the meantime, but querying with before and after would likely be broken when relay tries to reconcile the edge with an existing connection list.
I'll take a look at this next week if it's still open and try a PR. Thanks for the great work so far!
@thebritican Did you find a work-around for this? I'm creating a cursor via offset_to_cursor and trying to return it as an edge, but can't get it to work.
@syrusakbary What would the "theGraphQLCoreType" be? My connection is to objects of type Comment, so I've been trying to use CommentEdge. It produces the correct schema, the execution errors with Cannot return null for non-nullable field CommentEdge.cursor, which I've tried to set in mutate_and_get_payload in a couple of different ways ...
Current implementation of cursor_for_object_in_connection relies heavily on the fact that data is provided as a list. What about Django ORM/SQLAlchemy where there may be thousands of records? This would cause to fetch them all. It doesn't seem like a good idea.
@syrusakbary I'm currently struggling with this too. I have something like this:
classCreateEvent(graphene.Mutation):
classInput:
description=graphene.String()
start_date=graphene.String()
end_date=graphene.String()
# ...all_day=graphene.Boolean()
event_edge=graphene.Field('EventNodeEdge')
event=graphene.Field(EventNode)
@classmethoddefmutate(cls, instance, args, info):
event=Event.objects.create(**args)
cursor=cursor_for_object_in_connection([e.idforeinEvent.objects.all()], event.id)
assertcursorreturncls(
event=event,
# Side question: Which Edge class should this be? I see two:# graphql_relay.connection.connectiontypes.Edge# graphene.relay.Edgeevent_edge=Edge(
node=event,
cursor=cursor,
)
)
Currently I'm receiving "Cannot return null for non-nullable field EventNodeEdge.cursor.". However, cursor_for_object_in_connection() is returning a string value.
This is driving me up the wall. Still receiving Cannot return null for non-nullable field EventNodeEdge.cursor errors. I'd be willing to offer a bounty for some clear documentation if it can be done ASAP.
Thank you very much @syrusakbary, that'll be really useful :-)
For now I've managed a temporary workaround by just doing this.props.relay.forceFetch() after the mutation has been executed, thereby reloading all currently rendered data from the server.
@adamcharnock I'm revising this now.
Are you using graphene.Mutation but it seems you should be using relay.ClientIDMutation and mutate_and_get_payload function, as Edges are only relevant to Nodes.
@syrusakbary I could swear I replied to this! Sorry for the delay. I had actually using both relay.ClientIDMutation and 'mutate_and_get_payload ' with no luck. I may be working on this again soon so it would be great if I could figure this stuff out :-)
Fundamentally, index-based cursors are never going to work, because you need the full context of the list to know what the index will be. In a relay context, that means a different cursor for every sort/filter combo that might be addressing the edge. The data required for that is out of control, IMO.
Stepping back, the only requirement for a cursor is that you can select before and after it. Thus, the simplest type of non-index cursor is the sort value for the list. For example, say you have a list of objects with created_date fields, ordered by created_date. In that case, your cursor could literally be the created_date value for the object, since you can always .filter(created_date__gt=cursor) to slice the results.
Optionally, if you want the sort field to be dynamic, you can use an object's unique id. In that case you would need to query the sort value matching that id and perform a similar query.
As far as I can tell, once of these two options is required to solve this issue.
@defrex It is just a tricky way to get the new cursor while performing a RANGE_ADD request, where the new edge should be the last one. It's not a clean one but a fast one for the reason that you don't need to iterate the list at all. In addition, RANGE_ADD is the only situation that a cursor is required on the server-side explicitly, as far as I'm concerned.
@varunarora@defrex@thebritican in the version 1.0 of Graphene Relay edges and connections are created in a easier, cleaner and more understandable way.
@reinierpdEdge is a very custom ObjectType that depends on the node of the connection, therefore will be a new Edge type per Connection node.
Once you have a specific Connection type (MyCustomConnection), you can easily retrieve it's edge with MyCustomConnection.Edge.
Closing the issue as I think Connections are better defined in later versions of Graphene.
Please comment or reopen if you feel there is still not enough info into how to us Connections/Edges now.
Since I struggled a lot to expose a newly created item as edge, here is a complete example using Graphene 2.0, building on the suggestions from @mickeyinfoshan and @varuna82. Hope this is of some help:
Activity
syrusakbary commentedon Dec 10, 2015
@thebritican I'm still thinking a good way for handling the case you commented.
gregziegan commentedon Dec 14, 2015
While we investigate a solution to this, is there a way to incorporate an
Edgeas the payload and then write schema with vanillapython-graphqlobjects? Basically, can I write this in Python and reference it as an output field?If not, my next intermediate solution was to produce the schema with Graphene and then introspect the produced schema, modifying the payload.
As for how Graphene could implement this: we only need a
cursor_idfield to be returned as well asnode. I'm not sure howcursor_idis computed, but if we compute that string via the Edge constructor and pass it the relevant node, would that suffice? I was trying to usecursorForObjectInConnectionto no avail.syrusakbary commentedon Dec 14, 2015
@thebritican meanwhile you can use raw
graphql-coreobjects as fields.So this would work:
I will try to have this working with graphene around this week.
PS: Graphene is using
graphql-relaylibrary under the hoods, but the functioncursor_for_object_in_connectionis not yet implemented there, so probably I will implement this ingraphql-relaybefore anything else.gregziegan commentedon Jan 6, 2016
I see the
cursor_for_object_in_connectionand it appears functionally equivalent to the JS repo (and passes on the corresponding tests). Until it is working(?), however, using agraphQLCoreTypeis tough since the aforementioned method is used to generate the opaque cursor string. I'm assuming I can just generate a naive cursor in the meantime, but querying withbeforeandafterwould likely be broken when relay tries to reconcile the edge with an existing connection list.I'll take a look at this next week if it's still open and try a PR. Thanks for the great work so far!
mikberg commentedon Jan 14, 2016
@thebritican Did you find a work-around for this? I'm creating a cursor via
offset_to_cursorand trying to return it as an edge, but can't get it to work.@syrusakbary What would the "theGraphQLCoreType" be? My connection is to objects of type
Comment, so I've been trying to useCommentEdge. It produces the correct schema, the execution errors withCannot return null for non-nullable field CommentEdge.cursor, which I've tried to set inmutate_and_get_payloadin a couple of different ways ...rafales commentedon Jan 27, 2016
Current implementation of
cursor_for_object_in_connectionrelies heavily on the fact that data is provided as a list. What about Django ORM/SQLAlchemy where there may be thousands of records? This would cause to fetch them all. It doesn't seem like a good idea.adamcharnock commentedon Feb 4, 2016
@syrusakbary I'm currently struggling with this too. I have something like this:
Currently I'm receiving "Cannot return null for non-nullable field EventNodeEdge.cursor.". However,
cursor_for_object_in_connection()is returning a string value.Any help here would be very welcome indeed!
adamcharnock commentedon Feb 6, 2016
This is driving me up the wall. Still receiving
Cannot return null for non-nullable field EventNodeEdge.cursorerrors. I'd be willing to offer a bounty for some clear documentation if it can be done ASAP.syrusakbary commentedon Feb 6, 2016
@adamcharnock I will work on this ASAP (is superbowl here so probably in the weekend will be difficult).
No need for bounties 😉
adamcharnock commentedon Feb 8, 2016
Thank you very much @syrusakbary, that'll be really useful :-)
For now I've managed a temporary workaround by just doing
this.props.relay.forceFetch()after the mutation has been executed, thereby reloading all currently rendered data from the server.defrex commentedon Feb 10, 2016
BTW, a couple tips for anyone else looking at this. You can grab a reference to a Relay edge like so.
Also, if you're using array cursors for now, this is probably the most performant option.
syrusakbary commentedon Feb 13, 2016
@adamcharnock I'm revising this now.
Are you using
graphene.Mutationbut it seems you should be usingrelay.ClientIDMutationandmutate_and_get_payloadfunction, as Edges are only relevant toNodes.Let me know if that's the problem.
adamcharnock commentedon Feb 29, 2016
@syrusakbary I could swear I replied to this! Sorry for the delay. I had actually using both
relay.ClientIDMutationand 'mutate_and_get_payload ' with no luck. I may be working on this again soon so it would be great if I could figure this stuff out :-)2 remaining items
mickeyinfoshan commentedon Apr 21, 2016
Any new progress about this issue?
mickeyinfoshan commentedon Apr 24, 2016
I've found a tricky way to get the new edge, which may help.
use
offset_to_cursoringraphql_relay.connection.arrayconnection.Here's an example:
defrex commentedon Apr 25, 2016
@mickeyinfoshan that assumes your edge is at the end of the list. If it's somewhere in the list, but you don't know the offset, this won't work.
defrex commentedon Apr 25, 2016
Fundamentally, index-based cursors are never going to work, because you need the full context of the list to know what the index will be. In a relay context, that means a different cursor for every sort/filter combo that might be addressing the edge. The data required for that is out of control, IMO.
Stepping back, the only requirement for a cursor is that you can select before and after it. Thus, the simplest type of non-index cursor is the sort value for the list. For example, say you have a list of objects with
created_datefields, ordered bycreated_date. In that case, your cursor could literally be thecreated_datevalue for the object, since you can always.filter(created_date__gt=cursor)to slice the results.Optionally, if you want the sort field to be dynamic, you can use an object's unique
id. In that case you would need to query the sort value matching thatidand perform a similar query.As far as I can tell, once of these two options is required to solve this issue.
mickeyinfoshan commentedon Apr 26, 2016
@defrex It is just a tricky way to get the new cursor while performing a RANGE_ADD request, where the new edge should be the last one. It's not a clean one but a fast one for the reason that you don't need to iterate the list at all. In addition, RANGE_ADD is the only situation that a cursor is required on the server-side explicitly, as far as I'm concerned.
varunarora commentedon Jun 27, 2016
Is there any temporary version of documentation on this? Perhaps in another branch?
syrusakbary commentedon Aug 6, 2016
@varunarora @defrex @thebritican in the version 1.0 of Graphene Relay edges and connections are created in a easier, cleaner and more understandable way.
Please take a look!
https://github.com/graphql-python/graphene/blob/master/graphene/relay/tests/test_connection.py
richburdon commentedon Oct 19, 2016
Broken link (https://github.com/graphql-python/graphene/blob/1.0/graphene/relay/tests/test_connection.py)
Also, the official example doesn't seem to implement creating edges in the mutation.
https://github.com/graphql-python/graphene/blob/master/examples/starwars_relay/schema.py
So I'm still getting:
Error: Cannot query field "createItemEdge" on type "CreateItemMutationPayload".
Because My parent class defines:
items = relay.ConnectionField(Item)
varuna82 commentedon Oct 19, 2016
Here's how I create an edge. There might be a better solutions, but hope this will help for now.
Following link explains how to create custom connection.
http://docs.graphene-python.org/en/latest/relay/connection/
richburdon commentedon Oct 19, 2016
Thanks @varuna82 really helpful.
reinierpd commentedon May 31, 2017
I'm using graphene-django 1.3 and neither
relay.types.Edgeorrelay.Edgeexist. Any idea from where can i import Edge ?? @mickeyinfoshansyrusakbary commentedon Jun 24, 2017
@reinierpd
Edgeis a very customObjectTypethat depends on the node of the connection, therefore will be a newEdgetype per Connection node.Once you have a specific
Connectiontype (MyCustomConnection), you can easily retrieve it's edge withMyCustomConnection.Edge.Closing the issue as I think Connections are better defined in later versions of Graphene.
Please comment or reopen if you feel there is still not enough info into how to us Connections/Edges now.
rangermeier commentedon Oct 26, 2017
Since I struggled a lot to expose a newly created item as edge, here is a complete example using Graphene 2.0, building on the suggestions from @mickeyinfoshan and @varuna82. Hope this is of some help:
Merge pull request graphql-python#60 from timothyjlaurent/graphql-pyt…