dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.84k stars 1.33k forks source link

Document how to use environment variables in `.yml` config file #1737

Closed davetapley closed 5 years ago

davetapley commented 5 years ago

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.

revskill10 commented 5 years ago

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

dotansimha commented 5 years ago

@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

davetapley commented 5 years ago

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

dotansimha commented 5 years ago

@dukedave It would be great :)

pomazanbohdan commented 5 years ago
headers: 
  "${x-hasura-admin-secret:MYKEY}"
  "${content-type:application/json}"

??

Help me pls.

dotansimha commented 5 years ago

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}
pomazanbohdan commented 5 years ago
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

pomazanbohdan commented 5 years ago
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
dotansimha commented 5 years ago

@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.

pomazanbohdan commented 5 years ago

@dotansimha )) x-hasura-admin-secret

dotansimha commented 5 years ago

@Pomazan-Bogdan can you try this way?

headers:
  x-hasura-admin-secret: {1PocfEqWXPPMX***}
  content-type: ${CONTENT_TYPE:application/json}
pomazanbohdan commented 5 years ago

@dotansimha not work too

dotansimha commented 5 years ago

@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?

pomazanbohdan commented 5 years ago

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

pomazanbohdan commented 5 years ago

= { "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}
pomazanbohdan commented 5 years ago

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

dotansimha commented 5 years ago

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)

pomazanbohdan commented 5 years ago

@dotansimha This format works! Thank.

pomazanbohdan commented 5 years ago

I think this should be mentioned separately in the documentation.

dotansimha commented 5 years ago

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

pomazanbohdan commented 5 years ago

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

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

dotansimha commented 5 years ago

@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.

thendra commented 4 years ago

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

ardatan commented 4 years ago

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
thendra commented 4 years ago

Brilliant, thanks!

ian-maurmann commented 3 years ago

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

Sidenote for the history books: Yaml was released in 2001. Douglas Crockford released json in 2002.

mvilrokx commented 3 years ago

I know this issue is closed but if somebody lands here to figure out why their Headers aren't picked up, even though you configured them in your .yml config file, it seems that correct indentation is the key to get this working. I am using Hasura and require a x-hasura-admin-secret Header in my setup but I could not get it to work, no matter what instructions I followed (which is how I ended up here). The only thing that worked for me was to have 2 tabs indentation in front of headers, i.e. this works:

schema:
  - ${SCHEMA_PATH}:
      headers:
        x-hasura-admin-secret: ${HASURA_ADMIN_SECRET}

This does not work:

schema:
  - ${SCHEMA_PATH}:
    headers:
      x-hasura-admin-secret: ${HASURA_ADMIN_SECRET}

This would produce Failed to load schema from http://localhost:8080/v1/graphql,headers:

As you can see, the actual value of headers is missing.

wzulfikar commented 3 years ago

@mvilrokx I've been fiddling with this for the last 30 minutes and the fix was actually to indent the headers just like you said. This is what worked for me: image