I'am new to GraphQL but I really like it. Now that I'am playing with interfaces and unions, I'am facing a problem with mutations.
Suppose that I have this schema :
interface FoodType {
id: String
type: String
composition: [Ingredient]
}
type Pizza implements FoodType {
id: String
type: String
pizzaType: String
toppings: [String]
size: String
composition: [Ingredient]
}
type Salad implements FoodType {
id: String
type: String
vegetarian: Boolean
dressing: Boolean
composition: [Ingredient]
}
type BasicFood implements FoodType {
id: String
type: String
composition: [Ingredient]
}
type Ingredient {
name: String
qty: Float
units: String
}
Now, I'd like to create new food items, so I started doing something like this :
type Mutation {
addPizza(input:Pizza):FoodType
addSalad(input:Salad):FoodType
addBasic(input:BasicFood):FoodType
}
This did not work for 2 reasons :
- If I want to pass an object as parameter, this one must be an "input" type. But "Pizza", "Salad" and "BasicFood" are just "type".
- An input type cannot implement an interface.
So, I need to modify my previous schema like this :
interface FoodType {
id: String
type: String
composition: [Ingredient]
}
type Pizza implements FoodType {
id: String
type: String
pizzaType: String
toppings: [String]
size: String
composition: [Ingredient]
}
type Salad implements FoodType {
id: String
type: String
vegetarian: Boolean
dressing: Boolean
composition: [Ingredient]
}
type BasicFood implements FoodType {
id: String
type: String
composition: [Ingredient]
}
type Ingredient {
name: String
qty: Float
units: String
}
type Mutation {
addPizza(input: PizzaInput): FoodType
addSalad(input: SaladInput): FoodType
addBasic(input: BasicInput): FoodType
}
input PizzaInput {
type: String
pizzaType: String
toppings: [String]
size: String
composition: [IngredientInput]
}
input SaladInput {
type: String
vegetarian: Boolean
dressing: Boolean
composition: [IngredientInput]
}
input BasicFoodInput {
type: String
composition: [IngredientInput]
}
input IngredientInput {
name: String
qty: Float
units: String
}
So, here I defined my 3 creation methods for Pizza, Salad and Basic food. I need to define 3 input types (one for each food) And I also need to define a new input type for Ingredients.
It makes lot of duplication. Are you ok with that? Or there is a better way to deal with this?
Thank you