envelop icon indicating copy to clipboard operation
envelop copied to clipboard

enhance(response-cache/cfw-kv): simplify cache init

Open ardatan opened this issue 1 year ago • 4 comments

This PR introduces the following changes;

  • Now cache parameter accepts a factory function that takes the context and returns the cache implementation
  • Now the cache implementation does not require any ctx value, but only the name of the KV namespace.

See the changes in the README for details

import { createSchema, createYoga, YogaInitialContext } from 'graphql-yoga'
import { useResponseCache } from '@envelop/response-cache'
import { createKvCache } from '@envelop/response-cache-cloudflare-kv'
import { resolvers } from './graphql-schema/resolvers.generated'
import { typeDefs } from './graphql-schema/typeDefs.generated'

export type Env = {
  GRAPHQL_RESPONSE_CACHE: KVNamespace
}

const graphqlServer = createYoga<Env & ExecutionContext>({
  schema: createSchema({ typeDefs, resolvers }),
  plugins: [
    useResponseCache({
      cache: createKvCache({
        KVName: 'GRAPHQL_RESPONSE_CACHE',
        keyPrefix: 'graphql' // optional
      }),
      session: () => null,
      includeExtensionMetadata: true,
      ttl: 1000 * 10 // 10 seconds
    })
  ]
})

export default {
  fetch: graphqlServer
}

ardatan avatar May 20 '24 00:05 ardatan

🦋 Changeset detected

Latest commit: 40868c0a6432244764bb67aeaea5c19df928fd14

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

This PR includes changesets to release 2 packages
Name Type
@envelop/response-cache Minor
@envelop/response-cache-cloudflare-kv Major

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

changeset-bot[bot] avatar May 20 '24 00:05 changeset-bot[bot]

🚀 Snapshot Release (alpha)

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

Package Version Info
@envelop/response-cache 6.2.0-alpha-20240528125003-40868c0a npm ↗︎ unpkg ↗︎
@envelop/response-cache-cloudflare-kv 1.0.0-alpha-20240528125003-40868c0a npm ↗︎ unpkg ↗︎

github-actions[bot] avatar May 20 '24 00:05 github-actions[bot]

💻 Website Preview

The latest changes are available as preview in: https://acf91942.envelop.pages.dev

github-actions[bot] avatar May 20 '24 00:05 github-actions[bot]

✅ Benchmark Results

     ✓ no_errors
     ✓ expected_result

     checks.............................................: 100.00% ✓ 927048     ✗ 0     
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: 100.00% ✓ 217082     ✗ 0     
     ✓ { mode:envelop-cache-jit }.......................: 100.00% ✓ 364688     ✗ 0     
     ✓ { mode:envelop-just-cache }......................: 100.00% ✓ 216392     ✗ 0     
     ✓ { mode:graphql-js }..............................: 100.00% ✓ 128886     ✗ 0     
     data_received......................................: 3.5 GB  30 MB/s
     data_sent..........................................: 202 MB  1.7 MB/s
     envelop_init.......................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     envelop_total......................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     event_loop_lag.....................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     graphql_context....................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     graphql_execute....................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     graphql_parse......................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     graphql_validate...................................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     http_req_blocked...................................: avg=1.93µs  min=692ns    med=1.72µs  max=11.03ms p(90)=2.35µs  p(95)=2.64µs 
     http_req_connecting................................: avg=18ns    min=0s       med=0s      max=1.04ms  p(90)=0s      p(95)=0s     
     http_req_duration..................................: avg=2.3ms   min=128.9µs  med=2.03ms  max=76.5ms  p(90)=4.17ms  p(95)=4.71ms 
       { expected_response:true }.......................: avg=2.3ms   min=128.9µs  med=2.03ms  max=76.5ms  p(90)=4.17ms  p(95)=4.71ms 
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: avg=2.48ms  min=353.83µs med=2.2ms   max=14.95ms p(90)=4.31ms  p(95)=4.69ms 
     ✓ { mode:envelop-cache-jit }.......................: avg=1.35ms  min=128.9µs  med=1.06ms  max=13.6ms  p(90)=2.16ms  p(95)=2.34ms 
     ✓ { mode:envelop-just-cache }......................: avg=2.48ms  min=352.26µs med=2.19ms  max=29.83ms p(90)=4.3ms   p(95)=4.67ms 
     ✓ { mode:graphql-js }..............................: avg=4.38ms  min=622.43µs med=3.68ms  max=76.5ms  p(90)=7.36ms  p(95)=8.05ms 
     http_req_failed....................................: 0.00%   ✓ 0          ✗ 463524
     http_req_receiving.................................: avg=31.3µs  min=12.32µs  med=27.49µs max=12.59ms p(90)=42µs    p(95)=46.83µs
     http_req_sending...................................: avg=12.29µs min=4.14µs   med=10.19µs max=9.14ms  p(90)=14.81µs p(95)=19.02µs
     http_req_tls_handshaking...........................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     http_req_waiting...................................: avg=2.26ms  min=102.58µs med=1.99ms  max=76.41ms p(90)=4.12ms  p(95)=4.66ms 
     http_reqs..........................................: 463524  3862.49141/s
     iteration_duration.................................: avg=2.58ms  min=333.98µs med=2.29ms  max=77.58ms p(90)=4.44ms  p(95)=5.02ms 
     iterations.........................................: 463524  3862.49141/s
     vus................................................: 10      min=10       max=10  
     vus_max............................................: 20      min=20       max=20  

theguild-bot avatar May 20 '24 00:05 theguild-bot

Just to let you know, there is another PR (https://github.com/n1ru4l/envelop/pull/2237) which also updates the CFW cache plugin. It updates types, so it should not be of a huge impact, I probably can do the rebase for you after merging if you want.

EmrysMyrddin avatar May 21 '24 09:05 EmrysMyrddin

You can rebase but also keep in mind, my PR removes the need of the type compatibility. So we don't even need to deal with ReadacleStream conflict in that case.

ardatan avatar May 21 '24 09:05 ardatan

We are still importing the KVNamespace type aren't we ?

You mean that the fact we don't take this as a parameter anymore avoid the typing issue in user land ?

EmrysMyrddin avatar May 21 '24 09:05 EmrysMyrddin

@EmrysMyrddin I believe so but even if not, that is not the main goal of this PR. The main goal is to simplify the usage, move the server definition etc on top, less verbose API etc.

ardatan avatar May 21 '24 09:05 ardatan

I still think it can be good to merge the other PR anyway, pinning version could avoid breaking the plugin on updates ?

EmrysMyrddin avatar May 21 '24 11:05 EmrysMyrddin

@ardatan Is it ok to merge this ?

EmrysMyrddin avatar May 28 '24 08:05 EmrysMyrddin

Sure you can rebase the conflicts and merge!

ardatan avatar May 28 '24 09:05 ardatan