-
Notifications
You must be signed in to change notification settings - Fork 2k
Support for union types when using buildSchema #947
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
Conversation
8e05738
to
511e2df
Compare
@jonbretman I like the idea 👍 I also think |
511e2df
to
f89ed6a
Compare
Thanks for the feedback @IvanGoncharov. Agree that I've made some changes to how this works, not exactly what you were suggesting but avoids the if-else-elseif complexity in |
@jonbretman I think such functionality makes sense for both Union and Interfaces. |
f89ed6a
to
6857be1
Compare
@IvanGoncharov makes sense. Updated to support Interface types too and added a test for that. |
@jonbretman Just reviewed this PR one more time and notice that you implemented this behavior only for I think type resolvers should be handled the same way as field resolvers. That mean you can pass |
6d18701
to
88017c4
Compare
@IvanGoncharov sorry for the delay in updating this PR. I've now made the changes you suggested ( think). Can you have another look please? |
@jonbretman NP. Can you do exact same changes as in 956d58b in that case you don't need change signature of |
88017c4
to
062c9d9
Compare
@IvanGoncharov I'm not sure what you mean by "exact same". I needed to change the signature of |
@jonbretman I meant that you should use In your current implementation you first check presence of I made a commit on top of your PR which solves above problem and also doesn't require change to If you agree with proposed changes please merge this commit into your PR. |
062c9d9
to
7f2434e
Compare
@IvanGoncharov ok I understand now, I had tried removing the It seems like you are more familiar with the codebase than I am but I'm not sure I agree with what you are suggesting. I think that there should be a sensible default for resolving interface and union types with a generated schema and allowing it to be provided via a I've updated this PR with something that supports both @IvanGoncharov I appreciate you taking the time to help review this and I'd love to see something get merged that adds this support. Hopefully we can get something that everyone is happy with. |
@jonbretman Got carried away 😄 Patching In theory, it's better to have same runtime behavior for schemas created by calling Would be great if @leebyron comment on this issue so we know which direction to go. |
@leebyron I'd still like to try to get this moved forward if possible - any thoughts? |
Thanks for this great work and your patience. I really like this idea though I think it can be generalized further - no need to limit this to only generated schemas instead of explicitly defined ones. I'll amend this PR to generalize the concept and remove some cleaned up code in the process! |
@leebyron no worries, thanks for getting this landed 👍 |
I'm aware that this is possible with https://github.com/apollographql/graphql-tools but it seems like something which should work out of the box with just this library.
I came up with two ways in which this could work but went for this one as an initial implementation.
Idea 1 (this PR):
For union types a
__type
field is added to the resolved objects for which the value is a string corresponding to the correct type to use. You can see the test for an example of this.Idea 2:
A second argument to
buildSchema
which takes some options one of which could be customresolveType
functions for union types e.g.This would then get passed through to
buildASTSchema
and used here instead of the default function which throws an error.Open to suggestions. I believe this should work for interfaces too but didn't add a test as wanted to get some feedback first.