G
G
Gariks2019-01-24 16:31:47
typescript
Gariks, 2019-01-24 16:31:47

Prisma.io - how to get the number of records in a model?

There is a GraphQL server written in TypeScript using this example (graphQL-yoga, prisma).
There is schema.graphql

type Game {
  id: ID!
  name: String!
  excerpt: String
}

type Query {
    games: [Game!]!
}

How to get the number of records in the database of the Game model?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Lanets, 2019-12-23
@Fi1osof

Late answer, but still.
When you deploy a schema, three types of query are formed for each type (we are talking about Query):
object - a unique object
objects - a list of objects
objectsConnection - a list of objects with quantity data, etc.
In your case it will be

query {
  gamesConnection{
    aggregate{
      count
    }
    edges{
      node{
        id
      }
    }
  }
}

The answer will be like:
{
  "data": {
    "gamesConnection": {
      "aggregate": {
        "count": 0
      },
      "edges": []
    }
  }
}

Accordingly, it is not necessary to specify edges if you only need to get the number of records.
An important point: in earlier versions of prism (like 1.15), aggregate returned the total number of objects found. That is, for example, if you write a query like this:
query {
  gamesConnection (
    where: {
      name_contains: "Test"
    }
    first: 2
  ){
    aggregate{
      count
    }
    edges{
      node{
        id
      }
    }
  }
}

You might get a response like
{
  "data": {
    "gamesConnection": {
      "aggregate": {
        "count": 15
      },
      "edges": [{id:"dsfdsf"}, {"id":"sdfsdgds}]
    }
  }
}

That is, 2 records are received (because first: 2 is specified), but a total of 15 records are found containing "Test" in the title.
So, in later versions (and now), aggregate returns exactly the number of records received, that is, in our case it will return not 15, but 2 (because it received 2 records), even if there are only 15 records. I think that such a change is absolutely pointless (because on the client side I myself can count how many objects I received), and even sent them a ticket, but they said it was a feature, not a bug.
For this reason, you need to write queries like this:
query games (
  $where: GameWhereInput
  $first: Int
){
  gamesConnection (
    where: $where
  ){
    aggregate{
      count
    }
  }
  
  games (
    where: $where
    first: $first
  ){
    id
    name
  }
}

That is, there are two requests at once with the transfer of a condition and a limit as parameters.
Only the condition is passed to the first (count). And in the second (directly the list of objects) there is already a condition and a limit (and other parameters, if necessary). The answer will be like
{
  "data": {
    "gamesConnection": {
      "aggregate": {
        "count": 0
      }
    },
    "games": []
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question