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 docker image #126

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
docs/
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,27 @@ app.use(router.allowedMethods());
app.listen(3001);
```

## Docker image

### Build

```
$ yarn install
$ yarn run build:release
```

```
$ docker build . -f docker-image/Dockerfile -t apisguru/graphql-voyager
$ docker push apisguru/graphql-voyager
```

### Configuration

You can config docker image with environment variable

- `PORT` to change listen port
- `GRAPHQL_ENDPOINT` address of graphql API
- `HEADER` headers to add to request, formated as 'header1=value1&header2=value2'

harobed marked this conversation as resolved.
Show resolved Hide resolved
## Credits
This tool is inspired by [graphql-visualizer](https://github.com/NathanRSmith/graphql-visualizer) project.
26 changes: 26 additions & 0 deletions docker-image/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM node:11.14.0-alpine

RUN apk add \
ca-certificates \
musl-dev

ADD dist/ /graphql-voyager/dist/
ADD middleware/ /graphql-voyager/middleware/
ADD src/ /graphql-voyager/src/
ADD package.json /graphql-voyager/package.json
ADD docker-image/package.json /graphql-voyager/docker-image/package.json
ADD docker-image/package-lock.json /graphql-voyager/docker-image/package-lock.json

RUN npm install

ADD docker-image/index.js /graphql-voyager/docker-image/index.js

WORKDIR /graphql-voyager/

WORKDIR /graphql-voyager/docker-image/
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line makes the previous one unnecessary


RUN npm install

ENV PORT=3001

CMD node index.js
41 changes: 41 additions & 0 deletions docker-image/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const express = require('express');
const voyager = require('graphql-voyager/middleware');

const app = express();

const headersJS = (() => {
if (!process.env.HEADERS || typeof process.env.HEADERS !== 'string') {
return JSON.stringify({});
};
const headers = {};

process.env.HEADERS.split('&').forEach(header => {
let headerSplit = header.split('=');

if (headerSplit.length === 2) {
headers[headerSplit[0]] = headerSplit[1];
}
});
return JSON.stringify(headers);
})()

app.use('/', voyager.express({
endpointUrl: process.env.GRAPHQL_ENDPOINT,
headersJS: headersJS
}));

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

const port = process.env.PORT;

app.listen(port, function(err) {
if (err) {
throw new Error(
`Failed to start listening on ${port}, error: ${err.message}`
);
}
console.log(`listening on http://0.0.0.0:${port}`);
});
Loading