Graphql-js: Extending an Object Type in Code

Created on 8 Nov 2018  路  6Comments  路  Source: graphql/graphql-js

(Posting here since haven't been able to get an answer from any other of channels. 馃槄)

So, using SDL you can add fields to an existing type like:

extend type Query {
   extraField: Int!
}

But how can I do this in code using the built-in classes (GraphQLObjectType, etc)?

Sounds like extendSchema might offer a way, but it is undocumented and I'm not sure how to use it in this case.

Is this supported?

My apologies in advance if I'm missing something really obvious.

question

All 6 comments

+1

Is this supported?

@ldiego08 Yes, it's supported. Our documentation lagging behind since it had last major updated in 2016. Discussed in #1368

For example on how to use extendSchema please see our tests:
https://github.com/graphql/graphql-js/blob/master/src/utilities/__tests__/extendSchema-test.js

@IvanGoncharov Sorry to ask this after a while, but do we have now docs about this?

can we reopen this?

as extendSchema only works with SDL and not with graphql-js types?

+1, wanna reference external type for apollo federation as a plain string, but don't see other way than low-level string replace atm

+1, any ideas on how to do this via code

Was this page helpful?
0 / 5 - 0 ratings