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
Hi,
I would appreciate an insight about how to extend connection info with a custom object on the same level as "edges" and "pageInfo". For example please see "custom_object" below for location:
One way to do it for fields at the same level as an edge is to attach an attribute to the node, when you resolve them. And then to resolve for the field at the same level as edges by assessing the attribute.
you can do something similar at the connection level .. I explained this, tho, in more detail to you at graphql-python/graphene#573
class WishConnection(relay.Connection):
class Meta:
node = ProductType
class Edge:
through = graphene.Field(WishReasonType, description='Connection reason')
def resolve_through(self, info, **args):
return self.node.preason[0]
...
# In some Type
def resolve_products(self, info, **args):
qs = Reason.objects.filter(wish=self).only('reason', 'qty')
len = args.get
# notice how I add the preason attribute to each node
result = Paginator(self.products.prefetch_related(
Prefetch('reason_set', queryset=qs, to_attr='preason')).all(),
args.get('first', 20))
return result
I haven't actually tried this, but looking at the code for the graphene relay connection, and following it's __init_subclass_with_meta call, it eventually calls yank_fields_from_attrs, so I think you can subclass relay.Connection and add the field as a class attribute.
So I'm thinking this would work?
class ConnectionWithCustomObject(relay.Connection):
custom_object = graphene.Field(MyCustomObject)
class Meta:
node = MyNode
def resolve_custom_object(...)
...
And if it's something you want on all your connections, I think you could have the custom_object stuff on a base class and make all your connections use that. I'd love to hear what ends up working for you.
Hi @spockNinja, @japrogramer, Thank you for the advice!
I already have everything setup like this ^, however stuck with a more general picture.
So in brief:
-- currently resolver returns one original queryset
-- In addition to it, I need to pass result of a count operation.
Ideally I would assign it to some variable in the resolver and that variable would be accessible from the my custom connection type.
classTagConnection(relay.Connection):
classMeta:
node=TagNodetotal_count=graphene.Int()
resolve_total_count(self, info):
# here I somehow access the `some_count_var` variable and returnreturnsome_count_var
I can definitely see what you're trying to accomplish. Without whipping up a full example to test, I'm honestly not too sure of the best place to pass that information up, or if it should be passed up at all.
I'm thinking that if something is going to live outside of the edges, that it needs to be information that does not depend on resolutions inside of edges. In your example case, with a total Tag.objects.count(), there is no need to do that in the resolve_tags part, as you can call the same thing in resolve_total_count.
If you want some custom information showing up that is dependent on resolutions inside of edges, then I believe you should go with @japrogramer's suggestion and put it as a custom field on the Edge of the Connection and have access to the edge's nodes.
The only tricky part in this is to call the super() such that DjangoConnectionFields and ConnectionFields type() aren't called, in order to get the node's type.
@nuschk thank you for this example. I had a similar case where I wanted to add a "conveniences" nodes field to the connection in order to just get all nodes (ie. much like within the swapi where you have planets within the allPlanets connection) and your solution seems to work in my case.
Does this extension have other implications you are aware of? Possible side-effects?
Also, it would be great to have this supported within graphene-django out of the box by just declaring a field and a resolve function on a subclass of DjangoConnectionField.
@nuschk well I ran into a complication with the mentioned approach. I tried to use it for multiple connections and it errors out with AssertionError: Found different types with the same name in the schema: Connection, Connection. (within graphene/types/typemap.py", line 85, in graphene_reducer). Did you try it with multiple connections?
Well, my brain always melts when I dive into graphene's meta mechanics, so I'm not entirely sure my advice will be sound.
However, the problem is, that the inner Connection class is declared multiple times. I think the only way you can prevent this, is to declare it for each node type separately. An example with a connection to nodes of type Listing:
@nuschk wow. Thats a horrible amount of boilerplate code to simply add total field, especially considering that it should be done for each type...
I wonder if there more friendly solution available
@TheRexx thanks! It works for me with a small change to avoid class names clashing:
name = '{}FilterConnection'.format(self._type._meta.name)
But it works only for those connections declared in the query root, not the auto-generated for each type related list. Any idea of how to cover that case too?
@greenled You can bring this code to a separate module, and connect to the schema where it is needed. In any case, you prescribe nodes for each model manually and they have different parameters. If I did not understand your question correctly, please add a sample code.
Activity
[-]Extending connection info[/-][+]How to extend connection info?[/+]japrogramer commentedon Nov 11, 2017
One way to do it for fields at the same level as an edge is to attach an attribute to the node, when you resolve them. And then to resolve for the field at the same level as edges by assessing the attribute.
you can do something similar at the connection level .. I explained this, tho, in more detail to you at graphql-python/graphene#573
spockNinja commentedon Nov 15, 2017
I haven't actually tried this, but looking at the code for the graphene relay connection, and following it's
__init_subclass_with_metacall, it eventually callsyank_fields_from_attrs, so I think you can subclassrelay.Connectionand add the field as a class attribute.So I'm thinking this would work?
And if it's something you want on all your connections, I think you could have the custom_object stuff on a base class and make all your connections use that. I'd love to hear what ends up working for you.
oharlem commentedon Dec 1, 2017
Hi @spockNinja, @japrogramer, Thank you for the advice!
I already have everything setup like this ^, however stuck with a more general picture.
So in brief:
-- currently resolver returns one original queryset
-- In addition to it, I need to pass result of a count operation.
Ideally I would assign it to some variable in the resolver and that variable would be accessible from the my custom connection type.
Example:
then
hope this all makes sense!
P.S.
@japrogramer , @spockNinja
Apologies for the late response guys :|
spockNinja commentedon Dec 17, 2017
@mpmlj
I can definitely see what you're trying to accomplish. Without whipping up a full example to test, I'm honestly not too sure of the best place to pass that information up, or if it should be passed up at all.
I'm thinking that if something is going to live outside of the
edges, that it needs to be information that does not depend on resolutions inside ofedges. In your example case, with a totalTag.objects.count(), there is no need to do that in theresolve_tagspart, as you can call the same thing inresolve_total_count.If you want some custom information showing up that is dependent on resolutions inside of
edges, then I believe you should go with @japrogramer's suggestion and put it as a custom field on theEdgeof theConnectionand have access to the edge's nodes.nuschk commentedon Jan 4, 2018
Not sure if that solves it, but I just did this to get totalCount back into the a DjangoConnection (without coupling it to an ObjectType):
The only tricky part in this is to call the super() such that
DjangoConnectionFields andConnectionFieldstype()aren't called, in order to get the node's type.rmoorman commentedon Mar 9, 2018
@nuschk thank you for this example. I had a similar case where I wanted to add a "conveniences"
nodesfield to the connection in order to just get all nodes (ie. much like within the swapi where you haveplanetswithin theallPlanetsconnection) and your solution seems to work in my case.Does this extension have other implications you are aware of? Possible side-effects?
Also, it would be great to have this supported within graphene-django out of the box by just declaring a field and a resolve function on a subclass of DjangoConnectionField.
rmoorman commentedon Mar 9, 2018
@nuschk well I ran into a complication with the mentioned approach. I tried to use it for multiple connections and it errors out with
AssertionError: Found different types with the same name in the schema: Connection, Connection.(withingraphene/types/typemap.py", line 85, in graphene_reducer). Did you try it with multiple connections?nuschk commentedon Mar 9, 2018
Well, my brain always melts when I dive into graphene's meta mechanics, so I'm not entirely sure my advice will be sound.
However, the problem is, that the inner
Connectionclass is declared multiple times. I think the only way you can prevent this, is to declare it for each node type separately. An example with a connection to nodes of typeListing:Again, please take this with a grain of salt. I don't completely understand what's going on. :-)
aldarund commentedon Mar 15, 2018
@nuschk wow. Thats a horrible amount of boilerplate code to simply add total field, especially considering that it should be done for each type...
I wonder if there more friendly solution available
nuschk commentedon Mar 19, 2018
@aldarund Heh, well, I agree, although I think at least it's readable. There might be something clever I'm missing.
TheRexx commentedon Jul 13, 2018
I made the code reusable.
IMHO But it is not a beautiful.
greenled commentedon Sep 27, 2018
@TheRexx thanks! It works for me with a small change to avoid class names clashing:
But it works only for those connections declared in the query root, not the auto-generated for each type related list. Any idea of how to cover that case too?
japrogramer commentedon Sep 27, 2018
@greenled what do you mean auto generated connections?
TheRexx commentedon Oct 2, 2018
@greenled You can bring this code to a separate module, and connect to the schema where it is needed. In any case, you prescribe nodes for each model manually and they have different parameters. If I did not understand your question correctly, please add a sample code.
changeling commentedon May 14, 2019
For future reference, see if this solution fits your needs:
#636 (comment)
rando-bando-gando commentedon Sep 21, 2020
Solution so that
totalCountfield is also discoverable by the schema: