ferry icon indicating copy to clipboard operation
ferry copied to clipboard

Question: How to make a fragment with a root union

Open dav3rin opened this issue 3 years ago • 1 comments

I have a union type Food = Beverage | Fruit | Vegetable

fragment FoodFrag on Food {
    ... on Beverage {
        ...FoodCommon
        flavour
    }
    ... on Vegetable {
        ...FoodCommon
        vitamins
    }
    ... on Fruit {
        ...FoodCommon
        sweetness
    }
}

When I try use FoodFrag in other queries/mutations like this...

# import '../fragments/food_fragments.graphql'

mutation CreateFood ($food_in: FoodInput!){
    createFood(food_in: $food_in) {
        ...FoodFrag
    }
}

...I get a stack overflow error from the build runner.

Am I doing something wrong? Is this a bug with ferry ? Is this a limitation of graphql? Workaround ideas?

dav3rin avatar Sep 23 '22 10:09 dav3rin

How is FoodCommon defined? I don't think you can use a fragment on a union this way.

If you have shared fields on all food types, the way to go is to make an interface that defines the shared fields and the concrete types which add the specialized fields. See https://graphql.org/learn/schema/#interfaces

knaeckeKami avatar Sep 23 '22 13:09 knaeckeKami