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

fix: refactor graphback to hide unused features #3

Merged
merged 3 commits into from
Sep 14, 2020
Merged
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
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:
Copy link
Member

Choose a reason for hiding this comment

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

There's a custom Postgres dockerfile we're using. You can copy this configuration.

Copy link
Member

Choose a reason for hiding this comment

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

The reason for this is that it provides a pre-seeded DB for testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I struggled to run it - credentials would not allow me to log in for some reason.

image: postgres:9.6
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: rhtr-pass
POSTGRES_USER: rhtr-user
POSTGRES_DB: city-info
24 changes: 12 additions & 12 deletions services/iot-graphql-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
},
"license": "Apache 2.0",
"dependencies": {
"@graphback/runtime-knex": "~0.16.1",
"@graphql-codegen/add": "~2.0.1",
"@graphql-codegen/typescript": "~1.17.9",
"@graphback/runtime-knex": "0.16.2",
"@graphql-codegen/add": "2.0.1",
"@graphql-codegen/typescript": "1.17.9",
"apollo-server-express": "~2.17.0",
"cors": "~2.8.5",
"dotenv": "~8.2.0",
Expand All @@ -27,20 +27,20 @@
"graphback": "~0.16.2",
"graphql": "~15.3.0",
"graphql-config": "~3.0.3",
"graphql-migrations": "~0.16.1",
"graphql-subscriptions": "~1.1.0",
"graphql-tag": "~2.11.0",
"knex": "~0.21.5",
"pg": "~8.3.3"
"graphql-migrations": "0.16.2",
"graphql-subscriptions": "1.1.0",
"graphql-tag": "2.11.0",
"knex": "0.21.5",
"pg": "8.3.3"
},
"devDependencies": {
"@graphql-cli/codegen": "~1.17.8",
"@graphql-cli/codegen": "1.17.8",
"@types/bson": "~4.0.2",
"@types/glob": "~7.1.3",
"@types/graphql": "~14.2.3",
"@types/graphql": "14.5.0",
"@types/node-fetch": "~2.5.7",
"graphback-cli": "~0.16.1",
"graphql-cli": "~4.0.0",
"graphback-cli": "0.16.2",
"graphql-cli": "4.1.0-beta.1",
"prettier": "~2.1.1",
"ts-node": "~9.0.0",
"tslint": "~6.1.3",
Expand Down
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
50 changes: 50 additions & 0 deletions services/iot-graphql-api/src/graphback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { createKnexDbProvider } from '@graphback/runtime-knex'
import { ApolloServer } from 'apollo-server-express'
import { buildGraphbackAPI } from 'graphback'
import { loadConfigSync } from 'graphql-config'
import { removeNonSafeOperationsFilter, migrateDB } from 'graphql-migrations'
import { connectDB, loadDBConfig } from './db'

/**
* create Apollo server using automatically generated resolvers
*/
export const createApolloServer = async () => {
const graphbackExtension = 'graphback'
const config = loadConfigSync({
extensions: [
() => ({
name: graphbackExtension
})
]
})

const projectConfig = config.getDefault()
const graphbackConfig = projectConfig.extension(graphbackExtension)
const modelDefs = projectConfig.loadSchemaSync(graphbackConfig.model)

const db = connectDB()

const { typeDefs, resolvers, contextCreator } = buildGraphbackAPI(modelDefs, {
crud: {
create: false,
subCreate: false,
subUpdate: false,
subDelete: false
},
dataProviderCreator: createKnexDbProvider(db)
})

const dbConfig = loadDBConfig()
await migrateDB(dbConfig, typeDefs, {
operationFilter: removeNonSafeOperationsFilter
})
wtrocki marked this conversation as resolved.
Show resolved Hide resolved
console.log('Migrated database')

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

return apolloServer
}
60 changes: 16 additions & 44 deletions services/iot-graphql-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ import { ApolloServer } from 'apollo-server-express'
import cors from 'cors'
import express from 'express'
import http from 'http'
import { buildGraphbackAPI } from 'graphback'
import { loadDBConfig, connectDB } from './db'
import { migrateDB, removeNonSafeOperationsFilter } from 'graphql-migrations'
import { createKnexDbProvider } from '@graphback/runtime-knex'
import { loadConfigSync } from 'graphql-config'
import { HTTP_RESPONSE_DELAY, NODE_ENV, HTTP_PORT } from './config'
import { createApolloServer } from './graphback'

const app = express()

app.use(cors())

app.use((req, res, next) => {
app.use((req: Express.Request, res: Express.Response, next) => {
if (HTTP_RESPONSE_DELAY && NODE_ENV === 'dev') {
// Simulate request processing time in dev mode
setTimeout(() => next(), HTTP_RESPONSE_DELAY)
Expand All @@ -22,43 +18,19 @@ app.use((req, res, next) => {
}
})

const graphbackExtension = 'graphback'
const config = loadConfigSync({
extensions: [
() => ({
name: graphbackExtension
})
]
})

const projectConfig = config.getDefault()
const graphbackConfig = projectConfig.extension(graphbackExtension)
const modelDefs = projectConfig.loadSchemaSync(graphbackConfig.model)

const db = connectDB()
const dbConfig = loadDBConfig()
createApolloServer()
.then((apolloServer: ApolloServer) => {
apolloServer.applyMiddleware({ app })

const { typeDefs, resolvers, contextCreator } = buildGraphbackAPI(modelDefs, {
dataProviderCreator: createKnexDbProvider(db)
})

migrateDB(dbConfig, typeDefs, {
operationFilter: removeNonSafeOperationsFilter
}).then(() => {
console.log('Migrated database')
})
const httpServer = http.createServer(app)
apolloServer.installSubscriptionHandlers(httpServer)

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

apolloServer.applyMiddleware({ app })

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

httpServer.listen({ port: HTTP_PORT }, () => {
console.log(`🚀 Server ready at http://localhost:${HTTP_PORT}/graphql`)
})
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}`)
})