graphql sandbox & server

This commit is contained in:
2023-05-17 16:57:30 -07:00
parent 5917c35710
commit 1d5056139b
9 changed files with 1793 additions and 0 deletions

34
.dockerignore Normal file
View File

@@ -0,0 +1,34 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/engine/reference/builder/#dockerignore-file
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.next
**/.cache
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
**/build
**/dist
LICENSE
README.md

5
.env.example Normal file
View File

@@ -0,0 +1,5 @@
DB_HOST=iceberg-gaming.com
DB_PORT=3306
DB_USERNAME=user
DB_PASSWORD=pass
DB_DATABASE=dbname

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
node_modules
.env

32
Dockerfile Normal file
View File

@@ -0,0 +1,32 @@
# syntax=docker/dockerfile:1
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/engine/reference/builder/
ARG NODE_VERSION=16.17.0
FROM node:${NODE_VERSION}-alpine
# Use production node environment by default.
ENV NODE_ENV production
WORKDIR /usr/src/app
# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.npm to speed up subsequent builds.
COPY package-lock.json package.json ./
RUN --mount=type=cache,target=/root/.npm \
npm ci --omit=dev
# Run the application as a non-root user.
USER node
COPY server.js .
# Expose the port that the application listens on.
EXPOSE 4000
# Run the application.
CMD node server.js

View File

@@ -1 +1,11 @@
# 17th-UnitTracker-API
```docker
docker-compose up -d --build
OR
<!-- if running as basic container, uncomment the dotenv line in server.js -->
docker build -t unit-tracker-api .
docker run -p 4000:4000 unit-tracker-api
```

17
docker-compose.yaml Normal file
View File

@@ -0,0 +1,17 @@
name: 17th-unittracker-api
services:
server:
env_file:
- .env
build:
context: .
environment:
NODE_ENV: production
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
DB_USERNAME: ${DB_USERNAME}
DB_PASSWORD: ${DB_PASSWORD}
DB_DATABASE: ${DB_DATABASE}
ports:
- 4000:4000

1628
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

20
package.json Normal file
View File

@@ -0,0 +1,20 @@
{
"name": "17th-unittracker-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "IndigoFox",
"license": "ISC",
"dependencies": {
"@apollo/server": "^4.7.1",
"db2graphql": "^0.12.0",
"dotenv": "^16.0.3",
"graphql": "^16.6.0",
"knex": "^2.4.2",
"mysql": "^2.18.1"
}
}

45
server.js Normal file
View File

@@ -0,0 +1,45 @@
const knex = require('knex');
const db2g = require('db2graphql');
const { ApolloServer } = require('@apollo/server');
const { startStandaloneServer } = require('@apollo/server/standalone');
// dotenv - not used if running in docker compose
// require('dotenv').config();
// get env variables
const { DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_DATABASE } = process.env;
const start = async (cb) => {
/**************************************/
const api = new db2g('mariadb', knex({
client: 'mysql',
connection: {
host: DB_HOST,
port: DB_PORT,
user: DB_USERNAME,
password: DB_PASSWORD,
database: DB_DATABASE
}
}));
await api.connect(DB_DATABASE); // Connects to database and extracts database schema
// Get generated schema and resolvers
const schema = api.getSchema();
const resolvers = api.getResolvers();
/**************************************/
// Create Apollo Server and start
if (!schema) throw new Error('Error: empty schema');
console.log(schema);
const server = new ApolloServer({
typeDefs: schema,
resolvers,
introspection: true,
playground: true,
});
startStandaloneServer(server).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
}
start();