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: [server] Add new onOperation hook #3734

Closed
wants to merge 5 commits into from

Conversation

EmrysMyrddin
Copy link
Collaborator

@EmrysMyrddin EmrysMyrddin commented Feb 5, 2025

This PR introduces a new hook onOperation which is called for each operation to handle.

The main purpose of this hook is to customize the graphql operation handling function.

For example, one will be able to wrap the entire graphql execution to give an access to an AsyncLocalStorage context:

function myPlugin(): Plugin {
  const context = new AsyncLocalStorage();
  return {
    onOperation({ operationHandler, setOperationHandler }) {
      const store = { foo: 'bar' }
      setOperationHandler((payload) => context.run(store, operationHandler, payload))
   }
}

Fixes YOGA-2

Copy link

changeset-bot bot commented Feb 5, 2025

🦋 Changeset detected

Latest commit: cdeb918

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

This PR includes changesets to release 24 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-apollo-usage-report 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

@EmrysMyrddin EmrysMyrddin marked this pull request as draft February 5, 2025 14:43
Copy link
Contributor

github-actions bot commented Feb 5, 2025

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 Feb 5, 2025

💻 Website Preview

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

Copy link
Contributor

github-actions bot commented Feb 6, 2025

✅ 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% ✓ 508842      ✗ 0     
     data_received................................: 2.1 GB  14 MB/s
     data_sent....................................: 102 MB  682 kB/s
     http_req_blocked.............................: avg=1.65µs   min=1.02µs   med=1.39µs   max=6.07ms   p(90)=2.13µs   p(95)=2.38µs  
     http_req_connecting..........................: avg=2ns      min=0s       med=0s       max=159.85µs p(90)=0s       p(95)=0s      
     http_req_duration............................: avg=365.22µs min=214.31µs med=333.53µs max=20.9ms   p(90)=478.82µs p(95)=501.03µs
       { expected_response:true }.................: avg=365.22µs min=214.31µs med=333.53µs max=20.9ms   p(90)=478.82µs p(95)=501.03µs
     ✓ { mode:graphql-jit }.......................: avg=288.37µs min=214.31µs med=272.57µs max=20.9ms   p(90)=303.54µs p(95)=316.49µs
     ✓ { mode:graphql-no-parse-validate-cache }...: avg=505.05µs min=416.77µs med=480.99µs max=6.69ms   p(90)=521.43µs p(95)=560.04µs
     ✓ { mode:graphql-response-cache }............: avg=351.82µs min=270.79µs med=334.81µs max=7ms      p(90)=365.93µs p(95)=377.51µs
     ✓ { mode:graphql }...........................: avg=368.45µs min=275.57µs med=341.41µs max=14.57ms  p(90)=389.53µs p(95)=438.2µs 
     ✓ { mode:uws }...............................: avg=351.94µs min=270.87µs med=333.13µs max=6.42ms   p(90)=368.81µs p(95)=391.62µs
     http_req_failed..............................: 0.00%   ✓ 0           ✗ 254421
     http_req_receiving...........................: avg=34.19µs  min=16.95µs  med=33.54µs  max=3.18ms   p(90)=40.61µs  p(95)=44.19µs 
     http_req_sending.............................: avg=9.14µs   min=5.98µs   med=8.19µs   max=359.82µs p(90)=11.71µs  p(95)=12.97µs 
     http_req_tls_handshaking.....................: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting.............................: avg=321.88µs min=183.3µs  med=291.54µs max=20.76ms  p(90)=434.58µs p(95)=455.69µs
     http_reqs....................................: 254421  1696.124416/s
     iteration_duration...........................: avg=584.4µs  min=393.07µs med=548.35µs max=21.49ms  p(90)=700.91µs p(95)=727.45µs
     iterations...................................: 254421  1696.124416/s
     vus..........................................: 1       min=1         max=1   
     vus_max......................................: 2       min=2         max=2   

@EmrysMyrddin EmrysMyrddin marked this pull request as ready for review February 6, 2025 12:45
Copy link
Collaborator

@ardatan ardatan left a comment

Choose a reason for hiding this comment

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

I'm a bit concerned about other hooks like onExecutionResult and onParams with this hook. But I think it is ok since we will use this to wrap the logic.
Should we also document this?

@EmrysMyrddin
Copy link
Collaborator Author

Yes, we can add a note about this fact, and that the recommended usage is for wrapping.

The behavior is also coherent with onRequest hook. If you replace the request handler there, all other hooks will be skipped too.

@EmrysMyrddin
Copy link
Collaborator Author

Closed in favor of #3736

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.

2 participants