Access Connection info (e.g. count) for type in a resolver?


#1

Say I have a Post type and therefore I have a generated PostConnection type.

In my “posts” resolver I want to not only return post data (title, content etc) but also some meta information e.g. The total number of posts.

I have tried the following in my resolver:

let connection = await ctx.db.query.postsConnection(
  { },
  info,
)
let count = connection.aggregate.count

But I get the error:

Field “postsConnection” of type “PostConnection!” must have a selection of subfields. Did you mean “postsConnection { … }”?

This makes sense logically e.g. when querying PostConnection I need to return the aggregate or postInfo etc.

Any idea how to get this information or why this does not work?

Thanks.


#2

Have a look at the Prisma docs Queries: Connection queries


#3

Thanks @meep.

I’m familiar with how to query the connection types via GraphQL query syntax directly as shown in the docs but am trying to use the generated Prisma schema API with Typescript. Sorry I didn’t make that clear.

Here are some connection queries in the graphcool/graphql-server-example:


#4

What are you passing in as info?

Is it similar to?

query {
  postsConnection(where: {
    title_contains: "GraphQL"
  }) {
    aggregate {
      count
    }
  }
}

#5

Agreed with @meep, the following should work:

let connection = await ctx.db.query.postsConnection(
  { },
  `{ aggregate { count } } }`,
)
let count = connection.aggregate.count

The way you wrote it, you make the info object depend on the query coming in from the client. Only if that includes the aggregate { count } part is your code valid.

Check out this blog article, I’ve found it super useful in understanding the info object and all its intricacies :slightly_smiling_face: