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

make the context referentialy stable #1911

Merged
merged 3 commits into from
Sep 27, 2023
Merged

Conversation

EmrysMyrddin
Copy link
Collaborator

@EmrysMyrddin EmrysMyrddin commented Jul 12, 2023

Description

Today, when the context is extended via the provided extendContext parameter, the context is mutated or cloned depending on the hook.

This PR aims to unify the behaviour of every hooks by always mutating the context when extending. This avoid to create unnecessary object instances and to have a referentially stable context object.

The reference stability is usefull to allow plugins to reference the context outside of envelop hooks. Here an example of usage with a DAO relying on context for some its methods. Without referential stability, developer will always have to give the context as a parameter.

class DAO extends Client {
	constructor({ context }) {
		this.context = context
	}

	getCurrentUser() {
		return this.getUser(this.context.auth.userId)
	}

	getUser(id) {
		return this.query(sql`SELECT * FROM users WHERE user.id = ${id}`)
	}
}

const daoPlugin = {
	onEveloped({ extendContext, context }) {
		extendContext({ dao: new DAO({ context }) })
	}
}

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as
    expected) => I'm not sure if this is a breaking change. It could, but I think most of the time, this behaviour is the one expected

Checklist:

  • I have followed the
    CONTRIBUTING doc and the
    style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented on my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@changeset-bot
Copy link

changeset-bot bot commented Jul 12, 2023

🦋 Changeset detected

Latest commit: af1cec7

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

This PR includes changesets to release 1 package
Name Type
@envelop/core 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
Copy link
Contributor

github-actions bot commented Jul 12, 2023

🚀 Snapshot Release (alpha)

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

Package Version Info
@envelop/core 4.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/apollo-datasources 3.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/apollo-federation 4.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/apollo-server-errors 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/apollo-tracing 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/auth0 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/dataloader 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/depth-limit 3.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/disable-introspection 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/execute-subscription-event 4.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/extended-validation 3.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/filter-operation-type 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/fragment-arguments 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/generic-auth 6.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/graphql-jit 6.0.3-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/graphql-middleware 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/graphql-modules 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/immediate-introspection 2.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/live-query 6.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/newrelic 6.0.3-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/on-resolve 3.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/opentelemetry 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/operation-field-permissions 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/parser-cache 6.0.3-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/persisted-operations 6.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/preload-assets 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/prometheus 8.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/rate-limiter 5.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/resource-limitations 4.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/response-cache 5.3.1-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/response-cache-redis 3.3.1-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/sentry 6.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/statsd 4.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/validation-cache 6.0.3-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎
@envelop/testing 6.0.2-alpha-20230908154216-af1cec7c npm ↗︎ unpkg ↗︎

@github-actions
Copy link
Contributor

💻 Website Preview

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

@theguild-bot
Copy link
Collaborator

theguild-bot commented Jul 12, 2023

✅ Benchmark Results

     ✓ no_errors
     ✓ expected_result

     checks.............................................: 100.00% ✓ 625984     ✗ 0     
     data_received......................................: 2.4 GB  16 MB/s
     data_sent..........................................: 136 MB  906 kB/s
     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     
     ✓ { mode:prom-tracing }............................: 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     
     ✓ { mode:prom-tracing }............................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     http_req_blocked...................................: avg=1.85µs  min=600ns    med=1.1µs   max=9.72ms  p(90)=1.6µs   p(95)=2µs    
     http_req_connecting................................: avg=178ns   min=0s       med=0s      max=9ms     p(90)=0s      p(95)=0s     
     http_req_duration..................................: avg=4.47ms  min=180.39µs med=3.35ms  max=79.92ms p(90)=9.22ms  p(95)=15.76ms
       { expected_response:true }.......................: avg=4.47ms  min=180.39µs med=3.35ms  max=79.92ms p(90)=9.22ms  p(95)=15.76ms
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: avg=4.04ms  min=399.69µs med=3.57ms  max=39.17ms p(90)=6.53ms  p(95)=7.77ms 
     ✓ { mode:envelop-cache-jit }.......................: avg=2.25ms  min=180.39µs med=1.68ms  max=33.74ms p(90)=4.05ms  p(95)=7.27ms 
     ✓ { mode:envelop-just-cache }......................: avg=4.01ms  min=395.19µs med=3.52ms  max=79.92ms p(90)=6.61ms  p(95)=7.87ms 
     ✓ { mode:graphql-js }..............................: avg=6.63ms  min=643.99µs med=5.68ms  max=75.85ms p(90)=10.72ms p(95)=12.36ms
     ✓ { mode:prom-tracing }............................: avg=18.65ms min=3.03ms   med=16.66ms max=73.73ms p(90)=32.55ms p(95)=33.48ms
     http_req_failed....................................: 0.00%   ✓ 0          ✗ 312992
     http_req_receiving.................................: avg=40.79µs min=11.1µs   med=18.09µs max=26.21ms p(90)=25.8µs  p(95)=32µs   
     http_req_sending...................................: avg=29.78µs min=4.1µs    med=6.4µs   max=26.46ms p(90)=11.5µs  p(95)=16.7µs 
     http_req_tls_handshaking...........................: avg=0s      min=0s       med=0s      max=0s      p(90)=0s      p(95)=0s     
     http_req_waiting...................................: avg=4.4ms   min=152.39µs med=3.28ms  max=75.75ms p(90)=8.9ms   p(95)=15.71ms
     http_reqs..........................................: 312992  2086.39894/s
     iteration_duration.................................: avg=4.78ms  min=372.59µs med=3.67ms  max=80.48ms p(90)=9.76ms  p(95)=16.05ms
     iterations.........................................: 312992  2086.39894/s
     vus................................................: 10      min=10       max=20  
     vus_max............................................: 20      min=20       max=20  

@EmrysMyrddin EmrysMyrddin force-pushed the fix/referential-stability branch from 88050dc to 935a638 Compare September 5, 2023 08:24
@EmrysMyrddin EmrysMyrddin force-pushed the fix/referential-stability branch from fdaa353 to 353bd4c Compare September 8, 2023 15:39
@ardatan ardatan merged commit dee6b8d into main Sep 27, 2023
@ardatan ardatan deleted the fix/referential-stability branch December 2, 2024 09:21
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.

3 participants