11

This is a very basic question but how do you call an extended type or interface?

All the documentations points to using extend type Person to add fields based on Person.

I would expect it to work like this

Employee extend type Person { salary: Int! } 

But the documentation suggests it's like this:

extend type Person{ salary: Int! } 

So, how do I query for an Employee salary? What if there are multiple extensions of Person, e.g. Employee and Renter? I think I might be hampered by traditional thinking but I would expect the extension to result in something named and queryable.

1 Answer 1

14

The extend keyword is effectively used to modify an existing type within a schema. This is most commonly used in two scenarios:

1. Concatenating multiple strings that represent a single schema. You can have your schema broken up across multiple files, divided by domain. You can then do something like:

#base.graphql type Query { viewer: User } # user.graphql extend type Query { users: [User!]! } # post.graphql extend type Query { post: [Post!]! } 

This results in a schema that's effectively the same as:

type Query { viewer: User users: [User!]! post: [Post!]! } 

2. Extending from a base schema. You might have multiple schemas that build on top of some base schema. Or you might be stitching together remote schemas. In these scenarios, we often want to add fields specific to our new schema that don't exist on the base types. This can be used to implement directives that are missing from the base schema as well:

extend type SomeType @customDirective 

The extend keyword can only modify existing types; it is not a vehicle for inheritance. In fact, GraphQL does not support type inheritance. Interfaces provide a level of abstraction over existing types, but types that implement an interface do not inherit any fields from that interface. There's no way to do that, unless you use some library like graphql-s2s.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, yeah, so I was indeed hampered by traditional thinking. I was indeed assuming some kind of inheritance.
Ok, so how does one actually query for the local field added via extend? The code generates a GraphQLError: Cannot query field "salary" on type "Person".
@DanDascalescu this would make a good separate question, since it doesn't directly deal with the subject of the original question or my answer.
The OP (also) asked, "So, how do I query for an Employee salary?", which is what I'm trying to do in that example. The schema definition works, querying does not.
Like I explained in response to your Github comment, buildSchema doesn't support extensions. It simply ignores any type system extension definitions that happen to be present. You're querying it correctly, but you need to build your schema using a different means.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.