Prisma plugin - t.relation resolve to a single record? #1228
Answered
by
hayes
PatricioNG
asked this question in
Q&A
-
Wondering if there's a way to do a resolver for a relation field? Use case: a table of metadata, I have a field called Currently I am doing this which returns the record as the first index of an array:
Was hoping for something like:
|
Beta Was this translation helpful? Give feedback.
Answered by
hayes
Jul 3, 2024
Replies: 1 comment 2 replies
-
You can use t.field with const MetadataRef = builder.prismaObject('Metadata', { /* ... */ })
// ...
(t) => ({
metafield: t.field({
type: MetadataRef,
nullable: true,
args: {
key: t.arg.string({ required: true }),
},
select: ({ key }, ctx, nestedSelection) => ({
Metadata: nestedSelection({
where: {
key,
}
})
}),
resolve({ Metadata }, args, context, info) {
return Metadata[0] ?? null;
},
}),
}); |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
PatricioNG
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use t.field with
select
for this. Something like this is probably what you are looking for: