Interfaces
Use the t.interface
type factory to create interface types.
const Dog = t.object({
name: 'Dog',
fields: {
species: t.string,
isLoyal: t.boolean,
},
});
const Cat = t.object({
name: 'Cat',
fields: {
species: t.string,
isDomesticated: t.boolean,
},
});
const Pet = t.interface({
name: 'Pet',
fields: {
species: t.string,
},
/**
* the library makes sure that object types
* passed here correctly implement the interface.
*/
implementors: [Dog, Cat],
resolveType: () => {
/**
* All abstract types need a resolveType function that will
* figure out the type name of a resolved object so that
* GraphQL can understand which Object type this returned
* piece of data falls under.
*/
if (/** some condition */) {
return 'Dog' as const;
} else {
return 'Cat' as const;
}
}
})
/** TypeScript */
type Pet = {
species: string;
};
# GraphQL
interface Pet {
species: String!
}
type Dog implements Pet {
species: String!
isLoyal: Boolean!
}
type Cat implements Pet {
species: String!
isDomesticated: Boolean!
}