dotansimha / graphql-yoga

🧘 Rewrite of a fully-featured GraphQL Server with focus on easy setup, performance & great developer experience. The core of Yoga implements WHATWG Fetch API and can run/deploy on any JS environment.
https://the-guild.dev/graphql/yoga-server
MIT License
8.25k stars 574 forks source link

apollo-inline-trace - tracing should not fail if non-nullable field throw an error #3455

Closed kroupacz closed 1 week ago

kroupacz commented 2 weeks ago

Description

If non-nullable field in GQL schema throw an error then the tracing plugin is not able to find correct trace node by its path and throw an error here. See the details in test called 'FAILING TEST - nonNullableFail - simple query - tracing plugin will throw "Could not find node with path testNestedField.failing" error' in this PR.

I am not sure what is the reason why the path is missing in the "ctx.nodes", but probably the reason is that: https://spec.graphql.org/draft/#sel-GAPHRPTCAACEzBg6S

If the field which experienced an error was declared as Non-Null, the null result will bubble up to the next nullable field.

A possible solution could be:

if (Array.isArray(errToReport.path)) { const specificNode = getNearestNode(ctx.nodes,errToReport.path); if (specificNode) { node = specificNode; } else { throw new Error(Could not find node with path ${errToReport.path.join('.')}); } }

... ... ... ...

/**

Test Environment:

changeset-bot[bot] commented 2 weeks ago

🦋 Changeset detected

Latest commit: e5bafe9cf4ab878e5c9854d1c9da3010b14fbe75

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages | Name | Type | | ---------------------------------------- | ----- | | @graphql-yoga/plugin-apollo-usage-report | Patch | | @graphql-yoga/plugin-apollo-inline-trace | Patch | | @graphql-yoga/nestjs-federation | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

kroupacz commented 1 week ago

Hello @EmrysMyrddin, I just added commit with a possible fix that works without problems in my test and local environment. May i ask you to check it and info if the fix is ok for you? Then I will remove the test environment (tests) from packages/plugins/apollo-inline-trace/__tests__/apollo-inline-trace-failing-tests folder.

Thank you in advance. Tomáš Kroupa

kroupacz commented 1 week ago

Interesting, I tried to write a similar test in this test suite, but I wasn't able to reproduce the same issue. The problem seems to only occur if the Yoga is used as a gateway. 🤔 @EmrysMyrddin or @ardatan, may i ask you to help me rewrite the last test called 'FAILING TEST - nonNullableFail - simple query - tracing plugin will throw "Could not find node with path testNestedField.failing" error to be more suitable for your test environment (so remove my custom implementation with @apollo/gateway, axios, ...) 🙏

ardatan commented 1 week ago

@kroupacz Usually we avoid dealing with real servers but use yoga.fetch for testing that mimics the real fetch as you can see in the existing tests.

kroupacz commented 1 week ago

Hello @EmrysMyrddin and @ardatan, I just rewrote the tests to fit more into the testing environment of this repository. But I am not sure what to do with following error which can be seen in CI.

    TypeError: Cannot read properties of undefined (reading 'QUERY')

      1 | /* eslint-disable import/no-extraneous-dependencies */
      2 | import { GraphQLSchema } from 'graphql';
    > 3 | import { IntrospectAndCompose, LocalGraphQLDataSource } from '@apollo/gateway';
        | ^
kroupacz commented 1 week ago

The above mentioned issue is caused by incompatibility with graphql@15.8.0. 😞 @apollo/federation-internals module used in @apollo/gateway requires graphql@16 or newer.

node_modules/.pnpm/@apollo+federation-internals@2.9.3_graphql@15.8.0/node_modules/@apollo/federation-internals/src/graphQLJSSchemaToAST.ts:26:50

Is there a way to do not run this test against graphql@15?

kroupacz commented 1 week ago

FYI @EmrysMyrddin , @ardatan I solved the incompatibility with graphql@15 like this:

import { versionInfo } from 'graphql';

const describeIf = (condition: boolean) => (condition ? describe : describe.skip);

describeIf(versionInfo.major >= 16)('Inline Trace - Yoga gateway', () => {
  ... ...

I hope you will be ok with that... 🙂

kroupacz commented 1 week ago

Hello @EmrysMyrddin, could we somehow finish this together? Is there anything else that needs to be done on my side? It should be the last piece which is blocking us from starting to use GraphQL Yoga as a gateway. 🙂 Thank you in advance. Tomáš

kroupacz commented 1 week ago

FYI: @EmrysMyrddin @ardatan I added one more commit with updated version of @envelop/on-resolve package. Can I ask for a code review of this PR? 🙏 We're quite in a hurry for this fix because we can't use yoga without it. Thank you in advance. Tomáš