Graphql-code-generator: Add support for loading environment variables from a .env file

Created on 22 Apr 2019  路  20Comments  路  Source: dotansimha/graphql-code-generator

Is your feature request related to a problem? Please describe.
Currently environment variables must exist on process.env before running the codegen cli


I'd like to be able to load and reference variables defined in a .env file in the codegen.yml file

Describe the solution you'd like

Using dotenv to either load a .env file if it exists or a cli flag to do so

Describe alternatives you've considered

  • Using the programmatic approach

Additional context

core enhancement waiting-for-release

Most helpful comment

@dotansimha Just checked again and it's not working, using -r dotenv/config and the env variables do not get loaded

All 20 comments

@jflewis We support require extensions, so I think you should be able to do: require: dotenv/config in your codegen.yml and it will work. (https://github.com/motdotla/dotenv#preload)

I also added it to the docs: https://github.com/dotansimha/graphql-code-generator/commit/8a85e15af7c6bb6d1db9af393a33bb3117dde6fc

@dotansimha Sorry for the late response, haven't had an opportunity to take a look at this.

It seems that this doesn't exactly work. While the require: dotenv/config does load the .env file (which is great news), this action is happening _after_ the parsing of the YML. So I am unable to use .env in combination with ENV variables in the yml.

@jflewis Ohh I see, it makes sense, because in order to get require values, we need to load the yml file. Keeping open, we'll try to find a solution for it soon.

@jflewis

It's very ugly but it'd be workaround until this issue fixed.

$ node --require dotenv/config ./node_modules/.bin/graphql-codegen --config codegen.yml

I think that allowing to specify -r to the codegen should do the trick (just like we can set watch from the cli but also from the config file).
This way, if you need something to get loaded before the codegen reads the yml, you can use -r and we'll make sure to load it before. (similar to what @otofu-square suggested).

Available in 1.3.0 馃帀

Stopped working after upgrading to 1.8.1

@lednhatkhanh I guess 1.8.0 doesn't work as well?
@kamilkisiela can you please take a look? maybe it's related to cosmiconfig?

@lednhatkhanh it seems like loading env vars works, can you please share a reproduction in a new issue?

Stopped working after upgrading to 1.8.1

happened to me as well.
it seems like env vars aren't loaded in the yml file even though i'm using --require dotenv/config

@dotansimha Just checked again and it's not working, using -r dotenv/config and the env variables do not get loaded

Thanks, I created a new issue to track this: https://github.com/dotansimha/graphql-code-generator/issues/2722

@lednhatkhanh @cnt0705

Fixed in: https://github.com/dotansimha/graphql-code-generator/pull/2758
Alpha available: 1.8.2-alpha-c7249947.43

@dotansimha I upgraded to the latest @graphql-codegen/cli (1.17.10).
Passing this options used to work in 1.12.1

graphql-codegen -r dotenv/config dotenv_config_path=../../.env

But in 1.17.10 I'm seeing this error:

Cannot find module 'dotenv_config_path=../../.env'

Any input would be greatly appreciated

@johnnyBira 1.12 is pretty old. I'm not sure i'm able to compare that.
You can use DOTENV_CONFIG_PATH env var for that

Can you direct me to the docs for this please? @dotansimha

@johnnyBira
dotenv supports setting configuration via environment variables (see https://github.com/motdotla/dotenv#preload), so instead of using a CLI flag for codegen, you can just set an environment variables that will effect dotenv directly.

This makes sense. Thanks for taking the time explaining @dotansimha

Was this page helpful?
0 / 5 - 0 ratings