What is the best option to return custom object in query or mutation using Prisma plugin? #1308
ds-codemag
started this conversation in
General
Replies: 1 comment 1 reply
-
You can define a builder.prismaObject('User', {
select: {
id: true,
},
fields: (t) => ({
id: t.exposeID('id'),
email: t.exposeString('email'),
password: t.exposeString('password'),
role: t.expose('role', {type: Role}),
refreshToken: t.exposeString('refreshToken', {nullable: true}),
createdAt: t.expose('createdAt', {type: 'DateTime'}),
updatedAt: t.expose('updatedAt', {type: 'DateTime'})
})
}); This will update your type so that only the id field is expected. In your query, you should use t.prismaField, and just rely on builder.queryField('users', (t) =>
t.prismaField({
type: ['User'],
resolve: (query, root, args, ctx, info) => prisma.user.findMany({ ...query }),
})
); The |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I created user model in Prisma schema:
Then I created user object using prismaObject().
User model and object contains password field but in query I want to return users without password.
I used Prisma omitApi.
And I'm getting type error because 'password' is missing
To solve this error I created new object using generated User type from Prisma Client:
And I changed query using not prismaField() but standard field() method:
Everything works fine.
But my question is:
Is this the only solution or maybe there is a better solution where i can still use prismaObject and prismaField?
Beta Was this translation helpful? Give feedback.
All reactions