Hi all. I’m a huge fan of Prisma—great work! Absol...
# prisma-client
n
Hi all. I’m a huge fan of Prisma—great work! Absolutely love it. I’m using Prisma along with Apollo Client/Server in a Next.js app with a GraphQL API and a Postgres db. I’m using apollo-micro-server to integrate with Next.js and have the Prisma GraphQL Playground running at
localhost:3000/api/graphql
to test my queries before implementing them. I’m using my Prisma schema definitions inside of my Apollo Server typeDefs/resolvers, and am running version
2.21.2
of @prisma/client and version
2.22.2
of apollo-server-micro. I’m currently unable to get the new relation’s count feature working (meaning, the
_count
option doesn’t appear in my GraphQL Playground queries), and have tried virtually every permutation I can think of. When I’m inside of a React component and am updating my gql query, the
_count
option will autocomplete as I type, as if it’s being recognized, but upon saving my changes, the page breaks. It won’t autocomplete when I’m inside my GraphQL playground, however. I have enabled the
selectRelationCount
option in the generator portion of my
schema.prisma
as the docs instruct, and ran
yarn prisma generate
to ensure that my new client data is updated. Here’s a simplified example with a User type that has many Posts: typeDefs
Copy code
type User {
  id: Int!
  email: String!
  posts: [Post]
}

type Post {
  id: Int!
  title: String!
  user: [User]
}
I can’t figure out why the following resolver won’t allow me to access the
_count
data in my GraphQL Playground queries or within a gql query in a React component: resolvers
Copy code
allUsers: () => {
  return prisma.user.findMany({
    include: {
      _count: {
        select: {
          posts: true,
        },
      },
      posts: true,
    },

  });
},
This is my first time integrating Prisma with Apollo/Next.js so please let me know if I’m overlooking something super simple. Thanks!
1
r
did you figure this out Nick?
did you add the count API to the schema?
a
Hi @Nick 👋🏾 You can resolve this by adding
_count
, property to your User type and creating a
Count
type as well. Update your type definitions to resemble this:
Copy code
type {
  id: Int!
  email: String!
  posts: [Post]
  _count: Count
}

type Count {
  posts: Int
}
For this example, prisma is added to the GraphQL context. Your resolver would therefore resemble this:
Copy code
allUsers: (_parent, _args, context: Context) => {
  return context.prisma.user.findMany({
    include: {
      _count: {
        select: {
          posts: true
        }
      },
      posts: true,
    }
  })
}
If you run into any issues, I'd be glad to help 🙂
n
Hi @Robert Witchell & @Alex Ruheni: I hadn’t been able to resolve this on my own yet, but Alex’s suggestion worked (with a slight modification)! My issue was that I wasn’t adding a
_count
property to my User type, and I didn’t have a
Count
type, either. Now it’s working in the GraphQL playground as expected. The only change I had to make to Alex’s resolver code is removing the reference to
context: Context
and the use of
context
in the return line. Not sure why this is (I’m not as familiar with the use of Context). Here’s the resolver code that worked for me:
Copy code
allUsers: (_parent, _args) => {
  return prisma.user.findMany({
    include: {
      _count: {
        select: {
          posts: true
        }
      },
      posts: true,
    }
  })
}
Thanks very much for your speedy replies/help!
🎉 1