dotansimha / dotansimha/graphql-code-generator
Import environment variables from angular to codegen.js
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 1.4k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 23
Description
### Discussed in https://github.com/dotansimha/graphql-code-generator/discussions/7092
Originally posted by **enriqueteles** November 25, 2021
Hello, I'm using the graphql-code-generator in an angular project and I'd like to be able to use the angular environments instead of the dotenv but I don't know how.
The environment file (environment.ts) is something like:
```ts
export const environment = {
uri: 'https://endpoint.com/graphql',
apiKey: 'ourkey'
}
```
I am using a js config file instead of yml (to be easier to import) and I already tried to pass the require `ts-node/register` in the command to generate and in the config file but returns an error `Cannot use import statement outside a module`.
I am able to generate the types and functions (using the typescript-apollo-angular plugin and others) passing the url and apiKey directly in the config file inside the schema but right now I need to use environments variables to increase security and for "best practices".
Here is the config file that is working but needs to import the envs:
```js
module.exports = {
overwrite: true,
config: {
namingConvention: {
transformUnderscore: true
}
},
generates: {
'./src/app/shared/graphql/products/products-graphql.ts': {
schema: {
"https://endpoint.com/graphql": {
headers: {
'x-api-key': 'ourkey'
}
}
},
documents: "src/app/shared/graphql/products/**/*.graphql",
plugins: [
{
"add": {
content: '// This file is generated, Do not edit manually.'
}
},
"typescript",
"typescript-operations",
"typescript-apollo-angular",
"fragment-matcher"
],
config: {
declarationKind: "interface",
namedClient: "products",
},
},
}
}
```
Any help is welcome :grimacing:
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.