Skip to content

Union type for prisma nodes #442

Answered by P4sca1
P4sca1 asked this question in Q&A
Jun 30, 2022 · 2 comments · 3 replies
Discussion options

You must be logged in to vote

I found out how to do it 🥳
You need to add typeBrandKey when returning the nodes, so that Pothos / GraphQL knows the correct type of the object at runtime.

import { typeBrandKey } from '@pothos/core'

const NodeA = builder.prismaNode({ ... })
const NodeB = builder.prismaNode({ ... })

const MyUnion = builder.unionType('MyUnion', { types: [NodeA, NodeB] })

builder.queryFields((t) => ({
  myUnionQuery: t.field({
    type: MyUnion,
    resolve: () => {
      // ...
      return {
        ...prismaNode,
        [typeBrandKey]: 'NodeA' // The name of the prisma model that is returned
      }
    }
})

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@hayes
Comment options

@hayes
Comment options

@P4sca1
Comment options

Answer selected by P4sca1
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