Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you extend types in GraphQL?

Tags:

types

graphql

For example, a Pet is an Animal with an owner and name.

type Animal {   species: String }  type Pet extends Animal {   owner: Owner   name: String } 
like image 844
atkayla Avatar asked Feb 22 '18 00:02

atkayla


People also ask

What is GraphQL extension?

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.

How do you update mutations in GraphQL?

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.

What are the three types of operations in GraphQL?

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.


2 Answers

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.

like image 61
Dan Dascalescu Avatar answered Nov 13 '22 00:11

Dan Dascalescu


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     }   } 
like image 29
dpix Avatar answered Nov 13 '22 02:11

dpix