|
| 1 | +const proxy = require('http-proxy-middleware'); |
| 2 | + |
| 3 | +const { Enums } = ReactiumBoot; |
| 4 | + |
| 5 | +const graphqlProxyPath = process.env.GRAPHQL_PROXY_URL || '/graphql'; |
| 6 | +const playgroundURL = process.env.GRAPHQL_PLAYGROUND_URL || '/playground'; |
| 7 | +const playgroundEnabled = process.env.GRAPHQL_PLAYGROUND === 'on' || process.env.NODE_ENV === 'development'; |
| 8 | + ; |
| 9 | +const proxyEnabled = process.env.GRAPHQL_PROXY_ENABLED !== 'off'; |
| 10 | +const graphqlAPI = |
| 11 | + process.env.GRAPHQL_URL || `http://127.0.0.1:4000${graphqlProxyPath}`; |
| 12 | +const logLevel = process.env.DEBUG === 'on' ? 'debug' : 'error'; |
| 13 | + |
| 14 | +BOOT('GraphQL Module for Reactium...'); |
| 15 | +BOOT('GraphQL API:', graphqlAPI); |
| 16 | +DEBUG('Set GraphQL API URL with GRAPHQL_URL environment variable (defaults to http://127.0.0.1:4000/graphql)'); |
| 17 | + |
| 18 | +BOOT('GraphQL Proxy:', proxyEnabled ? graphqlProxyPath : 'disabled'); |
| 19 | +DEBUG('Set GraphQL Proxy URL with GRAPHQL_PROXY_URL environment variable. Disable with GRAPHQL_PROXY_ENABLED=off (defaults to /graphql)'); |
| 20 | + |
| 21 | +BOOT('GraphQL Playground:', playgroundEnabled ? playgroundURL : 'disabled'); |
| 22 | +DEBUG('Set GraphQL Playground URL with GRAPHQL_PLAYGROUND_URL environment variable. Disable with GRAPHQL_PLAYGROUND=off (default in production)'); |
| 23 | + |
| 24 | +ReactiumBoot.Hook.registerSync( |
| 25 | + 'Server.AppGlobals', |
| 26 | + (req, AppGlobals) => { |
| 27 | + AppGlobals.register('playgroundEnabled', { |
| 28 | + name: 'playgroundEnabled', |
| 29 | + value: playgroundEnabled, |
| 30 | + }); |
| 31 | + |
| 32 | + AppGlobals.register('graphqlAPI', { |
| 33 | + name: 'graphqlAPI', |
| 34 | + value: proxyEnabled ? graphqlProxyPath : graphqlAPI, |
| 35 | + server: graphqlAPI, |
| 36 | + }); |
| 37 | + }, |
| 38 | + Enums.priority.highest, |
| 39 | + 'REACTIUM-CORE-SDK-API-GLOBALS', |
| 40 | +); |
| 41 | + |
| 42 | +if (graphqlAPI && proxyEnabled && graphqlProxyPath) { |
| 43 | + ReactiumBoot.Server.Middleware.register('graphql', { |
| 44 | + name: 'graphql', |
| 45 | + use: proxy(graphqlProxyPath, { |
| 46 | + target: graphqlAPI, |
| 47 | + changeOrigin: true, |
| 48 | + pathRewrite: { |
| 49 | + [`^${graphqlProxyPath}`]: '', |
| 50 | + }, |
| 51 | + logLevel, |
| 52 | + ws: true, |
| 53 | + }), |
| 54 | + order: Enums.priority.highest, |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +if (playgroundEnabled && graphqlAPI) { |
| 59 | + const express = require('express'); |
| 60 | + const playgroundMiddleware = |
| 61 | + require('graphql-playground-middleware-express').default; |
| 62 | + const Router = express.Router(); |
| 63 | + Router.get(playgroundURL, playgroundMiddleware({ endpoint: graphqlProxyPath })); |
| 64 | + |
| 65 | + ReactiumBoot.Server.Middleware.register('graphql-playground', { |
| 66 | + name: 'graphql-playground', |
| 67 | + use: Router, |
| 68 | + order: Enums.priority.highest, |
| 69 | + }); |
| 70 | +} |
0 commit comments