Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Nodes shorthand and totalCount in DjangoFilterConnectionField #1322

Answered by everdrone
everdrone asked this question in Q&A
Discussion options

Is there a way to get direct access to a nodes list alongside with totalCount inside DjangoFilterConnectionField?

I'm going after this schema structure (from the GitHub GraphQL API)

type LabelConnection {
 edges: [LabelEdge]
 nodes: [Label] # <-- nodes shorthand
 pageInfo: PageInfo!
 totalCount: Int!
}

I tried subclassing both ConnectionField and DjangoFilterConnectionField but without luck

You must be logged in to vote

Found a very hacky workaround for anyone interested, this patch works for

  • graphene==2.1.9
  • graphql-relay==2.0.1
  • graphene-django==2.15.0

You'll need to patch the packages after install.

--- site-packages/graphql_relay/connection/connectiontypes.py	2022年05月21日 14:17:29.000000000 +0200
+++ site-packages/graphql_relay/connection/connectiontypes.py	2022年05月21日 14:17:17.000000000 +0200
@@ -1,13 +1,17 @@
 class Connection(object):
- def __init__(self, edges, page_info):
+ def __init__(self, edges, page_info, nodes, total_count):
 self.edges = edges
 self.page_info = page_info
+ self.nodes = nodes
+ self.total_count = total_count
 def to_dict(self):
 ...

Replies: 2 comments 2 replies

Comment options

Found a very hacky workaround for anyone interested, this patch works for

  • graphene==2.1.9
  • graphql-relay==2.0.1
  • graphene-django==2.15.0

You'll need to patch the packages after install.

--- site-packages/graphql_relay/connection/connectiontypes.py	2022年05月21日 14:17:29.000000000 +0200
+++ site-packages/graphql_relay/connection/connectiontypes.py	2022年05月21日 14:17:17.000000000 +0200
@@ -1,13 +1,17 @@
 class Connection(object):
- def __init__(self, edges, page_info):
+ def __init__(self, edges, page_info, nodes, total_count):
 self.edges = edges
 self.page_info = page_info
+ self.nodes = nodes
+ self.total_count = total_count
 def to_dict(self):
 return {
 'edges': [e.to_dict() for e in self.edges],
 'pageInfo': self.page_info.to_dict(),
+ 'nodes': self.nodes,
+ 'totalCount': len(self.nodes)
 }
--- site-packages/graphql_relay/connection/arrayconnection.py	2022年05月21日 14:06:15.000000000 +0200
+++ site-packages/graphql_relay/connection/arrayconnection.py	2022年05月21日 14:04:43.000000000 +0200
@@ -95,6 +95,8 @@
 return connection_type(
 edges=edges,
+ nodes=_slice,
+ total_count=list_length,
 page_info=pageinfo_type(
 start_cursor=first_edge_cursor,
 end_cursor=last_edge_cursor,
--- site-packages/graphene/relay/connection.py	2022年05月21日 14:06:29.000000000 +0200
+++ site-packages/graphene/relay/connection.py	2022年05月21日 14:11:51.000000000 +0200
@@ -91,6 +91,7 @@
 cls.Edge = edge
 options["name"] = name
+ options["description"] = "The connection type for {}.".format(base_name)
 _meta.node = node
 _meta.fields = OrderedDict(
 [
@@ -106,10 +107,26 @@
 (
 "edges",
 Field(
- NonNull(List(edge)),
+ # NonNull(List(edge)),
+ NonNull(List(NonNull(edge))),
 description="Contains the nodes in this connection.",
 ),
 ),
+ (
+ "nodes",
+ Field(
+ # NonNull(List(_node)),
+ NonNull(List(NonNull(_node))),
+ description="A list of nodes.",
+ ),
+ ),
+ (
+ "total_count",
+ Field(
+ NonNull(Int),
+ description="Identifies the total count of items in the connection.",
+ ),
+ ),
 ]
 )
 return super(Connection, cls).__init_subclass_with_meta__(
You must be logged in to vote
0 replies
Answer selected by everdrone
Comment options

Ma diventerà mai una feature ufficiale? Non riusciamo ad ottenerla estendendo qualcosa?

You must be logged in to vote
2 replies
Comment options

From the looks of it, doesn't looks like it's ever going to make it as a feature.

I've been using strawberrygraphql before moving away from python, it seemed very promising

Comment options

Io stavo guardando anche questo per restare in
django https://pypi.org/project/graphene-django-extras/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /