Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for headers with an environment variable #19

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ The easiest way to configure your project is by setting an environment variable
export GRAPHQL_ENDPOINT="https://your.api/graphql"
```

You can also configure headers with the `GRAPHQL_HEADERS` environment variable. The value must be a valid JSON string.

```sh
export GRAPHQL_HEADERS="{\"Authorization\":\"xxxxx\"}"
```

### Method 2: Configuration via `.graphqlrc` file

You can either use your actual GraphQL endpoint or if preferred a local schema.json or schema.js file.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-config-parser",
"version": "1.2.0",
"version": "1.2.1",
"description": "The easiest way to configure your development environment with your GraphQL schema (supported by most tools, editors & IDEs)",
"main": "src/index.js",
"files": [
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ export function parse (path: string = process.cwd()): Config {
throw new Error(`No valid GraphQL endpoint: ${endpoint}`)
}

let headers
if (process.env.hasOwnProperty('GRAPHQL_HEADERS')) {
try {
headers = JSON.parse(process.env.GRAPHQL_HEADERS)
} catch (ex) {
throw new Error(`GraphQL headers are not valid JSON`)
}
}
return {
url: endpoint,
type: 'request',
headers,
} as ConfigRequest
}

Expand Down
1 change: 1 addition & 0 deletions tests/env/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test.before(async t => {

test(async (t) => {
process.env['GRAPHQL_ENDPOINT'] = 'http://localhost:33333'
process.env['GRAPHQL_HEADERS'] = '{"authorization":"xxxxx"}'

const config = parse()
const resolvedSchema = await resolveSchema(config)
Expand Down