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

fix(gateway): pass null required fields correctly to resolvers #4157

Conversation

jakeblaxon
Copy link
Contributor

This fixes a bug in the Apollo Gateway, where certain subfields that were null in the parent object that is passed to resolvers were getting expanded into objects that contained all null subfields.

Scenario

When service B requires a field from service A, if that field is null in service A, it would be passed to B's resolver expanded as an object with all null subfields.

For example, if B required the field movie from service A in its resolver, and the value in service A looks like the following:

"actor": {
    "movie": null
}

the actor object, being the parent, would be passed to B's resolver as the following:

"actor": {
    "movie": {
        "title": null,
        "year": null,
        ...
    }
}

This can cause unexpected errors in the resolver logic, since the resolver may assume that the title and year fields will never be null (as per the graphQL schema).

This fix prevents this expansion, so that the actor object passed toB's resolver remains the same, i.e. the actor.movie field is null.

@jakeblaxon
Copy link
Contributor Author

@trevor-scheer this was another bug I had noticed when working with nested @requires fields. I believe I pinpointed the source of the error, but I'd like your review to make sure that this fix is safe. I'd prefer to get this fix in with the same release as #4064, if possible. Please let me know if you need anything from me. Thanks!

@abernix abernix closed this Jun 24, 2020
@abernix abernix reopened this Jun 25, 2020
@abernix abernix changed the base branch from master to main June 25, 2020 09:02
@jakeblaxon jakeblaxon force-pushed the prevent-null-from-expanding-into-objects branch from b5a418a to 56a5661 Compare June 25, 2020 21:14
@jakeblaxon jakeblaxon changed the title fix(gateway): prevent null fields from expanding into objects when passed to resolvers fix(gateway): pass null required fields correctly to resolvers Jun 25, 2020
@jakeblaxon
Copy link
Contributor Author

@trevor-scheer can you take a look at this when you get a chance? This fixes an issue where the resolver parent type can sometimes break the contract as specified by the schema (e.g. certain non-nullable fields appear as null in the parent type passed to the resolver). This can cause some nasty bugs in production since it breaks type safety.

@trevor-scheer
Copy link
Member

Hey @jakeblaxon, thank you for patience (and effort!) on this. This looks good to me. If you don't mind pushing up a few comments that explain the conditions that we're handling differently that would be much appreciated. Explaining the edges will be helpful for us in the future 🙂

@jakeblaxon
Copy link
Contributor Author

@trevor-scheer sounds good! I actually just found what I think is a more simple fix for this. I left a comment above to explain the check as well. Can you take another look and let me know what you think?

@trevor-scheer
Copy link
Member

@jakeblaxon in digging in a bit more deeply to understand this change, I found that your test passes even if I revert the change. Are you aware of that? This is not to say the change is necessarily wrong, but if it's correct then I think the test may be missing the point. Maybe we need to reconsider the testing path? Let me know your thoughts!

@jakeblaxon
Copy link
Contributor Author

jakeblaxon commented Jul 24, 2020

@trevor-scheer That's interesting. The test fails for me, even with the latest pull from main. Did you make sure to run npm run compile beforehand? I know that's tripped me up before.

@trevor-scheer
Copy link
Member

Thanks @jakeblaxon, my mistake on that and all is well! This is good to go. Thanks again 🥳

@trevor-scheer trevor-scheer merged commit eeaeaf0 into apollographql:main Jul 24, 2020
@jakeblaxon jakeblaxon deleted the prevent-null-from-expanding-into-objects branch July 24, 2020 23:39
abernix pushed a commit to apollographql/federation that referenced this pull request Sep 4, 2020
…ographql/apollo-server#4157)

This commit fixes a bug in the Apollo Gateway, where certain subfields that
were null in the parent object that is passed to resolvers were getting
expanded into objects that contained all null subfields.
Apollo-Orig-Commit-AS: apollographql/apollo-server@eeaeaf0
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants