Skip to content

Commit

Permalink
fix: adding compose and config
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki committed Sep 11, 2020
1 parent 61f1138 commit 54eaa15
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
11 changes: 11 additions & 0 deletions services/iot-graphql-api/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'

services:
postgres:
image: postgres:9.6
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: rhtr-pass
POSTGRES_USER: rhtr-user
POSTGRES_DB: city-info
11 changes: 6 additions & 5 deletions services/iot-graphql-api/src/db.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import Knex from 'knex'
import config from './config'

export function loadDBConfig() {
let port
if (process.env.DB_PORT) {
port = parseInt(process.env.DB_PORT, 10)
}
return {
client: process.env.DB_CLIENT,
client: config.DB_CLIENT,
connection: {
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
user: config.DB_USER,
password: config.DB_PASSWORD,
database: config.DB_DATABASE,
host: config.DB_HOST,
port: port && !isNaN(port) ? port : 5432
},
pool: { min: 5, max: 30 }
Expand Down
10 changes: 4 additions & 6 deletions services/iot-graphql-api/src/graphback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { connectDB, loadDBConfig } from './db'
/**
* create Apollo server using automatically generated resolvers
*/
export const createApolloServer = () => {
export const createApolloServer = async () => {
const graphbackExtension = 'graphback'
const config = loadConfigSync({
extensions: [
Expand All @@ -35,18 +35,16 @@ export const createApolloServer = () => {
})

const dbConfig = loadDBConfig()
migrateDB(dbConfig, typeDefs, {
await migrateDB(dbConfig, typeDefs, {
operationFilter: removeNonSafeOperationsFilter
}).then(() => {
console.log('Migrated database')
})
console.log('Migrated database')

const apolloServer = new ApolloServer({
typeDefs,
resolvers: [resolvers],
context: contextCreator
})

return apolloServer;
return apolloServer
}

22 changes: 15 additions & 7 deletions services/iot-graphql-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ApolloServer } from 'apollo-server-express'
import cors from 'cors'
import express from 'express'
import http from 'http'
Expand All @@ -17,12 +18,19 @@ app.use((req: Express.Request, res: Express.Response, next) => {
}
})

const apolloServer = createApolloServer();
apolloServer.applyMiddleware({ app })
createApolloServer()
.then((apolloServer: ApolloServer) => {
apolloServer.applyMiddleware({ app })

const httpServer = http.createServer(app)
apolloServer.installSubscriptionHandlers(httpServer)
const httpServer = http.createServer(app)
apolloServer.installSubscriptionHandlers(httpServer)

httpServer.listen({ port: HTTP_PORT }, () => {
console.log(`🚀 Graphback Server ready at http://localhost:${HTTP_PORT}/graphql \nFor more information see: https://graphback.dev`)
})
httpServer.listen({ port: HTTP_PORT }, () => {
console.log(
`🚀 Graphback Server ready at http://localhost:${HTTP_PORT}/graphql \nFor more information see: https://graphback.dev`
)
})
})
.catch((error) => {
console.log(`Error: ${error}`)
})

0 comments on commit 54eaa15

Please sign in to comment.