Skip to content

Commit 35f3010

Browse files
authored
Merge pull request #1 from harobed/44-add-headers-to-docker-image
Add HEADERS environment variable to allow header in graphql req…
2 parents ab1efcf + 2b10f99 commit 35f3010

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ app.use(router.allowedMethods());
220220
app.listen(3001);
221221
```
222222

223-
## Build Docker Image
223+
## Docker image
224+
225+
### Build
224226

225227
```
226228
$ npm install
@@ -232,5 +234,13 @@ $ docker build . -f docker-image/Dockerfile -t apisguru/graphql-voyager
232234
$ docker push apisguru/graphql-voyager
233235
```
234236

237+
### Configuration
238+
239+
You can config docker image with environment variable
240+
241+
- `PORT` to change listen port
242+
- `GRAPHQL_ENDPOINT` address of graphql API
243+
- `HEADER` headers to add to request, formated as 'header1=value1&header2=value2'
244+
235245
## Credits
236246
This tool is inspired by [graphql-visualizer](https://github.com/NathanRSmith/graphql-visualizer) project.

docker-image/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,26 @@ const voyager = require('graphql-voyager/middleware');
33

44
const app = express();
55

6-
app.use('/', voyager.express({ endpointUrl: process.env.GRAPHQL_ENDPOINT }));
6+
const headersJS = (() => {
7+
if (!process.env.HEADERS || typeof process.env.HEADERS !== 'string') {
8+
return {};
9+
};
10+
const headers = {};
11+
12+
process.env.HEADERS.split('&').forEach(header => {
13+
let headerSplit = header.split('=');
14+
15+
if (headerSplit.length === 2) {
16+
headers[headerSplit[0]] = headerSplit[1];
17+
}
18+
});
19+
return JSON.stringify(headers);
20+
})()
21+
22+
app.use('/', voyager.express({
23+
endpointUrl: process.env.GRAPHQL_ENDPOINT,
24+
headersJS: headersJS
25+
}));
726

827
process.on('SIGINT', function () {
928
console.log("\nGracefully shutting down from SIGINT (Ctrl-C)");

0 commit comments

Comments
 (0)