Graphql-code-generator: Document how to use environment variables in `.yml` config file

Created on 18 Apr 2019  路  25Comments  路  Source: dotansimha/graphql-code-generator

Is your feature request related to a problem? Please describe.

Hardcoding YOUR-TOKEN-HERE when specifying the schema with a URL may require checking secrets in to version control (in codegen.yml) to use graphql-codegen on a CI server.

Describe the solution you'd like

Be able to pass headers in via the CLI, or support variables in YAML.

Describe alternatives you've considered

  1. Checking in codegen.yml with a placeholder, and using sed to insert the secret.
  2. Using the --header switch.

Additional context

It appears a --header switch was supported, but was removed on 0.14.0 馃槥

Prisma support variables in YAML like this.

docs

Most helpful comment

I don't even know who created yaml format instead of just using json. What a bad invention, which leads to cascade bad practices :(

All 25 comments

I don't even know who created yaml format instead of just using json. What a bad invention, which leads to cascade bad practices :(

@revskill10 We support JSON config as well :)
@dukedave you can use environment variables to pass it, see this: https://github.com/dotansimha/graphql-code-generator/pull/1368

@dotansimha great! Should I do a PR to update the docs?

@dukedave It would be great :)

headers: 
  "${x-hasura-admin-secret:MYKEY}"
  "${content-type:application/json}"

??

Help me pls.

Docs are updated now: https://graphql-code-generator.com/docs/getting-started/codegen-config#environment-variables

@Pomazan-Bogdan you should use:

headers: 
  x-hasura-admin-secret: ${MYKEY}
  content-type: ${CONTENT_TYPE:application/json}
headers: 
  x-hasura-admin-secret: ${MYKEY}
  content-type: ${CONTENT_TYPE:application/json}

@dotansimha
Not Work(
TypeError: Cannot create property 'source' on string 'Unable to download schema from remote: Missing Authorization header in JWT authentication mode'

image

overwrite: true
schema: https://graphql.ecoalition.org.ua/v1/graphql
headers:
  - x-hasura-admin-secret: {1PocfEqWXPPMX***}
  - content-type: ${CONTENT_TYPE:application/json}
documents: "src/**/*.graphql"
generates:
  src/graphql/index.tsx:
    plugins:
      - typescript
      - typescript-operations
      - typescript-react-apollo
  src/graphql/fragments.tsx:
    plugins:
      - fragment-matcher

@Pomazan-Bogdan it looks like you need to specify Authorization header but you didn't... please make sure what is the name of the headers that you need to pass.

@dotansimha ))
x-hasura-admin-secret

@Pomazan-Bogdan can you try this way?

headers:
  x-hasura-admin-secret: {1PocfEqWXPPMX***}
  content-type: ${CONTENT_TYPE:application/json}

@dotansimha
not work too

@Pomazan-Bogdan my bad, can you try this? also, does the actual values works (without env vars)?

schema:
  - https://graphql.ecoalition.org.ua/v1/graphql:
      headers:
        x-hasura-admin-secret: {1PocfEqWXPPMX***}
        content-type: ${CONTENT_TYPE:application/json}

also, which version of the codegen do you use?

@dotansimha
TypeError: Cannot create property 'source' on string 'Unable to download schema from remote: invalid x-hasura-admin-secret/x-hasura-access-key'

=
{ "errors": [ { "extensions": { "path": "$", "code": "access-denied" }, "message": "invalid x-hasura-admin-secret/x-hasura-access-key" } ] }

schema:
  - https://graphql.ecoalition.org.ua/v1/graphql:
      headers:
        x-hasura-admin-secret: {1PocfEqWX*****}
        content-type: ${CONTENT_TYPE:application/json}

"@graphql-codegen/cli": "^1.4.0", 袨_芯
"gen:graphql": "gql-gen --config codegen.yml",

Can you try with the explicit values?

schema:
  - https://graphql.ecoalition.org.ua/v1/graphql:
      headers:
        X-Hasura-Admin-Secret: PUT_SECRET_HERE
        Content-Type: application/json

also, does an introspection request works for you? with tools like GraphQL Playground (or Postman)

@dotansimha
This format works!
Thank.

I think this should be mentioned separately in the documentation.

@Pomazan-Bogdan
It's documents in our website: https://graphql-code-generator.com/docs/getting-started/schema-field#url

add to documents line "Content-Type: application/json" as sample?)

I mean that I saw the documentation, but I couldn鈥檛 understand what to do.
Without the second line. and not abstract, not perceived (

@Pomazan-Bogdan The example just shows how to add any header, not specific ones.
I even think you can remove Content-Type: application/json because the HTTP request that the codegen does it an introspection, which is already JSON.

Can someone also please update the docs on how to turn on DEBUG and VERBOSE modes? It currently says to set them as environment variables in the but how to do that is unclear at the moment :) https://graphql-code-generator.com/docs/getting-started/codegen-config/#debug-mode

Setting environmental variables depends on your OS. Let's say on POSIX(Mac, Linux etc) environments you can set an environmental variable only for that command like below;

DEBUG=1 yarn graphql-codegen

Brilliant, thanks!

Was this page helpful?
0 / 5 - 0 ratings