How to Inherit or Extend typeDefs in GraphQL

18,724

Solution 1

The extend keyword is great if you have a base schema and want to build two or more usable schemas based on it. You can, for example, define a root Query type with queries shared by all schemas, and then extend it within each individual schema to add queries specific to that schema. It can also be used to modularize a schema. However, it's only a mechanism to add functionality to existing types -- it can't be used to create new types.

GraphQL does not inherently support inheritance. There is no syntax that would help you avoid duplication of fields across multiple types.

You can utilize string interpolation to avoid typing out the same fields again and again:

const sharedFields = `
  foo: String
  bar: String
`
const typeDefs = `
  type A {
    ${sharedFields}
  }

  type B {
    ${sharedFields}
  }
`

Barring that, you can also utilize a library like graphql-s2s which allows you to utilize inheritance and generic types. Schemas generated this way still have to be compiled to valid SDL though -- at best, libraries like graphql-s2s just offer some syntactic sugar and a better DX.

Lastly, you can restructure your types to avoid the field duplication altogether at the cost of a more structured response. For example, instead of doing this:

type A {
  a: Int
  foo: String
  bar: String
}

type B {
  b: Int
  foo: String
  bar: String
}

you can do this:

type X {
  foo: String
  bar: String
  aOrB: AOrB
}

union AOrB = A | B

type A {
  a: Int
}

type B {
  b: Int
}

Solution 2

Using a schema transpiler like graphql-s2s to achieve inheritance is probably overkill, and graphql-s2s is outdated as of 2021.

Have a look at this Apollo Server directive: https://github.com/jeanbmar/graphql-inherits

const typeDefs = gql`
  directive @inherits(type: String!) on OBJECT

  type Car {
    manufacturer: String
    color: String
  }
  
  type Tesla @inherits(type: "Car") {
    manufacturer: String
    papa: String
    model: String
  }
`;

class InheritsDirective extends SchemaDirectiveVisitor {
    visitObject(type) {
        const fields = type.getFields();
        const baseType = this.schema.getTypeMap()[this.args.type];
        Object.entries(baseType.getFields()).forEach(([name, field]) => {
            if (fields[name] === undefined) {
                fields[name] = field;
            }
        });
    }
}
Share:
18,724

Related videos on Youtube

Chris Geirman
Author by

Chris Geirman

Hacking everyday on something react, react-native, graphql, serverless, and coldfusion (yes, it's still a thing!)

Updated on September 15, 2022

Comments

  • Chris Geirman
    Chris Geirman over 1 year

    I have a type User. Users can also be a type TeamMember. The only difference between a User and TeamMember is an added field teamRole: String. So, I’d love to do something like the following to avoid having to redundantly define all the user's fields…

      type User {
        id: ID!,
        name: String,
        (many other field defs)
      }
    
      type TeamMember extends User  {
        teamRole: String,
      }
    

    Anyone aware of a syntax for this? I thought extend would be the answer, but it seems more like javascript’s prototype

  • Chris Geirman
    Chris Geirman over 6 years
    Thanks! This is exactly what I was looking for. Well, not exactly, since ideally I wouldn't have needed another package, but graphql-s2s gives me what I needed. I'm using graphql-yoga as my server, so I had to monkey patch their typeDefs declaration, which is also less than idea. If you know of a better way, I'm all ears. Thanks!
  • A.com
    A.com almost 5 years
    Is it true this is still not available as of mid-2019?
  • Daniel Rearden
    Daniel Rearden almost 5 years
    @A.com Yes. You can see the latest specification here.
  • Alexander
    Alexander about 4 years
    but wouldn't you as of early 2020 use @inherit from instead?
  • Daniel Rearden
    Daniel Rearden about 4 years
    @Alexander that is not a standard directive -- what library is that used by?
  • flodin
    flodin almost 4 years
    When I use google to search the exact phrase "@inherit from", this stack overflow question is the only page that shows up. You've invented a brand new sentence.