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

feat: allow extra params in body #3333

Merged
merged 5 commits into from
Jul 1, 2024
Merged

feat: allow extra params in body #3333

merged 5 commits into from
Jul 1, 2024

Conversation

ardatan
Copy link
Collaborator

@ardatan ardatan commented Jun 27, 2024

By default, Yoga does not allow extra parameters in the request body other than query, operationName, extensions, and variables, then throws 400 HTTP Error.
This change adds a new option called extraParamNames to allow extra parameters in the request body.

import { createYoga } from 'graphql-yoga';

const yoga = createYoga({
  /* other options */
  extraParamNames: ['extraParam1', 'extraParam2'],
});

const res = await yoga.fetch('/graphql', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        query: 'query { __typename }',
        extraParam1: 'value1',
        extraParam2: 'value2',
    }),
});

console.assert(res.status === 200);

Copy link

changeset-bot bot commented Jun 27, 2024

🦋 Changeset detected

Latest commit: b1bfc8a

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

This PR includes changesets to release 23 packages
Name Type
graphql-yoga Minor
@graphql-yoga/nestjs Major
@graphql-yoga/render-graphiql Major
@graphql-yoga/plugin-apollo-inline-trace Major
@graphql-yoga/apollo-managed-federation Major
@graphql-yoga/plugin-apq Major
@graphql-yoga/plugin-csrf-prevention Major
@graphql-yoga/plugin-defer-stream Major
@graphql-yoga/plugin-disable-introspection Major
@graphql-yoga/plugin-graphql-sse Major
@graphql-yoga/plugin-jwt Major
@graphql-yoga/plugin-persisted-operations Major
@graphql-yoga/plugin-prometheus Major
@graphql-yoga/plugin-response-cache Major
@graphql-yoga/plugin-sofa Major
apollo-federation-gateway-with-yoga Patch
apollo-subgraph-with-yoga Patch
graphql-lambda Patch
cloudflare-advanced Patch
cloudflare Patch
nextjs-app Patch
hello-world-benchmark 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

Copy link
Contributor

github-actions bot commented Jun 27, 2024

💻 Website Preview

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

Copy link
Contributor

github-actions bot commented Jun 27, 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}

     checks.......................................: 100.00% ✓ 419648      ✗ 0     
     data_received................................: 1.7 GB  14 MB/s
     data_sent....................................: 85 MB   706 kB/s
     http_req_blocked.............................: avg=1.48µs   min=1µs      med=1.32µs   max=244.91µs p(90)=1.97µs   p(95)=2.16µs  
     http_req_connecting..........................: avg=2ns      min=0s       med=0s       max=123.54µs p(90)=0s       p(95)=0s      
     http_req_duration............................: avg=359.52µs min=216.29µs med=323.28µs max=24.01ms  p(90)=462.46µs p(95)=482.4µs 
       { expected_response:true }.................: avg=359.52µs min=216.29µs med=323.28µs max=24.01ms  p(90)=462.46µs p(95)=482.4µs 
     ✓ { mode:graphql-jit }.......................: avg=282.91µs min=216.29µs med=260.79µs max=15.38ms  p(90)=290.59µs p(95)=302.96µs
     ✓ { mode:graphql-no-parse-validate-cache }...: avg=485.71µs min=395.66µs med=458.14µs max=10.36ms  p(90)=499.43µs p(95)=527.11µs
     ✓ { mode:graphql-response-cache }............: avg=341.63µs min=268.51µs med=322.2µs  max=7.57ms   p(90)=353.98µs p(95)=365.4µs 
     ✓ { mode:graphql }...........................: avg=363.92µs min=276.07µs med=332.44µs max=24.01ms  p(90)=377.99µs p(95)=412.07µs
     http_req_failed..............................: 0.00%   ✓ 0           ✗ 209824
     http_req_receiving...........................: avg=33.72µs  min=16.15µs  med=33.56µs  max=1.14ms   p(90)=39.05µs  p(95)=41.11µs 
     http_req_sending.............................: avg=8.09µs   min=5.92µs   med=7.17µs   max=302.42µs p(90)=10.72µs  p(95)=11.47µs 
     http_req_tls_handshaking.....................: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting.............................: avg=317.69µs min=180.47µs med=281.97µs max=23.85ms  p(90)=420.24µs p(95)=438.08µs
     http_reqs....................................: 209824  1748.506543/s
     iteration_duration...........................: avg=567.07µs min=386.05µs med=527.68µs max=24.72ms  p(90)=673.56µs p(95)=697.89µs
     iterations...................................: 209824  1748.506543/s
     vus..........................................: 1       min=1         max=1   
     vus_max......................................: 2       min=2         max=2   

Copy link
Contributor

github-actions bot commented Jun 27, 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 Jun 27, 2024

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@graphql-yoga/apollo-link 3.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/urql-exchange 3.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
graphql-yoga 5.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/nestjs 3.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/nestjs-federation 3.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/plugin-apollo-inline-trace 3.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/apollo-managed-federation 0.2.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/plugin-apq 3.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/plugin-csrf-prevention 3.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/plugin-defer-stream 3.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/plugin-disable-introspection 2.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/plugin-graphql-sse 3.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/plugin-jwt 2.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/plugin-persisted-operations 3.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/plugin-prometheus 5.2.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/plugin-response-cache 3.7.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/plugin-sofa 3.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎
@graphql-yoga/render-graphiql 5.5.0-alpha-20240628224657-b1bfc8af npm ↗︎ unpkg ↗︎

Copy link
Collaborator

@EmrysMyrddin EmrysMyrddin left a comment

Choose a reason for hiding this comment

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

As said in slack, I would prefere to give access to the extra params in a seperate ctx field.

});

console.assert(res.status === 200);
```
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we also add this to the normal documentation?

Maybe a section called "Request customization", where we show this and also how you can set a custom GraphQL request parser?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

0203957
What do you think?

@enisdenjo
Copy link
Collaborator

As said in slack, I would prefere to give access to the extra params in a seperate ctx field.

@EmrysMyrddin why?

@@ -402,6 +402,26 @@ describe('requests', () => {
expect(body.errors?.[0].message).toBe('Unexpected parameter "test" in the request body.');
});

it('does not error if there is a specified invalid parameter in the request body', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe also add a test of having a field that's not specified in the extraParamNames?

Copy link
Collaborator Author

@ardatan ardatan Jun 28, 2024

Choose a reason for hiding this comment

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

Good idea! Done!
5f07980

Copy link
Collaborator

@n1ru4l n1ru4l left a comment

Choose a reason for hiding this comment

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

approved, bit lets add also some basic documentation

@ardatan ardatan merged commit 9f3f945 into main Jul 1, 2024
35 checks passed
@ardatan ardatan deleted the extra-params-in-body branch July 1, 2024 10:54
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