Skip to content

Prisma plugin - t.relation resolve to a single record? #1228

Answered by hayes
PatricioNG asked this question in Q&A
Discussion options

You must be logged in to vote

You can use t.field with select for this. Something like this is probably what you are looking for:

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;
    },
  }),
});

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@PatricioNG
Comment options

@hayes
Comment options

Answer selected by PatricioNG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants