Gato GraphQL logo

Environment Fields

Environment Fields

The GraphQL schema is provided with global field _env, which allows to obtain a value from an environment variable, or from a PHP constant (most commonly defined in wp-config.php, but can also be defined elsewhere).

Due to security reasons, the name of the environment variable and constants that can be accessed must be explicitly configured (explained in Configuring what environment variables and PHP constants can be queried).

Field _env receives the name of the environment variable or constant under parameter "name", and is resolved like this:

  • If there is an environment variable with that name, it returns it
  • Otherwise, if there is a constant with that name, it returns it
  • Otherwise, it returns null and adds an error to the GraphQL output.

For instance, this query retrieves the environment constant GITHUB_ACCESS_TOKEN which we might set-up to access a private repository in GitHub:

query {
  githubAccessToken: _env(name: "GITHUB_ACCESS_TOKEN")
}

Security: Not exposing credentials

Please read guide Security: Avoid exposing credentials used in the query.

Further examples

This query retrieves the DB configuration defined in the wp-config.php file:

query {
  dbName: _env(name: "DB_NAME")
  dbUser: _env(name: "DB_USER")
  dbPassword: _env(name: "DB_PASSWORD")
  dbHost: _env(name: "DB_HOST")
}