Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Environment variables don't work within the .graphqlrc.yml or .graphqlrc.json #1786

Open
felipeptcho opened this issue Mar 12, 2021 · 5 comments

Comments

@felipeptcho
Copy link

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
@ardatan
Copy link
Collaborator

ardatan commented Mar 12, 2021

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"
          ]
        }
      }
    }
  }
}

@felipeptcho
Copy link
Author

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.

@madeleineostoja
Copy link

I'm having the same issue, also using a .graphqlrc.yml config file and -r dotenv/config, variables are just showing up as undefined

@nzhl
Copy link

nzhl commented Jul 11, 2022

Any progress on this

@seyfer
Copy link

seyfer commented Dec 13, 2022

I also need this functionality to work in my project

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants