I have a type User
. Users can also be a type TeamMember
. The only difference between a User
and TeamMember
is an added field teamRole: String
. So, I’d love to do something like the following to avoid having to redundantly define all the user's fields…
type User {
id: ID!,
name: String,
(many other field defs)
}
type TeamMember extends User {
teamRole: String,
}
Anyone aware of a syntax for this? I thought extend
would be the answer, but it seems more like javascript’s prototype
extend
is great if you have a base schema and want to build two or more usable schemas based on it. You can, for example, define a root Query
type with queries shared by all schemas, and then extend it within each individual schema to add queries specific to that schema. It's meant as a mechanism to add functionality to existing types, rather than creating new types.
GraphQL does not inherently support inheritance. There is a library out there, however, that does add this functionality, if you're using GraphQL-JS. Link here. It adds both generic types and a way for types to inherit from another type.
来源:https://stackoverflow.com/questions/47523384/how-to-inherit-or-extend-typedefs-in-graphql