Skip to content
This repository has been archived by the owner on May 31, 2019. It is now read-only.

Commit

Permalink
Unify graphql path
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki committed Aug 21, 2018
1 parent f4697f0 commit 1347732
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 3 additions & 3 deletions server/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ if (process.env.PLAYGROUND_VARIABLES_FILE) {
}
}

const topLevelGraphqlPath = '/graphql'
const port = process.env.HTTP_PORT || '8000'

const config = {
server: {
apiPath: topLevelGraphqlPath,
port
},
graphQLConfig: {
tracing: true
},
playgroundConfig: {
endpoint: '/graphql', // if you want GraphiQL enabled
endpoint: topLevelGraphqlPath, // if you want GraphiQL enabled
query: playgroundQueryFileContent,
variables: playgroundVariableFileContent,
subscriptionEndpoint: process.env.PLAYGROUND_SUBS_ENDPOINT || `ws://${hostname()}:${port}/subscriptions`
Expand Down Expand Up @@ -90,8 +92,6 @@ if (process.env.SCHEMA_LISTENER_CONFIG) {
}
}

playgroundQueryFileContent = fs.readFileSync(process.env.PLAYGROUND_QUERY_FILE, 'utf-8')

if (process.env.KEYCLOAK_CONFIG_FILE) {
try {
let keycloakConfig = fs.readFileSync(process.env.KEYCLOAK_CONFIG_FILE, 'utf-8')
Expand Down
9 changes: 7 additions & 2 deletions server/security/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {log} = require('./lib/util/logger')
const { log } = require('../lib/util/logger')
const Keycloak = require('keycloak-connect')
var session = require('express-session')

Expand All @@ -9,7 +9,7 @@ const config = require('../config')
*
* @param {*} expressRouter
*/
exports.applyAuthMiddleware = (expressRouter) => {
exports.applyAuthMiddleware = (expressRouter, apiPath) => {
if (config.keycloakConfig) {
log.info('Initializing Keycloak authentication')
const memoryStore = new session.MemoryStore()
Expand All @@ -24,7 +24,12 @@ exports.applyAuthMiddleware = (expressRouter) => {
store: memoryStore
}, config.keycloakConfig)

// Install general keycloak middleware
expressRouter.use(keycloak.middleware())

// Protect the main route for all graphql services
// (disable unauthenticated access)
expressRouter.use(apiPath, keycloak.protect())
} else {
log.info('Keycloak authentication is not configured')
}
Expand Down
6 changes: 4 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {getMetrics, responseLoggingMetric} = require('./metrics')
const {applyAuthMiddleware} = require('./security')
const schemaParser = require('./lib/schemaParser')
const schemaListenerCreator = require('./lib/schemaListeners/schemaListenerCreator')
const { server } = require('./config')

function newExpressApp () {
let app = express()
Expand All @@ -18,12 +19,13 @@ function newExpressApp () {

app.use('*', cors())
app.use(expressPino)
applyAuthMiddleware(app)
applyAuthMiddleware(app, server.apiPath)
return app
}

function newApolloServer (app, schema, httpServer, tracing, playgroundConfig) {
let apolloServer = new ApolloServer({

schema,
context: async ({ req }) => {
return {
Expand All @@ -41,7 +43,7 @@ function newApolloServer (app, schema, httpServer, tracing, playgroundConfig) {
]
}
})
apolloServer.applyMiddleware({ app })
apolloServer.applyMiddleware({ app, disableHealthCheck: true, path: server.apiPath })
apolloServer.installSubscriptionHandlers(httpServer)

return apolloServer
Expand Down

0 comments on commit 1347732

Please sign in to comment.