Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Graphql Union using generated types

default discord avatar
emmanuellast year
8

Do you have a simple example of how to create a Graphql union with the types generated by Payload?

  • default discord avatar
    noheadphoneslast year

    What are you trying to get exactly? It might be easier to recommend something with the full picture

  • default discord avatar
    emmanuellast year

    So we have 3 schemas (collections), blogs, podcasts and videos, all of them shared common fields, so I want to create a union type/schema (e.g. type Post = Blog | Podcast | Video) so it can be exposed in the graphql api url and consumed by client.

  • default discord avatar
    noheadphoneslast year

    You would have to make a new custom graphql endpoint



    and construct a new type for yourself using the graphQL object passed via the handler with

    https://graphql.org/graphql-js/type/#graphqluniontype


    something akin to


    new GraphQL.GraphQLUnionType()


    for the types you can get the generated ones from your

    payload.collections['blog'].graphQL.types

    instance to use as types here


    types: [DogType, CatType],


    I wrote a guide here on how to extend the graphql API

    https://nouance.io/articles/how-to-extend-graphql-in-payload-with-custom-queries-mutations-and-re-usable-types


    Here's the guide in payload docs

    https://payloadcms.com/docs/graphql/extending
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.