Polymorphic result fields
A search or feed can return several concrete resource types that share fields such as an identifier and display name.
GraphQL · Class
A GraphQL interface is an abstract type that declares fields shared by concrete object types or other interfaces. It lets a field return different implementing types while clients query their common fields.
An interface defines shared fields for multiple concrete object types. A field returning an interface can produce any implementing object, while clients can request the shared fields and use inline fragments for type-specific fields.
interface Character {
id: ID!
name: String!
}interface Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
}
type Human implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
homePlanet: String
}This schema lets a field return different concrete character types while clients request the shared Character fields.
A search or feed can return several concrete resource types that share fields such as an identifier and display name.
Several domain objects expose the same required behavior while retaining their own additional fields.
A client needs common fields from multiple implementations and optional type-specific fields through fragments.
An interface is an abstract type that declares fields shared by multiple concrete object types. Each object that implements it must include those fields, so a field returning the interface can return different implementing objects.
Follow-up: How can a client request fields that exist only on one implementation? Use an inline fragment conditioned on that concrete type.
Interfaces are useful when one field should return objects of several types while exposing a common selection set. The schema guarantees that implementing object types provide the interface's declared fields.
Follow-up: What should a client do when an interface gains a new implementation? Query __typename and handle unknown runtime types without crashing.
Clients can query fields declared by the interface directly. For implementation-specific fields, they use inline fragments for the relevant concrete object type.
Follow-up: How does a client discover the available implementations? Introspection can reveal which object types implement an interface.
Object typesUnion typesInline fragmentsIntrospection__typename