Interfaces
Defining Interface Types
Defining interfaces works exactly like defining Objects, using Interfaces
key in
SchemaTypes object for the builder, and interfaceRef
rather than objectRef
.
In this example we'll use an Animal class and a Giraffe class that extends it:
Again, using classes is completely optional. The only requirement for interfaces is that the the type used for defining objects must be a superset of the the types of any interfaces they implement.
Now that we have our classes set up we can define the interface type. and add a enum definitions for our diet field:
implementing interfaces with object types
There are 2 new properties here: interfaces
and isTypeOf
.
Interfaces is an array of interfaces that the object type implements, and isTypeOf
is a function
that is run whenever we have an object of the interface type and we want to see if it's actually an
instance of our object type.
Using an Interface as a return type
Using interfaces as return types for fields works just like objects:
Querying interface fields
We can query interface fields like diet on any field that returns a giraffe:
or we can query a field that returns an interface and select different fields depending on the concrete type: