graphql-cli
graphql-cli copied to clipboard
Environment variables don't work within the .graphqlrc.yml or .graphqlrc.json
Describe the bug
Environment variables don't work within the .graphqlrc.yml
or .graphqlrc.json
config file. We have the option to pass --require dotenv/config
to the codegen command, but it doesn't work because codegen will only attempt to load the config file if it was not loaded before by graphql-cli. It would be nice if the graphql-cli could also read the --require
parameter to load the specified modules before loading the configuration.
To Reproduce
.env
GRAPHQL_ENDPOINT=https://myproject.mydomain.com/v1/graphql
ADMIN_SECRET=123456
.graphqlrc.json
{
"schema": [
{
"${GRAPHQL_ENDPOINT}": {
"headers": {
"ADMIN-SECRET": "${ADMIN_SECRET}"
}
}
}
],
"extensions": {
"codegen": {
"generates": {
"./build/graphql/schema.graphql": {
"plugins": [
"schema-ast"
]
}
}
}
}
}
Command line
$ graphql codegen --require dotenv/config
Result: Environment variables are loaded as undefined
.
Expected behavior Environment variables should be loaded with the correct values from the .env file.
Versions (please complete the following information):
- OS:
OS X Big Sur
-
graphql-cli
:4.1.0
-
graphql
:15.5.0
-
@graphql-cli/codegen
:1.17.21
-
@graphql-codegen/schema-ast
:1.18.1
-
dotenv
:8.2.0
I think ${}
syntax isn't supported with JSON files so you can use YAML files or JS files. Could you create a production for YAML files?
module.exports = {
"schema": [
{
[process.env.GRAPHQL_ENDPOINT]: {
"headers": {
"ADMIN-SECRET": process.env['ADMIN-SECRET']
}
}
}
],
"extensions": {
"codegen": {
"generates": {
"./build/graphql/schema.graphql": {
"plugins": [
"schema-ast"
]
}
}
}
}
}
Hi @ardatan
Thanks for your comment.
I also tried the YAML format. It doesn't work either. =/
The problem is that the environment variables are not being loaded into process.env
because dotenv/config
is not being invoked. If I add them manually in the terminal, it works. So, it's not a problem with the file format itself.
I debugged graphql-cli
and graphql-config
and it seems they don't have the code to require the modules specified in the --required
parameter as the @graphql-codegen/cli
has: https://github.com/dotansimha/graphql-code-generator/blob/master/packages/graphql-codegen-cli/src/config.ts#L183
So, when I pass --require dotenv/config
, it doesn't load the variables into the environment.
As I said before, the codegen command should be able to read the require
parameter, but it won't. It skips this part because the config is already loaded by graphql-cli
.
I'm having the same issue, also using a .graphqlrc.yml
config file and -r dotenv/config
, variables are just showing up as undefined
Any progress on this
I also need this functionality to work in my project