ferry
ferry copied to clipboard
Question: How to make a fragment with a root union
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?
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