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(proxiedResults): handle top level errors when using type merging #1648

Merged

Conversation

nicolas-cherel
Copy link
Contributor

@nicolas-cherel nicolas-cherel commented Jun 16, 2020

NOTE: once again, I've doubts to this being the right fix. maybe errors on query toplevel fields should throw errors instead of merely go through merging, the thing is, yet again, I'm not familiar enough with underlying principles to figure out the right call. But somehow, this fix solves the issue at the right place — on processing subschema results.

If the toplevel query of delegation has failed and reports an error on toplevel, with null as result,
handleNull() returns the errors has result, then mergeProxiedResults(), concats an error array consisting of [undefined], the errors props not being set on exception (in a previous fix on mergeProxiedResults, it was assumed that sources where always execution results).

I suspect that the real problem is the handleNull with handleResults logics that mangles errors and values results processing, but I'm not confortable enought to revize that.

The solution here is to twist the assomption that sources parameters of mergeProxiedResults() only receives results object but also exceptions if any occured, and treat them accordingly:

  • by putting the exception in the errors array
  • by removing the related source from the results merge

without the fix, we would endup with the non-informative following message in query errors, triggered when trying to process the [undefined] errors array mentionned above:

      TypeError: Cannot read property 'path' of undefined
          at Object.getErrors (graphql-tools/packages/utils/src/errors.ts:52:16)
          at defaultMergedResolver (graphql-tools/packages/delegate/src/defaultMergedResolver.ts:25:18)
          at resolveFieldValueOrError (graphql-tools/node_modules/graphql/execution/execute.js:485:18)
          at resolveField (graphql-tools/node_modules/graphql/execution/execute.js:443:16)
          at executeFields (graphql-tools/node_modules/graphql/execution/execute.js:280:18)
          at collectAndExecuteSubfields (graphql-tools/node_modules/graphql/execution/execute.js:731:10)
          at completeObjectValue (graphql-tools/node_modules/graphql/execution/execute.js:721:10)
          at completeValue (graphql-tools/node_modules/graphql/execution/execute.js:609:12)
          at completeValueCatchingError (graphql-tools/node_modules/graphql/execution/execute.js:513:19)
          at resolveField (graphql-tools/node_modules/graphql/execution/execute.js:444:10) {
        locations: [ [Object] ],
        path: [ 'userById', 'id' ]

…with error on query toplevel

If the toplevel query of delegation has failed and reports an error on toplevel, with null as result,
`handleNull()` returns the errors has result, then `mergeProxiedResults()`, concats an error array consisting of
`[undefined]`, the errors props not being set on exception (in a previous fix on mergeProxiedResults, it was
assumed that sources where always execution results).

I suspect that the real problem is the `handleNull` with `handleResults` logics that mangles errors and values
results processing, but I'm not confortable enought to revize that.

The solution here is to twist the assomption that `sources` parameters of `mergeProxiedResults()` only
receives results object but also exceptions if any occured, and treat them accordingly:
 - by putting the exception in the errors array
 - by removing the related source from the results merge

without the fix, we would endup with the non-informative following message in query errors, triggered
when trying to process the `[undefined]` errors array mentionned above:

```
      TypeError: Cannot read property 'path' of undefined
          at Object.getErrors (graphql-tools/packages/utils/src/errors.ts:52:16)
          at defaultMergedResolver (graphql-tools/packages/delegate/src/defaultMergedResolver.ts:25:18)
          at resolveFieldValueOrError (graphql-tools/node_modules/graphql/execution/execute.js:485:18)
          at resolveField (graphql-tools/node_modules/graphql/execution/execute.js:443:16)
          at executeFields (graphql-tools/node_modules/graphql/execution/execute.js:280:18)
          at collectAndExecuteSubfields (graphql-tools/node_modules/graphql/execution/execute.js:731:10)
          at completeObjectValue (graphql-tools/node_modules/graphql/execution/execute.js:721:10)
          at completeValue (graphql-tools/node_modules/graphql/execution/execute.js:609:12)
          at completeValueCatchingError (graphql-tools/node_modules/graphql/execution/execute.js:513:19)
          at resolveField (graphql-tools/node_modules/graphql/execution/execute.js:444:10) {
        locations: [ [Object] ],
        path: [ 'userById', 'id' ]
```
@yaacovCR yaacovCR closed this Jun 16, 2020
@yaacovCR yaacovCR reopened this Jun 16, 2020
@yaacovCR
Copy link
Collaborator

Closed in error!

@yaacovCR
Copy link
Collaborator

yaacovCR commented Jun 16, 2020

Looks good to me. throwing if any top level field gives you an error would mean that fewer fields get returned, doesn't seem sensible to drop the subschemas that are working

@yaacovCR
Copy link
Collaborator

yaacovCR commented Jun 16, 2020

The "mangling" happens because delegation is optimized for stitching so that users can simply return the result of delegation and allow GraphQL to do its thing. I don't see that changing anytime soon even though it is not so straightforward.

@nicolas-cherel
Copy link
Contributor Author

Ho, the comment is not precise enough. By mangling, I just wanted to mean that the original error was replaced by another one, unrelated, and thrown due to invalid data (undefined entry) in the errors array.

I've absolutely no problems with subschema errors aggregations :)

@ardatan ardatan added the bugfix label Jun 18, 2020
@yaacovCR yaacovCR changed the title fix(proxiedResults): don't mangle errors when delegation yields null on toplevel fix(proxiedResults): handle top level errors when using type merging Jun 21, 2020
@yaacovCR yaacovCR merged commit 48c96f1 into ardatan:master Jun 21, 2020
@theguild-bot
Copy link
Collaborator

The latest changes of this PR are available as alpha in npm: 6.0.11-alpha-48c96f1.0

Quickly update your package.json by running:

npx match-version @graphql-tools 6.0.11-alpha-48c96f1.0

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

Successfully merging this pull request may close these issues.

None yet

4 participants