Like what we’re doing? Star us on GitHub!

Union type fragments

rhythmic.visions
last month
1

Any way to make the generated graphql schema

not

create specific types for re-used objects, or merge them in a union? Would be so nice to be able to actually re-use fragments without each being uniquely typed.



Right now, given a field that is a group:

const groupField: Field = { type: 'group', name; 'repeated', fields: [...]}

using

groupField

in different collections, say

User

and

Post

will yield

User_Group

and

Post_Group

types, and not being collected in a union type, I can't really reuse a fragment for it in my gql-queries.

  • dribbens
    Payload Team
    4 weeks ago

    I can't think of a way you could do this without some changes.



    In what you described, we are doing this for graphQL blocks I believe, but it can't be copied for groups as they aren't re-used in a meaningful way. Suppose the Payload GraphQL schema generation tried to make types for every group it would be a mess of unnecessarily granular types. When names are the same as another group you'd either end up with a union of identical schemas repeated when fields are the same, or we'd need some way of comparing that the field names and types are in fact the same and iterating on the schema name.


    This sounds quite messy to do well. The only way I can think to have it work would be to add a config option for the graphQL name to groups and named tabs which would make it both—


    A) Provide a nice way for devs to name their composable schemas parts only when needed, and


    B) Add intention so devs can either make it work or fall on their own sword if they mess it up with a duplicate name that actually has different fields.



    It is an interesting question. If you think there is a path forward on how this might work, you could create a Github discussion to try and gain traction. If there is a will to build it from the community, that would help, but as it stands this isn't going to be prioritized by our team.

Open the post
Continue the discussion in Discord
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More