apollo-server-integrations / apollo-server-integration-cloudflare-workers

An integration to use Cloudflare Workers as a hosting service with Apollo Server
https://github.com/kimyvgy/worker-apollo-server-template
MIT License
13 stars 0 forks source link

Feature Request: Ability to pass Env down from the worker handler to the context #77

Closed ashwinv11 closed 3 months ago

ashwinv11 commented 8 months ago

First off, thank you for creating this!

It would be very useful if we could pass additional information like environment variables to the CloudflareWorkersHandler to the handleGraphQLRequest function. This would allow us to reference sensitive info like DB connection fields, the Cloudflare KV Namespace that's bound to the worker, and much more.

This would allow us to reference connections to persistence layers in DataSource classes within the context object.

Is there some way to do this today that I might be missing? Thank you!

kimyvgy commented 8 months ago

@ashwinv11 Currently, you can create the file bindings.d.ts for binding KV Namespace... For example: https://github.com/kimyvgy/worker-apollo-server-template/blob/main/bindings.d.ts

ashwinv11 commented 8 months ago

Thanks @kimyvgy! I actually found your repo and tried that out, however I'm getting a Context creation failed: GRAPHQL_KV_CACHE is not defined error when attempting to use the bindings.d.ts. When I clone and deploy your template I don't see that issue so must be something else I'm missing. I'll keep investigating and update here

ashwinv11 commented 8 months ago

Ah I think I understand why now, I'm writing my worker code using the newer ESM syntax vs the Service Worker format. With the former, env variables are no longer accessible via globals and looks like they must be passed down from the handler context. Would it be possible to consider adding this feature for the newer syntax style?

simplenotezy commented 3 months ago

I have the same issue here; I need to pass in env in a handler. Did you ever figure out a solution to this @ashwinv11?

kimyvgy commented 3 months ago

@simplenotezy @ashwinv11 The PR #80 solved this issue. Please upgrade your app to ^v1.0.0.

simplenotezy commented 3 months ago

Nice, thanks @kimyvgy - is it as simple as calling the global variable? Or could you share som example of how to grab the env