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(sofa): respect error extensions in case of 404 #3559

Merged
merged 6 commits into from
Dec 19, 2024
Merged

Conversation

ardatan
Copy link
Collaborator

@ardatan ardatan commented Dec 16, 2024

Fix the issue when SOFA returns 404 response from error extensions returned by a resolver, it will cause the server to continue the request handling with Yoga but instead it should return the response with 404 and the body SOFA returns.

That line was causing a 404 response to continue with Yoga's handler;
https://github.com/dotansimha/graphql-yoga/pull/3559/files#diff-ede815ebc73ee3925e49864ba3ac573940f77e8542cf395ccbf405b284c006aeL120

As in this test case;

  it('forwards error extensions correctly', async () => {
    const yoga = createYoga({
      schema: createSchema({
        typeDefs: /* GraphQL */ `
          type Query {
            me: Account
          }
          type Account {
            id: ID!
            name: String!
          }
        `,
        resolvers: {
          Query: {
            me: () => {
              throw createGraphQLError('account not found', {
                extensions: {
                  code: 'ACCOUNT_NOT_FOUND',
                  http: { status: 404 },
                },
              });
            },
          },
        },
      }),
      plugins: [
        useSofa({
          basePath: '/api',
        }),
      ],
    });
    for (let i = 0; i < 10; i++) {
      const res = await yoga.fetch('http://localhost/api/me');
      expect(res.status).toBe(404);
      const json = await res.json();
      expect(json).toEqual({
        errors: [
          {
            message: 'account not found',
            extensions: {
              code: 'ACCOUNT_NOT_FOUND',
            },
            path: ['me'],
          },
        ],
      });
    }
  });

Copy link

changeset-bot bot commented Dec 16, 2024

🦋 Changeset detected

Latest commit: da02de1

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

This PR includes changesets to release 1 package
Name Type
@graphql-yoga/plugin-sofa 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

Copy link
Contributor

github-actions bot commented Dec 16, 2024

Apollo Federation Subgraph Compatibility Results

Federation 1 Support Federation 2 Support
_service🟢
@key (single)🟢
@key (multi)🟢
@key (composite)🟢
repeatable @key🟢
@requires🟢
@provides🟢
federated tracing🟢
@link🟢
@shareable🟢
@tag🟢
@override🟢
@inaccessible🟢
@composeDirective🟢
@interfaceObject🟢

Learn more:

Copy link
Contributor

github-actions bot commented Dec 16, 2024

💻 Website Preview

The latest changes are available as preview in: https://828372f7.graphql-yoga.pages.dev

Copy link
Contributor

github-actions bot commented Dec 16, 2024

✅ Benchmark Results

     ✓ no_errors{mode:graphql}
     ✓ expected_result{mode:graphql}
     ✓ no_errors{mode:graphql-jit}
     ✓ expected_result{mode:graphql-jit}
     ✓ no_errors{mode:graphql-response-cache}
     ✓ expected_result{mode:graphql-response-cache}
     ✓ no_errors{mode:graphql-no-parse-validate-cache}
     ✓ expected_result{mode:graphql-no-parse-validate-cache}
     ✓ no_errors{mode:uws}
     ✓ expected_result{mode:uws}

     checks.......................................: 100.00% ✓ 514146      ✗ 0     
     data_received................................: 2.1 GB  14 MB/s
     data_sent....................................: 103 MB  689 kB/s
     http_req_blocked.............................: avg=1.51µs   min=990ns    med=1.31µs   max=294.34µs p(90)=1.98µs   p(95)=2.17µs  
     http_req_connecting..........................: avg=2ns      min=0s       med=0s       max=145.16µs p(90)=0s       p(95)=0s      
     http_req_duration............................: avg=363.91µs min=220.2µs  med=330.24µs max=18.81ms  p(90)=473.77µs p(95)=495.39µs
       { expected_response:true }.................: avg=363.91µs min=220.2µs  med=330.24µs max=18.81ms  p(90)=473.77µs p(95)=495.39µs
     ✓ { mode:graphql-jit }.......................: avg=292.94µs min=220.2µs  med=275.51µs max=18.81ms  p(90)=307.14µs p(95)=322.7µs 
     ✓ { mode:graphql-no-parse-validate-cache }...: avg=499.04µs min=407.81µs med=475.04µs max=5.94ms   p(90)=516.46µs p(95)=562.29µs
     ✓ { mode:graphql-response-cache }............: avg=345.95µs min=270.63µs med=329.48µs max=6.8ms    p(90)=359.6µs  p(95)=370.78µs
     ✓ { mode:graphql }...........................: avg=371.8µs  min=281.29µs med=340.66µs max=14.74ms  p(90)=411.8µs  p(95)=457.18µs
     ✓ { mode:uws }...............................: avg=346.95µs min=270.33µs med=328.37µs max=6.23ms   p(90)=363.36µs p(95)=383.89µs
     http_req_failed..............................: 0.00%   ✓ 0           ✗ 257073
     http_req_receiving...........................: avg=32.35µs  min=16.09µs  med=31.84µs  max=3.39ms   p(90)=38.56µs  p(95)=41.09µs 
     http_req_sending.............................: avg=8.63µs   min=5.88µs   med=7.56µs   max=424.86µs p(90)=11µs     p(95)=12.27µs 
     http_req_tls_handshaking.....................: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting.............................: avg=322.92µs min=190.57µs med=290.21µs max=18.71ms  p(90)=432.14µs p(95)=452.39µs
     http_reqs....................................: 257073  1713.807253/s
     iteration_duration...........................: avg=578.55µs min=396.67µs med=540.62µs max=19.39ms  p(90)=691.65µs p(95)=718.1µs 
     iterations...................................: 257073  1713.807253/s
     vus..........................................: 1       min=1         max=1   
     vus_max......................................: 2       min=2         max=2   

@Urigo Urigo self-requested a review December 18, 2024 13:06
Copy link
Collaborator

@Urigo Urigo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a documentation on how SOFA handles errors?

@ardatan
Copy link
Collaborator Author

ardatan commented Dec 18, 2024

@Urigo Good point!
I created this PR for it!
Urigo/SOFA#1612

renovate bot and others added 5 commits December 18, 2024 17:55
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@Urigo
Copy link
Collaborator

Urigo commented Dec 18, 2024

can you also link to the SOFA docs from the Yoga docs?
I think they are not linked so people can't find the full documentation?

@Urigo
Copy link
Collaborator

Urigo commented Dec 18, 2024

also, I see a lot of user comments on this page on discuss:
#2015

Can you please review all these questions and make sure there are good answers for them, including in the docs?

@ardatan
Copy link
Collaborator Author

ardatan commented Dec 19, 2024

Can you please review all these questions and make sure there are good answers for them, including in the docs?

I did and updated PR based on the feedback. Let me know if it is good enough.
I'll now add a link to SOFA docs here

Copy link
Collaborator

@Urigo Urigo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work!

@ardatan ardatan merged commit 5620283 into main Dec 19, 2024
25 checks passed
@ardatan ardatan deleted the fix-sofa-plugin branch December 19, 2024 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants