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.

adding count to query

See original GitHub issue

I’m using the following code and would like to to have variable “count” (number of total results) in my response – but I have no idea on how to solve it (already did quite some research).

class EntryNode(DjangoNode):
    class Meta:
        model = Entry


class Query(ObjectType):
    all_entries = DjangoFilterConnectionField(EntryNode)

    class Meta:
        abstract = True

Query

{allEntries(first:5) {
    pageInfo{hasPreviousPage, hasNextPage},
    edges{node{id,user{id,username},title,category{id,name}}}
}}

Results I’d like to have an additional variable “count” (either within “allEntries” or even better as a part of “pageInfo”)

{
    "data": {
        "allEntries": {
            "pageInfo": {
                "hasPreviousPage": false,
                "hasNextPage": false,
                "count": ???,
            },
            "count": ???,
            "edges": [
                {
                    "node": {
                        "id": "RW50cnlOb2RlOjUy",
                        ...
                    }
                }
            ]
        }
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
syrusakbarycommented, Apr 29, 2016

Hi @sehmaschine, this is how I approached the same issue in swapi-graphene.

Example swapi-graphene totalCount query.

{
  myFavoriteFilm: film(id:"RmlsbToz") {
    id
    title
    episodeId
    characters(first:5) {
      totalCount
      edges {
        node {
          name
        }
      }
    }
  }
}

Implementation: https://github.com/graphql-python/swapi-graphene/blob/master/starwars/schema.py#L18-L22

And then use connection_type in your EntryNode:

class EntryNode(DjangoNode):
    class Meta:
        model = Entry

    connection_type = Connection

Hope it helps!

[Reopen this issue if you run into some errors or have more questions in the process].

2reactions
Vitiell0commented, Jul 11, 2017

I arrived here by Google first but ultimately found the total_count implementation at this thread easier to follow and use: https://github.com/graphql-python/graphene-django/issues/162

from graphene import Int

class ProductNode(DjangoObjectType):
    class Meta:
        model = Product
        filter_fields = {
            'name': ['exact', 'icontains', 'istartswith'],
        }
        interfaces = (relay.Node, )

    @classmethod
    def get_connection(cls):
        class CountableConnection(relay.Connection):
            total_count = Int()

            class Meta:
                name = '{}Connection'.format(cls._meta.name)
                node = cls
            
            @staticmethod  # Redundant since Graphene kinda does this automatically for all resolve_ methods.
            def resolve_total_count(root, args, context, info):
                return root.length

        return CountableConnection
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to insert a count column into a sql query - Stack Overflow
SELECT COUNT (DISTINCT column_name) FROM table_name;. COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.
Read more >
Count data by using a query - Microsoft Support
Add a Total row · Open your query in Datasheet view. To do so for a database in the . · On the...
Read more >
SQL: COUNT Function - TechOnTheNet
The SQL COUNT function is used to count the number of rows returned in a SELECT statement. Syntax. The syntax for the COUNT...
Read more >
SQL COUNT() with GROUP by - w3resource
The use of COUNT() function in conjunction with GROUP BY is useful for characterizing our data under various groupings. A combination of same ......
Read more >
SQL Count – How to Select, Sum, and Average Rows in SQL
If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the...
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