Graphql-code-generator: Update Document How To Use Headers in `.yml` file

Created on 29 May 2019  ·  10Comments  ·  Source: dotansimha/graphql-code-generator

Describe the bug

To Reproduce
Steps to reproduce the behavior:

  1. Example codegen.yml config file:
schema: 
  - "http://localhost:4000":
      headers:
      Authorization: "Bearer [KEY]"
generates:
  ./src/app/graphql/generated/gql-gen-schema.json:
    plugins:
      - "introspection"

Expected behavior
Headers are parsed and passed.

Additional context
This example is not correct. Headers need to be an object or array.

This works:

schema: 
  - "http://localhost:4000":
      headers: {Authorization: "Bearer [KEY]"}
generates:
  ./src/app/graphql/generated/gql-gen-schema.json:
    plugins:
      - "introspection"

Please update docs.
Thanks,

docs

Most helpful comment

I mean combining these two;
https://graphql-code-generator.com/docs/getting-started/codegen-config#environment-variables
and
https://graphql-code-generator.com/docs/getting-started/schema-field#url

schema: 
  - "http://localhost:4000":
      headers:
      Authorization: "Bearer ${AUTH_TOKEN}"

So you can pass authentication token using env variables like;

AUTH_TOKEN=... graphql-codegen ...

All 10 comments

Also it would be really great to see an example of how to use this in a the programmatic API. Bearer token key's in our case will change very often, so we programmaticaly retrieve them, and need to feed them into the codegen function (https://graphql-code-generator.com/docs/getting-started/programmatic-usage). How to do that, would be a great thing to include in the docs!

@jcmoore0 The example in https://graphql-code-generator.com/docs/getting-started/schema-field#url is valid, just make sure you file indentation is valid (because i effects yml files).

You example has missing spaces before Authorization.

@KyleMcNutt The programatic usage accepts GraphQLSchema as an input, so it has anything to do with headers.
You can use loadSchema from graphql-toolkit in order to load your schema from a url (and pass headers).

@dotansimha Then the docs should be updated to make that more clear. As is, I assumed the schema field in the programmatic API would take any of the same params as the schema prop in the YAML file.

So far with every example I've tried I can't get the headers to work. Who has this working that can share their yaml file?

@swillis12 can you please share your codegen.yml file? It's tricky because you need to verify your whitespaces in YAML files.

Is there anyway to pass the Authorization key from command line?

You can use env variables @kuncevic

You can use env variables @kuncevic

Can you elaborate more on that?
Here is more input - I have npm script:

”gql:introspect": "npm run generate -- --config projects/shared-library/src/lib/graphql/config/introspect.yml”

my introspect.yml contain an Authorization token
Authorization: "Bearer xyz..."

So, I want somehow to pass that token trough the npm script instead having that hard coded in my introspect.yml file

I mean combining these two;
https://graphql-code-generator.com/docs/getting-started/codegen-config#environment-variables
and
https://graphql-code-generator.com/docs/getting-started/schema-field#url

schema: 
  - "http://localhost:4000":
      headers:
      Authorization: "Bearer ${AUTH_TOKEN}"

So you can pass authentication token using env variables like;

AUTH_TOKEN=... graphql-codegen ...

@ardatan great, thanks for your help!

Was this page helpful?
0 / 5 - 0 ratings