1

I have an Invoice model like:

type Invoice {
  id
  amount
}

I have 350'000 invoices. How can I sum amount for all invoices. (Max limitation is 1000)

This hack cannot work: https://www.prisma.io/forum/t/query-all-size-limit/557, as I have too many rows.

Related issues: https://github.com/prisma/prisma/issues/2162 https://github.com/prisma/prisma/issues/259 https://www.prisma.io/forum/t/query-all-size-limit/557 https://www.prisma.io/forum/t/sun-amount-more-than-350000-rows/7611

Alan
  • 9,167
  • 4
  • 52
  • 70
  • Something like this sounds like it would best be calculated and served on the GraphQL server; Does the `Invoice` have a `pagination` implementation? You can instead make multiple queries fetching 1,000 at a time if you insist on querying it. – Magnum Aug 01 '19 at 17:39
  • Thanks @Jim "it would best be calculated and served on the GraphQL server" ==> Im doing this query on the GraphQL server.. inside a resolver. I will return to the frontend only the result – Alan Aug 01 '19 at 23:18

1 Answers1

2

You can build a custom resolver in Prisma with a custom SQL query that will SUM it for you;

An example implementation might be something like: :

const client = new Client({
  database: "prisma",
  user: "...",
  password: "...",
  host: "localhost",
  port: 3000
})

const resolvers = {
  Query: {
    async sumInvoices(parent, args, context, info){
      const response = await client.query(
        `SELECT SUM(amount) FROM Invoice WHERE YOUR_CONDITION`
      );
      return response;
    };
};

You can check out Ben Awad's video on it too for additional examples: https://www.youtube.com/watch?time_continue=12&v=YUjlBuI8xsU

Magnum
  • 2,299
  • 4
  • 17
  • 23