Graphql-code-generator: Send authorization token with generated service

Created on 20 Jun 2019  路  3Comments  路  Source: dotansimha/graphql-code-generator

Is it possible to send an authorization token with the generated services? What I would like to do is send user's authorization token with each request to the GraphQL server so that I can get back only the data which this user should see.

Most helpful comment

You can generate it using a script, then set it as an environmental variable. Then use this env variable to send to codegen as an Authorization header.
Or you can use codegen programatically to generate a token
https://graphql-code-generator.com/docs/getting-started/schema-field#url

All 3 comments

Hi @rogerg93
It has nothing to do with the codegen, it's related only to the runtime library you use in your client-side (apollo/relay/url, each one has a different way of implementing auth).

The codegen only generates types or code for you, and has nothing to do with authentication.

I'm trying to use my production schema in order to generate a local introspection schema for the Apollo introspection fragment matcher, but the graphql endpoint is protected by authentication. How can I send my authentication token, in a header, along with the generate command?

You can generate it using a script, then set it as an environmental variable. Then use this env variable to send to codegen as an Authorization header.
Or you can use codegen programatically to generate a token
https://graphql-code-generator.com/docs/getting-started/schema-field#url

Was this page helpful?
0 / 5 - 0 ratings