Interfaces
A GraphQL interface defines a set of fields that multiple object types share. When a field returns an interface type, the client can query the shared fields directly and use fragments to access type-specific fields. Interfaces are output-only types and cannot be used as arguments or input fields.
GraphQL schema
interface Message {
author: User!
createdAt: DateTime!
}
type TextMessage implements Message {
author: User!
createdAt: DateTime!
content: String!
}
type Query {
messages: [Message]!
}Client query
{
messages {
createdAt
... on TextMessage {
content
}
}
}The shared createdAt field is queried directly on the interface. The content field, which exists only on TextMessage, is accessed through an inline fragment.
Defining an Interface Type#
Hot Chocolate maps C# interfaces and abstract classes to GraphQL interface types.
Ignoring Fields#
You can exclude specific fields from the GraphQL interface.
Overriding Names#
Use [GraphQLName] or the Name method to override inferred names.
Both produce the following schema:
interface Post {
author: User!
addedAt: DateTime!
}Interfaces Implementing Interfaces#
GraphQL interfaces can implement other interfaces, forming a hierarchy.
Register intermediate interfaces (like DatedMessage) explicitly if they are not returned directly from a resolver field.
Default Resolvers#
Interface fields can define default resolvers, similar to default interface methods in C#. When an object type implements the interface, it automatically inherits the resolver for any field where it does not define its own. If the object type does not even declare the field, the field is created automatically with the interface's resolver.
This is useful when multiple types share the same resolution logic for a field and you want to define it once on the interface rather than repeating it in every implementing type.
Object types can override an inherited resolver by defining their own resolver for the same field.
Next Steps#
- Need types without shared fields? See Unions.
- Need to define output types? See Object Types.
- Need to extend an existing type? See Extending Types.
- Need to document interface fields? See Documentation.