question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to extend connection info?

See original GitHub issue

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:

{
  "data": {
    "my_function": {
      "custom_object": {
         "field1": 101,
         "field2": 66,
      },
      "pageInfo": {
        "endCursor": "foo"
      },
      "edges": [
        {
          "node": {
            "id": "bar",
          }
        },
      ...      
     ]
    }
  }
}

Python 2.7 graphene 2.0 Django 1.10

Thank you!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
TheRexxcommented, Jul 13, 2018

I made the code reusable.

class CountingNodeConnectionField(DjangoFilterConnectionField):
    def __init__(self, type, fields=None, order_by=None,
                 extra_filter_meta=None, filterset_class=None,
                 *args, **kwargs):
        self._type = type
        self._fields = fields
        self._provided_filterset_class = filterset_class
        self._filterset_class = None
        self._extra_filter_meta = extra_filter_meta
        self._base_args = None
        super(DjangoFilterConnectionField, self).__init__(
            type,
            *args,
            **kwargs
        )

    @property
    def type(self):

        class NodeConnection(graphene.Connection):
            total_count = graphene.Int()

            class Meta:
                node = self._type
                name = '{}Connection'.format(self._type._meta.name)

            def resolve_total_count(self, info, **kwargs):
                return self.iterable.count()

        return NodeConnection

IMHO But it is not a beautiful.

5reactions
nuschkcommented, 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):

class CountingConnectionField(graphene_django.DjangoConnectionField):
    @property
    def type(self):
        node_type = super(graphene.relay.ConnectionField, self).type
        assert issubclass(node_type, graphene_django.DjangoObjectType), "DjangoConnectionField only accepts DjangoObjectType types"

        class Connection(graphene.Connection):
            total_count = graphene.Int()

            class Meta:
                name = node_type._meta.name + 'Connection'
                node = node_type

            def resolve_total_count(self, info, **kwargs):
                return self.iterable.count()

        return Connection

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Extend a Home Network
Here we look at various methods of extending a Home Network. ... Connect with Wi-FI using a Wi-Fi range extender or repeater.
Read more >
Complete Guide to Extending Your Wi-Fi Network
There are many ways to extend a WiFi network: extenders, powerline, ethernet access points, and even mesh networking systems.
Read more >
6 Ways To Extend Your Wi-Fi Range - Tech Advisor
If your Wi-Fi network doesn't reach the places you want it to, here are six ways to extend Wi-Fi coverage and get a...
Read more >
How To Extend Your WIFI Signal at Home! (info & setup)
How To Extend Your WIFI Signal, we look at two different wifi range extenders from TP - Link. information for both wifi extenders...
Read more >
How to Extend WiFi Range to Resolve a Weak Signal - NetSpot
1. Simply finding a new location for your wireless router can have a dramatic impact on your WiFi signal for a variety of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found