For example, a Pet
is an Animal
with an owner
and name
.
type Animal { species: String } type Pet extends Animal { owner: Owner name: String }
The graphql-js library allows for putting arbitrary data into GraphQL types config inside the extensions property. Annotating schema types or fields with a custom metadata, that can be then used at runtime by middlewares or resolvers, is a really powerful and useful feature.
Update mutations take filter as an input to select specific objects. You can specify set and remove operations on fields belonging to the filtered objects. It returns the state of the objects after updating. Note Executing an empty remove {} or an empty set{} doesn't have any effect on the update mutation.
There are three types of operations that GraphQL models: query – a read‐only fetch. mutation – a write followed by a fetch. subscription – a long‐lived request that fetches data in response to source events.
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.
Although you cannot create sub-classes / sub-types you can do a form of inheritance using interfaces: https://graphql.org/learn/schema/#interfaces
Example from the link above:
interface Character { id: ID! name: String! friends: [Character] appearsIn: [Episode]! }
type Human implements Character { id: ID! name: String! friends: [Character] appearsIn: [Episode]! starships: [Starship] totalCredits: Int } type Droid implements Character { id: ID! name: String! friends: [Character] appearsIn: [Episode]! primaryFunction: String }
When querying you can specify specific fields for different implementations
hero(episode: $ep) { name ... on Droid { primaryFunction } ... on Human { totalCredits } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With