23

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
  • 9,474
  • 5
  • 37
  • 70

2 Answers2

17

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
}
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • Thanks! This is exactly what I was looking for. Well, not exactly, since ideally I wouldn't have needed another package, but [`graphql-s2s`](https://github.com/nicolasdao/graphql-s2s) gives me what I needed. I'm using [`graphql-yoga`](https://github.com/graphcool/graphql-yoga/) as my server, so I had to [`monkey patch their typeDefs`](https://github.com/graphcool/graphql-yoga/blob/master/src/index.ts#L48) declaration, which is also less than idea. If you know of a better way, I'm all ears. Thanks! – Chris Geirman Nov 28 '17 at 21:21
  • Is it true this is still not available as of mid-2019? – A.com Jun 15 '19 at 14:34
  • @A.com Yes. You can see the latest specification [here](https://graphql.github.io/graphql-spec/). – Daniel Rearden Jun 15 '19 at 14:37
  • but wouldn't you as of early 2020 use `@inherit from` instead? – Alexander Mar 31 '20 at 11:26
  • 1
    @Alexander that is not a standard directive -- what library is that used by? – Daniel Rearden Mar 31 '20 at 11:38
  • 1
    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. – flodin Jun 29 '20 at 15:20
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;
            }
        });
    }
}
Jean-Baptiste Martin
  • 1,399
  • 1
  • 10
  • 19