Nick
04/16/2021, 5:34 PMlocalhost: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
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
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!Robert Witchell
04/20/2021, 8:24 AMRobert Witchell
04/20/2021, 8:25 AMAlex Ruheni
_count
, property to your User type and creating a Count
type as well.
Update your type definitions to resemble this:
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:
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 🙂Nick
04/20/2021, 2:19 PM_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:
allUsers: (_parent, _args) => {
return prisma.user.findMany({
include: {
_count: {
select: {
posts: true
}
},
posts: true,
}
})
}
Thanks very much for your speedy replies/help!