Anyone? Count? Anyone? How do you get the count si...
# orm-help
j
Anyone? Count? Anyone? How do you get the count since you can't get around the stupid limit of 1000 nodes. So stupid
m
Hi! Have you tried using the autogenerated`Connection` type for whatever type you want to count?
j
Yeah doesn't work
m
If you want to post some code / share a repo, I'd be happy to take a look. 🙂
j
I can't figure out the resolver
residentials: (_, args, context, info) => { return context.prisma.query.residentials({ orderBy: args.orderBy, skip: args.skip, first: args.first }, info ) },
So this works.. But I need to return the count. I wanted to do the pagination client side and catch it. I am doing the traditional pagination because I am doing a real estate site, having all the property listings loaded on one page would be too much for the user to keep track of.
type Query { residentials(orderBy: ResidentialOrderByInput, skip: Int, first: Int): [Residential]! residential(SystemID: Int): Residential }
So residentialConnection, I can't seem to find the method to return the count. Tried AggregateReisdential as well
m
Ok.
So, using a resolver shaped like this:
Copy code
numberOfUsers: async (parent, args, ctx: Context, info) => {
    return ctx.db.query.usersConnection({}, info)
  },
If I send the following query:
Copy code
{
  numberOfUsers {
    aggregate {
      count
    }
  }
}
I get the user count returned.
Obv, you would need to change the
users
part and define the query in your local schema.graphql file.
Copy code
type Query = {
  ... 
  numberOfUsers: UserConnection!
  ...
}
j
Error: Field count: Couldn't find type residentialsConnection in any of the schemas.
# import * from './generated/prisma.graphql' type Query { residentials(orderBy: ResidentialOrderByInput, skip: Int, first: Int): [Residential]! residential(SystemID: Int): Residential count: residentialsConnection! }
count: (_, args, context, info) => { return context.prisma.query.residentialsConnection({}, info); },
m
Can you change the return type of count to
ResidentialConnection
?
That is, in your schema.graphql file, not in your resolver.
j
"context.prisma.query.residentialConnection is not a function"
m
Right. Leave it as
residentialsConnection
in your resolver (i.e.,
ctd.db.query.residentialsConnection(..)
) but change it to
ResidentialConnection
in schema.graphql
Copy code
type Query {
 residentials(orderBy: ResidentialOrderByInput, skip: Int, first: Int): [Residential]!
 residential(SystemID: Int): Residential
 count: ResidentialConnection!
}
j
Did that
m
And still nothing?
j
get this... "context.prisma.query.residentialConnection is not a function",
query{ count{ aggregate{ count } } residentials( orderBy: ListingPrice_ASC skip: 25 first: 5 ) { SystemID ListingPrice } }
Query: { count: (_, args, context, info) => { return context.prisma.query.residentialConnection({}, info); }, residentials: (_, args, context, info) => { return context.prisma.query.residentials({ orderBy: args.orderBy, skip: args.skip, first: args.first }, info ) },
m
Right, so in
count
above (which I might rename just to ensure no naming conflicts occur), you need to amend the resolver to return
context.prisma.query.residentialsConnection({}, info)
It's singular in 'schema.graphql` and plural in the resolver.
It should probably be changed to be uniformly one way or the other and I think it's worth pushing this up the chain as a potential bug fix.
j
bingo => It's singular in 'schema.graphql` and plural in the resolver.
m
I'm going to open an issue right now. I'm glad we were able to figure it out.
j
I tried the method you explain before I asked for help. Didn't think of plural and singlar
m
Honestly, it's counterintuitive to think that it would be one way here and another way there when so related.
So, I didn't think of it either until I got an error trying to reproduce it on my end.
j
Haha
So would this be right?
query residentials($skip: Int, $first: Int){ residentials( orderBy: ListingPrice_ASC, skip: $skip, first: $first ) { SystemID Address City State Zip ListingPrice Bedrooms Bathrooms Class PropertyType MarketingRemarks Photos { MediaUrl } } count{ aggreate{ count } } }
nevermind works
Thanks for help
m
No prob! Anytime.
j
Come to think of it, aggregate count returns all the docs regardless of filters correct?
m
You can provide arguments to the
residentialsConnection
query.
In fact, you should be able to provide exactly the same
ResidentialWhereInput
argument.
Also, here is your issue in case you want to add anything: https://github.com/prismagraphql/prisma-binding/issues/183
n
Thanks for figuring this out!
👍 1