Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending an Object Type in Code #1569

Closed
ldiego08 opened this issue Nov 8, 2018 · 8 comments
Closed

Extending an Object Type in Code #1569

ldiego08 opened this issue Nov 8, 2018 · 8 comments
Labels

Comments

@ldiego08
Copy link

ldiego08 commented Nov 8, 2018

(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.

@IvanGoncharov
Copy link
Member

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

@CoericK
Copy link

CoericK commented May 16, 2020

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

@sibelius
Copy link

can we reopen this?

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

@tot-ra
Copy link

tot-ra commented Sep 15, 2020

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

@danielblignaut
Copy link

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

@ostap1010
Copy link

+1, how to use "extend" with GraphQLObjectType?

@sjerratsch
Copy link

sjerratsch commented May 25, 2021

I was just experimenting with this and it turns out you can force the "extend type ..." in the SDL string output, if you do the following:

new GraphQLObjectType({
    ...
    name: "*NAME HERE*",
    astNode: {kind: "ObjectTypeExtension", name: {kind: "Name", value: "*NAME HERE*"}},
    extensionASTNodes: [{kind: "ObjectTypeExtension", name: {kind: "Name", value: "*NAME HERE*"}}],
    ...
})

You need to set both. This won't interfere with the fields, but you may need to put directives yourself in "astNode" then, not sure.

@isaacgr
Copy link

isaacgr commented Aug 25, 2021

+1, how to use "extend" with GraphQLObjectType?

Is there anything like this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants