Repository navigation
Description
Bug description
I noticed this in my main project so downloaded the prisma example graphql-subscriptions from the github to see if I can reproduce and it appears to be happening their aswell. I'm trying to get the users and include only their posts which have published set to true. I've followed the steps outlined here - https://www.prisma.io/docs/orm/prisma-client/queries/relation-queries#filter-a-list-of-relations: https://www.prisma.io/docs/orm/prisma-client/queries/relation-queries#filter-a-list-of-relations and setup this query:
t.nonNull.list.nonNull.field('allUsers2', {
type: 'User',
resolve: (_parent, _args, context: Context) => {
return context.prisma.user.findMany({
include: {
posts: {
where: {
published: true
}
}
}
});
},
});
The result through postman and appollo explorer includes the posts with published true but weirdly when I console.log what the value should be inside of my server I get the correct response which has the filtered out published false posts.
I tried doing it other methods like this:
t.nonNull.list.nonNull.field('allUsers2', {
type: 'User',
resolve: async (_parent, _args, context: Context) => {
const usersWithPosts = await context.prisma.user.findMany({
include: {
posts: true
}
});
usersWithPosts.forEach(user => {
user.posts = user.posts.filter(post => post.published);
});
console.log( usersWithPosts)
return usersWithPosts;
},
});
But I'm still running into the same issue
How to reproduce
- Download prisma example projects
- Under typescript examples, open and install graphql-subscriptions example
- Add additonal query:
.nonNull.list.nonNull.field('allUsers2', {
type: 'User',
resolve: (_parent, _args, context: Context) => {
return context.prisma.user.findMany({
include: {
posts: {
where: {
published: true
}
}
}
});
},
});
run query through postman or graphql explorer:
query AllUsers2 {
allUsers2 {
id
posts {
id
published
}
name
}
}
posts with published true are returned when they shouldn't be.
Add a console.log for theresults inside the project and notice how the correct values are returned.
Expected behavior
Should be giving filtered results without posts that are published false
Prisma information
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
generator photon {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
published Boolean @default(false)
title String
content String?
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
import {
makeSchema,
mutationType,
nonNull,
objectType,
queryType,
stringArg,
subscriptionType,
intArg,
arg,
inputObjectType,
} from 'nexus'
import { Context } from './context'
const User = objectType({
name: 'User',
definition(t) {
t.nonNull.int('id')
t.string('name')
t.nonNull.string('email')
t.nonNull.list.nonNull.field('posts', {
type: 'Post',
resolve: (parent, _, context: Context) => {
return context.prisma.user
.findUnique({
where: { id: parent.id || undefined },
})
.posts()
},
})
},
})
const Post = objectType({
name: 'Post',
definition(t) {
t.nonNull.int('id')
t.nonNull.string('title')
t.string('content')
t.nonNull.boolean('published')
t.field('author', {
type: 'User',
resolve: (parent, _, context: Context) => {
return context.prisma.post
.findUnique({
where: { id: parent.id || undefined },
})
.author()
},
})
},
})
export const Query = queryType({
definition(t) {
t.nonNull.list.nonNull.field('allUsers', {
type: 'User',
resolve: (_parent, _args, context: Context) => {
return context.prisma.user.findMany()
},
})
t.nonNull.list.nonNull.field('allUsers2', {
type: 'User',
resolve: (_parent, _args, context: Context) => {
return context.prisma.user.findMany({
include: {
posts: {
where: {
published: true
}
}
}
});
},
});
t.nonNull.field('allUsers3', {
type: 'User',
resolve: (_parent, _args, context: Context) => {
return context.prisma.user.findFirst({
include: { posts: {
where: {
published: true,
}
} },
})
},
})
t.nonNull.list.nonNull.field('feed', {
type: 'Post',
args: {
searchString: stringArg(),
skip: intArg(),
take: intArg(),
},
resolve: (_parent, args, context: Context) => {
const or = args.searchString
? {
OR: [
{ title: { contains: args.searchString } },
{ content: { contains: args.searchString } },
],
}
: {}
return context.prisma.post.findMany({
where: {
published: true,
...or,
},
take: args.take || undefined,
skip: args.skip || undefined,
})
},
})
},
})
export const Mutation = mutationType({
definition(t) {
t.field('createDraft', {
type: 'Post',
args: {
data: nonNull(
arg({
type: 'PostCreateInput',
}),
),
authorEmail: stringArg(),
},
resolve: async (_, args, context: Context) => {
const newPost = await context.prisma.post.create({
data: {
title: args.data.title,
content: args.data.content,
author: {
connect: { email: args.authorEmail },
},
},
})
// publish the subscription here
context.pubsub.publish('newPost', newPost)
return newPost
},
})
t.field('togglePublishPost', {
type: 'Post',
args: {
id: nonNull(intArg()),
},
resolve: async (_, args, context: Context) => {
try {
const post = await context.prisma.post.findUnique({
where: { id: args.id || undefined },
})
if (!post.published) {
// publish the subscription here
context.pubsub.publish('postPublished', post)
}
return context.prisma.post.update({
where: { id: args.id || undefined },
data: { published: !post?.published },
})
} catch (e) {
throw new Error(
`Post with ID ${args.id} does not exist in the database.`,
)
}
},
})
},
})
export const Subscription = subscriptionType({
definition(t) {
t.field('newPost', {
type: 'Post',
subscribe(_root, _args, ctx) {
return ctx.pubsub.asyncIterator('newPost')
},
//@ts-ignore
resolve(payload) {
return payload
},
})
t.field('postPublished', {
type: 'Post',
subscribe(_root, _args, ctx) {
return ctx.pubsub.asyncIterator('postPublished')
},
//@ts-ignore
resolve(payload) {
return payload
},
})
},
})
const PostCreateInput = inputObjectType({
name: 'PostCreateInput',
definition(t) {
t.nonNull.string('title')
t.string('content')
},
})
export const schema = makeSchema({
types: [User, Post, Query, Mutation, Subscription, PostCreateInput],
outputs: {
schema: __dirname + '/../schema.graphql',
typegen: __dirname + '/generated/nexus.ts',
},
contextType: {
module: require.resolve('./context'),
export: 'Context',
},
sourceTypes: {
modules: [
{
module: '@prisma/client',
alias: 'prisma',
},
],
},
})Environment & setup
- OS: Windows
- Database: sqlite, but also experienced on postgresql
- Node.js version: Node.js v18.18.2
Prisma Version
5.14.0
Activity