Skip to content

feat(response-cache): isPersistedDocumentContext helper (using weakmap) #3955

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

n1ru4l
Copy link
Collaborator

@n1ru4l n1ru4l commented Apr 3, 2025

Closes #3951

Add helper function isPersistedDocumentContext.

This function can help you determine whether the GraphQL execution is done within the context of a
persisted document and for example apply additional plugins or security measures conditionally.

Usage Example: Enable max depth rule conditionally

import { createYoga } from 'graphql-yoga'
import { maxDepthRule } from '@escape.tech/graphql-armor-max-depth'
import { usePersistedOperations, isPersistedDocumentContext } from '@graphql-yoga/plugin-persisted-operations'
import schema from './schema.js'
import store from './store.js'

const yoga = createYoga({
  plugins: [
    usePersistedOperations({
      getPersistedOperation(key: string) {
        return store.get(key) || null
      },
      allowArbitraryOperations: true
    }),
    {
      onValidate(ctx) {
        if (isPersistedDocumentContext(ctx.context)) {
          return
        }

        ctx.addValidationRule(
          maxDepthRule({
            n: 20
          })
        )
      }
    }
  ],
  schema
})

Copy link
Contributor

github-actions bot commented Apr 3, 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 Apr 3, 2025

💻 Website Preview

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

Copy link
Contributor

github-actions bot commented Apr 3, 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% ✓ 462490      ✗ 0     
     data_received................................: 1.9 GB  13 MB/s
     data_sent....................................: 93 MB   619 kB/s
     http_req_blocked.............................: avg=1.5µs    min=922ns    med=1.33µs   max=271.52µs p(90)=1.97µs   p(95)=2.15µs  
     http_req_connecting..........................: avg=2ns      min=0s       med=0s       max=134.09µs p(90)=0s       p(95)=0s      
     http_req_duration............................: avg=430.71µs min=275.71µs med=396.45µs max=18.74ms  p(90)=559.02µs p(95)=581.93µs
       { expected_response:true }.................: avg=430.71µs min=275.71µs med=396.45µs max=18.74ms  p(90)=559.02µs p(95)=581.93µs
     ✓ { mode:graphql-jit }.......................: avg=373.11µs min=286.22µs med=356.26µs max=18.74ms  p(90)=390.01µs p(95)=404.14µs
     ✓ { mode:graphql-no-parse-validate-cache }...: avg=583.93µs min=473.07µs med=560.95µs max=7.33ms   p(90)=603.74µs p(95)=646.68µs
     ✓ { mode:graphql-response-cache }............: avg=429.91µs min=334.6µs  med=412.59µs max=7.1ms    p(90)=445.98µs p(95)=458.92µs
     ✓ { mode:graphql }...........................: avg=447.28µs min=339.49µs med=417.55µs max=17.97ms  p(90)=473.41µs p(95)=522.69µs
     ✓ { mode:uws }...............................: avg=363.45µs min=275.71µs med=344.83µs max=6.24ms   p(90)=379.9µs  p(95)=404.5µs 
     http_req_failed..............................: 0.00%   ✓ 0           ✗ 231245
     http_req_receiving...........................: avg=33.02µs  min=16.85µs  med=32.55µs  max=2.9ms    p(90)=39.25µs  p(95)=41.81µs 
     http_req_sending.............................: avg=8.69µs   min=5.68µs   med=7.64µs   max=1ms      p(90)=11.11µs  p(95)=12.17µs 
     http_req_tls_handshaking.....................: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting.............................: avg=388.98µs min=243.65µs med=355.44µs max=18.63ms  p(90)=516.65µs p(95)=537.93µs
     http_reqs....................................: 231245  1541.620507/s
     iteration_duration...........................: avg=643.65µs min=451.3µs  med=605.39µs max=19.19ms  p(90)=775.34µs p(95)=803.11µs
     iterations...................................: 231245  1541.620507/s
     vus..........................................: 1       min=1         max=1   
     vus_max......................................: 2       min=2         max=2   

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