Switch to apollo-server-express to enable middleware for authentication

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/3/head
trivernis 4 years ago
parent d475851bb3
commit b61dfa8810
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

13
package-lock.json generated

@ -310,6 +310,7 @@
"version": "3.4.0", "version": "3.4.0",
"resolved": "https://registry.npmjs.org/@types/crc/-/crc-3.4.0.tgz", "resolved": "https://registry.npmjs.org/@types/crc/-/crc-3.4.0.tgz",
"integrity": "sha1-I2a+tDmc1zSzPkLHrICVduYX1Io=", "integrity": "sha1-I2a+tDmc1zSzPkLHrICVduYX1Io=",
"dev": true,
"requires": { "requires": {
"@types/node": "*" "@types/node": "*"
} }
@ -863,18 +864,6 @@
"zen-observable-ts": "^0.8.21" "zen-observable-ts": "^0.8.21"
} }
}, },
"apollo-server": {
"version": "2.17.0",
"resolved": "https://registry.npmjs.org/apollo-server/-/apollo-server-2.17.0.tgz",
"integrity": "sha512-vVMu+VqjmsB6yk5iNTb/AXM6EJGd2uwzrcTAbwqvGI7GqCYDRZlGBwrQCjOU/jT/EPWdNRWks/qhJYiQMeVXSg==",
"requires": {
"apollo-server-core": "^2.17.0",
"apollo-server-express": "^2.17.0",
"express": "^4.0.0",
"graphql-subscriptions": "^1.0.0",
"graphql-tools": "^4.0.0"
}
},
"apollo-server-caching": { "apollo-server-caching": {
"version": "0.5.2", "version": "0.5.2",
"resolved": "https://registry.npmjs.org/apollo-server-caching/-/apollo-server-caching-0.5.2.tgz", "resolved": "https://registry.npmjs.org/apollo-server-caching/-/apollo-server-caching-0.5.2.tgz",

@ -27,13 +27,14 @@
"gulp-eslint": "^6.0.0", "gulp-eslint": "^6.0.0",
"gulp-nodemon": "^2.5.0", "gulp-nodemon": "^2.5.0",
"gulp-typescript": "^6.0.0-alpha.1", "gulp-typescript": "^6.0.0-alpha.1",
"typescript": "^4.0.2" "typescript": "^4.0.2",
"@types/crc": "^3.4.0"
}, },
"dependencies": { "dependencies": {
"@types/crc": "^3.4.0", "apollo-server-express": "^2.17.0",
"apollo-server": "^2.17.0",
"crc": "^3.8.0", "crc": "^3.8.0",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"express": "^4.17.1",
"graphql": "^15.3.0", "graphql": "^15.3.0",
"messagepack": "^1.1.12", "messagepack": "^1.1.12",
"pg": "^8.3.3", "pg": "^8.3.3",

@ -26,7 +26,7 @@ export class CargoBikeAPI extends DataSource {
bike.id = id bike.id = id
bike.description = token bike.description = token
bike.name = name bike.name = name
this.connection.manager.save(bike) await this.connection.manager.save(bike)
return { return {
success: true, success: true,
message: token, message: token,

@ -1,26 +1,55 @@
import { ApolloServer } from 'apollo-server' import { ApolloServer } from 'apollo-server-express'
import bikeresolver from './resolvers/cargobikeResolver' import bikeresolver from './resolvers/cargobikeResolver'
import { CargoBikeAPI } from './datasources/db/cargobikeAPI' import { CargoBikeAPI } from './datasources/db/cargobikeAPI'
import typeDefs from './schema/type-defs' import typeDefs from './schema/type-defs'
import 'reflect-metadata' import 'reflect-metadata'
import { createConnection } from 'typeorm' import { createConnection } from 'typeorm'
import { UserServerAPI } from './datasources/userserver/userserviceAPI' import { UserServerAPI } from './datasources/userserver/userserviceAPI'
import express from 'express'
require('dotenv').config() require('dotenv').config()
async function authenticate (req: any, res: any, next: any) {
if (process.env.NODE_ENV === 'develop') {
next()
} else {
const token = req.headers.authorization?.replace('Bearer ', '')
if (token) {
if (await userAPI.validateToken(token)) {
next()
} else {
res.status(401)
res.send('Unauthorized')
}
} else {
res.status(401)
res.send('Unauthorized')
}
}
}
createConnection().then(async () => { createConnection().then(async () => {
console.log('connected to db') console.log('connected to db')
}).catch(error => console.log(error)) }).catch(error => console.log(error))
const userAPI = new UserServerAPI(process.env.RPC_HOST)
const server = new ApolloServer({ const server = new ApolloServer({
resolvers: [bikeresolver], resolvers: [bikeresolver],
typeDefs, typeDefs,
dataSources: () => ({ dataSources: () => ({
cargoBikeAPI: new CargoBikeAPI(), cargoBikeAPI: new CargoBikeAPI(),
userAPI: new UserServerAPI(process.env.RPC_HOST) userAPI
}) })
}) })
const app = express()
app.post('/graphql', authenticate)
app.get(/\/graphql?&.*query=/, authenticate)
server.applyMiddleware({ app })
console.log(__dirname) console.log(__dirname)
server.listen() app.listen(4000, () => {
.then(({ url }) => console.log(`Server ready at ${url} `)) console.log('Server listening on port 4000')
})

Loading…
Cancel
Save