How to create a custom mutation Output with Prisma fields #1032
Answered
by
hayes
baconcheese113
asked this question in
Q&A
-
I'd like to create a mutation Output type composed of a prismaField and a string, something like: builder.mutationFields((t) => ({
getUserAndToken: t.object({
type: (t) => ({
user: t.prismaField({ type: 'User' }),
token: t.string(),
}),
args: {
email: t.arg.string({ required: true }),
password: t.arg.string({ required: true }),
},
resolve: async (_root, args, { prisma, user }) => {
return { user, token: 'myToken' };
},
}),
}); This type would only be used for this mutation return, is this possible? |
Beta Was this translation helpful? Give feedback.
Answered by
hayes
Sep 19, 2023
Replies: 2 comments 1 reply
-
I'd do something like this: const User = builder.prismaObject('User', { ... });
const UserAndToken = builder.objectRef<{ token: string, user: User }>('UserAndToken').implement({
fields: t => ({
user: t.field({ type: User, resolve: (parent) => parent.user }),
});
builder.mutationFields((t) => ({
getUserAndToken: t.field({
type: UserAndToken,
args: {
email: t.arg.string({ required: true }),
password: t.arg.string({ required: true }),
},
resolve: async (_root, args, context, info) => {
// get a query object that includes selections nested inside UserAndToken.user
const query = queryFromInfo({
context,
info,
path: ['user'],
});
const user = await context.prisma.user.findUniqueOrThrow({ ...query, where: { /* your lookup here */ } });
return { user, token: 'myToken' };
},
}),
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
baconcheese113
-
Thanks @hayes, that worked great! Can you verify that the following is true? It seems the generic parameter should reference your Prisma model type being returned, but the import { User as UserModel } from '@prisma/client';
import { User } from '../graphTypes/userGraphType';
const UserAndToken = builder
.objectRef<{ user: UserModel; token: string }>('UserAndToken')
.implement({
fields: (t) => ({
user: t.field({ type: User, resolve: (root) => root.user }),
}),
});
```ts |
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'd do something like this: