For example, a Pet
is an Animal
with an owner
and name
.
type Animal {
species: String
}
type Pet extends A
Starting with the June2018 stable version of the GraphQL spec, an Object type can extend another Object type:
Object type extensions are used to represent a type which has been extended from some original type. For example, this might be used to represent local data
In your example,
type Animal {
species: String
}
extend type Animal {
owner: Owner
name: String
}
This isn't inheritance per se; you can only extend the base type, not create new types based on it. Note there is no name for the new type; the existing Animal
type is extended.
The graphql.org documentation doesn't mention anything about extend
, but the documentation is admittedly lackluster and being transitioned from Facebook's ownership to the Linux Foundation.
The JavaScript reference implementation doesn't fully support extensions, but since you've tagged your question apollo-server, you can use graphql-tools, which does:
const { graphql } = require('graphql');
const { makeExecutableSchema } = require('graphql-tools');
const typeDefs = `
type Person {
name: String!
}
extend type Person {
salary: Int
}
type Query {
person: Person
}
`;
const resolvers = {
Query: {
person: () => ({ name: "John Doe", salary: 1234 })
}
}
const schema = makeExecutableSchema({ typeDefs, resolvers });
graphql(schema, '{ person {name salary} }').then((response) => {
console.log(response);
});
For actual type inheritance, see the graphql-s2s library.