Repository URL to install this package:
|
Version:
1.2.8 ▾
|
import { isPortFree } from '../deps'
async function listen(graphExpressApp) {
process.on('uncaughtException', uncaughtException => {
console.log('UNCAUGT_EXCEPTION')
console.log(uncaughtException)
console.log('\n\n\n')
})
// process.on('exit', () => )
// process.on('SIGTERM')
try {
const onListen = () =>
console.log('[uxui-graphql] listening http://localhost:4000/graphiql')
const gqlApp = graphExpressApp.listen(process.env.API_PORT, onListen)
// global.gqlApp = gqlApp
if (gqlApp.catch) {
gqlApp.catch(exception => console.log('GQL_SERVER_EXCEPTION', exception))
}
console.warn('@james @fixme')
global.gqlApp = await gqlApp
} catch (FIXMEJAMESEXCEPTION) {
console.log({ FIXMEJAMESEXCEPTION })
}
}
function standalone(app, gql) {
console.log('[uxui-graphql] - auto standalone')
const graphExpressApp = gql.createApp()
listen(graphExpressApp)
}
function subapp(app, gql) {
console.log('[uxui-graphql] - using graphql as subapp')
// should not be needed on same origin...
// app.options('/graphql', cors(uxuiGraphql.corsOptions))
// app.use('/graphql', cors(uxuiGraphql.corsOptions))
app.use('/api', gql.proxyMiddleware)
app.use('/graphql', gql.cookieMiddleware)
app.use('/graphql', gql.graphqlSubApp)
app.get('/graphql', gql.graphqlSubApp)
app.post('/graphql', gql.graphqlSubApp)
}
function graphiql(app, gql) {
console.log('[uxui-graphql] - using graphiql for query playground')
app.use('/graphiql', gql.graphiqlSubApp)
app.get('/graphiql', gql.graphiqlSubApp)
}
function loadForEnv(app, gql) {
if (process.env.AUTO_STANDALONE_GRAPHQL) {
standalone(app, gql)
return Promise.resolve(gql)
}
if (process.env.SUBAPP_GRAPHQL) {
subapp(app, gql)
}
if (process.env.GRAPHIQL) {
graphiql(app, gql)
}
return Promise.resolve(gql)
}
async function isFree() {
try {
const isNotAlreadyRunningGraphql = process.env.SKIP_API_PORT_CHECK
? true
: await isPortFree(+process.env.API_PORT)
return isNotAlreadyRunningGraphql
} catch (listeningException) {
return true
}
}
/**
* @todo
*
* @see https://stackoverflow.com/questions/15601703/difference-between-app-use-and-app-get-in-express-js
*/
async function runGraphqlServer(app) {
const isNotAlreadyRunningGraphql = await isFree()
if (isNotAlreadyRunningGraphql) {
console.log('[uxui-graphql] - starting in skreact')
// const uxuiGraphql = require('skava-graphql/dist/server.js')
let uxuiGraphql
try {
console.log('[uxui-graphql] requiring server.js')
uxuiGraphql = require('@skava/graphql/server.js')
} catch (requireException) {
console.log('[uxui-graphql] could not require server.js')
uxuiGraphql = require('@skava/graphql')
}
return loadForEnv(app, uxuiGraphql)
} else {
console.warn('[uxui-graphql] - already running')
return Promise.resolve(false)
}
}
export { runGraphqlServer }