n1ru4l / envelop

Envelop is a lightweight library allowing developers to easily develop, share, collaborate and extend their GraphQL execution layer. Envelop is the missing GraphQL plugin system.
https://envelop.dev
MIT License
779 stars 126 forks source link

[response-cache] avoid aliased when present #2266

Closed EmrysMyrddin closed 2 months ago

EmrysMyrddin commented 2 months ago

Description

To achieve automatique cache invalidation, the plugins needs __typename and id (or equivalent) fields to be present in the response. To ensure this rule, it adds an aliased __typename selection on the fly to the operation document.

While this works well, it have the down side of adding a lot of noise to the resulting document, making it sometimes significantly bigger than the original one. This can lead to exceed HTTP body limits in some cases when used on Mesh.

This PR mitigate this issue by adding aliased fields only when they are not already selected, reducing the noise in most cases since the __typename is often already selected by most graphql client for caching purpose.

Type of change

Please delete options that are not relevant.

changeset-bot[bot] commented 2 months ago

🦋 Changeset detected

Latest commit: f62b9a22b23c25b6f97c9883331645d9f7d64f17

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

This PR includes changesets to release 1 package | Name | Type | | ----------------------- | ----- | | @envelop/response-cache | 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 2 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
@envelop/response-cache 6.2.1-alpha-20240716144028-f62b9a22 npm ↗︎ unpkg ↗︎
github-actions[bot] commented 2 months ago
### 💻 Website Preview The latest changes are available as preview in: [https://281da2ee.envelop.pages.dev](https://281da2ee.envelop.pages.dev)
theguild-bot commented 2 months ago

✅ Benchmark Results

     ✓ no_errors
     ✓ expected_result

     checks.............................................: 100.00% ✓ 923840      ✗ 0     
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: 100.00% ✓ 219374      ✗ 0     
     ✓ { mode:envelop-cache-jit }.......................: 100.00% ✓ 356742      ✗ 0     
     ✓ { mode:envelop-just-cache }......................: 100.00% ✓ 219142      ✗ 0     
     ✓ { mode:graphql-js }..............................: 100.00% ✓ 128582      ✗ 0     
     data_received......................................: 3.5 GB  29 MB/s
     data_sent..........................................: 201 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=2.02µs  min=721ns    med=1.81µs  max=10.14ms  p(90)=2.41µs  p(95)=2.71µs 
     http_req_connecting................................: avg=12ns    min=0s       med=0s      max=784.48µs p(90)=0s      p(95)=0s     
     http_req_duration..................................: avg=2.3ms   min=158.28µs med=2.01ms  max=78.53ms  p(90)=4.14ms  p(95)=4.67ms 
       { expected_response:true }.......................: avg=2.3ms   min=158.28µs med=2.01ms  max=78.53ms  p(90)=4.14ms  p(95)=4.67ms 
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: avg=2.44ms  min=344.39µs med=2.16ms  max=15.29ms  p(90)=4.26ms  p(95)=4.62ms 
     ✓ { mode:envelop-cache-jit }.......................: avg=1.38ms  min=158.28µs med=1.07ms  max=16.06ms  p(90)=2.18ms  p(95)=2.37ms 
     ✓ { mode:envelop-just-cache }......................: avg=2.43ms  min=402.91µs med=2.13ms  max=28.78ms  p(90)=4.24ms  p(95)=4.61ms 
     ✓ { mode:graphql-js }..............................: avg=4.39ms  min=565.77µs med=3.72ms  max=78.53ms  p(90)=7.37ms  p(95)=8.06ms 
     http_req_failed....................................: 0.00%   ✓ 0           ✗ 461920
     http_req_receiving.................................: avg=31.17µs min=12.6µs   med=26.93µs max=15.58ms  p(90)=41.7µs  p(95)=46.21µs
     http_req_sending...................................: avg=12.52µs min=4.12µs   med=10.34µs max=7.38ms   p(90)=15.57µs p(95)=19.96µ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=128.21µs med=1.97ms  max=78.41ms  p(90)=4.09ms  p(95)=4.62ms 
     http_reqs..........................................: 461920  3849.095446/s
     iteration_duration.................................: avg=2.59ms  min=350.39µs med=2.29ms  max=79.03ms  p(90)=4.42ms  p(95)=4.99ms 
     iterations.........................................: 461920  3849.095446/s
     vus................................................: 10      min=10        max=10  
     vus_max............................................: 20      min=20        max=20