my question is more about general GraphQL schema definition conventions to build a well designed schema rather than a question about the difference between union/interface.
Let’s say I have the following type definitions in GQL :
interface Animal {
move: String
}
type Dog implements Animal {
move: String
height: Int
}
type Cat implements Animal {
move: String
jumpHeight: Int
}
union availableAnimals = Dog | Cat
type Spot {
animal: availableAnimals
}
Does it make sense that I created an union and put it as type in Spot ? I could have simply put the interface Animal instead. However, as I know this API will likely change a lot and don’t want to introduce breaking changes, I like that I can keep making the schema evolve without exposing the new Animals in the Spot yet(even if it’s not what I intend to do).
Also by having an union instead of an interface, I force the client to resolve a specific animal as I think it makes no sense in my schema to only resolve the common field between animals, is it a valid concern or is it more standard to let the client completely decides what he wants to do ?
For example, the client may not fully know GQL and always request the move field for each Animal (if the type was an interface) which causes a lot of internal DB/ 3rd party API queries but if he had to purposely specify it for each Animal, he would think more and only request that field for the only Animal he needs. Of course this does not protect against any kind of purposeful abuse but it still better than nothing?