Interfaces
Interfaces are an abstract type which may be implemented by object types.
An interface has fields, but itβs never instantiated. Instead, objects may implement interfaces, which makes them a member of that interface. Also, fields may return interface types. When this happens, the returned object may be any member of that interface.
For example, let’s say a Customer
(interface) can either be an Individual
(object) or a Company
(object). Here’s what that might look like in the
GraphQL Schema Definition Language
(SDL):
Notice that the Customer
interface requires the name: String!
field. Both
Company
and Individual
implement that field so that they can satisfy the
Customer
interface.
When querying, you can select the fields on an interface:
Whether the object is a Company
or an Individual
, it doesnβt matter β you
still get their name. If you want some object-specific fields, you can query
them with an
inline fragment , for
example:
Interfaces are a good choice whenever you have a set of objects that are used interchangeably, and they have several significant fields in common. When they donβt have fields in common, use a Union instead.
Defining interfaces
Interfaces are defined using the @strawberry.interface
decorator:
Interface classes should never be instantiated directly.
Implementing interfaces
To define an object type that implements an interface, the type must inherit from the interface:
If you add an object type which implements an interface, but that object type doesnβt appear in your schema as a field return type or a union member, then you will need to add that object to the Schema definition directly.
Interfaces can also implement other interfaces:
Implementing fields
Interfaces can provide field implementations as well. For example:
This resolve method will be called by objects who implement the interface.
Object classes can override the implementation by defining their own name
field:
Resolving an interface
When a fieldβs return type is an interface, GraphQL needs to know what specific
object type to use for the return value. In the example above, each customer
must be categorized as an Individual
or Company
. To do this you need to
always return an instance of an object type from your resolver: