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.21k stars 569 forks source link

feat: allow extra params in body #3333

Closed ardatan closed 3 months ago

ardatan commented 3 months ago

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);
changeset-bot[bot] commented 3 months ago

🦋 Changeset detected

Latest commit: b1bfc8afe4b3ba995a9f0355586a23f109036604

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

github-actions[bot] commented 3 months ago
### 💻 Website Preview The latest changes are available as preview in: [https://c12ca00c.graphql-yoga.pages.dev](https://c12ca00c.graphql-yoga.pages.dev)
github-actions[bot] commented 3 months ago

✅ 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   
github-actions[bot] commented 3 months ago

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:

github-actions[bot] commented 3 months ago

🚀 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 ↗︎
enisdenjo commented 3 months ago

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

@EmrysMyrddin why?