Graphcool / graphcool-framework

Apache License 2.0
1.77k stars 131 forks source link

Improve the environment variable experience #271

Open marktani opened 6 years ago

marktani commented 6 years ago

Issue by kbrandwijk Saturday Oct 14, 2017 at 10:57 GMT Originally opened as https://github.com/graphcool/prisma/issues/810


What feature are you missing? An easy way to work with stage-specific environment variables.

How could this feature look like in detail? Tradeoffs? I proposed the dotenv standard a while ago. That idea was discarded because the .graphcoolrc file structure wasn't a good fit for it. With the new structure, I think it's feasible to reconsider that decision.

If I have different secret environment variables for different stages, I can put them in the project file in the latest alpha, which exposes them when using git. So that's not an option. I can also use something like direnv, but that doesn't work well on Windows, and also, I don't have a seperate folder for each stage, so that approach doesn't work well either.

Unless there's another way I'm missing, I still don't have a proper solution for stage-specific env vars.

marktani commented 6 years ago

Comment by maticzav Friday Nov 24, 2017 at 16:06 GMT


Is this available already?

marktani commented 6 years ago

Comment by jonasdumas Tuesday Jan 02, 2018 at 14:00 GMT


+1 Any update about that ?

jhalborg commented 6 years ago

Also interested in this. It seems that dotenv has been adopted in Prisma, but what about graphcool-framework?

jesstelford commented 6 years ago

For future googlers, I've found a solution I wrote up here: https://www.graph.cool/forum/t/how-to-use-environment-variables/1964/7?u=jesstelford

I've figured out how to use a .env file along with the graphcool.yml file for some sweet env support:

  1. Create a .env file in the root of your project, and add some variables:
TWITTER_KEY=XXXXXXX
TWITTER_SECRET=XXXXXXXX
  1. Edit your graphcool.yml to add those env vars:
    functions:
    twitterAuth:
    handler:
      code:
        src: ./src/authentication/twitter.ts
        # Define environment variables to be used in function
        environment:
          TWITTER_KEY: ${env:TWITTER_KEY}
          TWITTER_SECRET: ${env:TWITTER_SECRET}
    type: resolver
    schema: ./src/authentication/twitter.graphql
  2. Install the dotenv npm module: yarn add --dev dotenv/npm install --save-dev dotenv
  3. Load the .env when you run graphcool deploy:
    cd graphcool-dir
    node -r dotenv/config $(which graphcool) deploy dotenv_config_path=../.env
  4. In your code, reference the env vars with process.env.TWITTER_KEY