Skip to content

Commit e92e08f

Browse files
committed
Add initial reactium graphql module.
1 parent 342df55 commit e92e08f

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ build/Release
3535
# Dependency directories
3636
node_modules/
3737
jspm_packages/
38-
reactium_modules/
38+
reactium_modules/*
3939

4040
# Typescript v1 declaration files
4141
typings/
@@ -93,3 +93,7 @@ src/app/server/webpack-manifest.json
9393
# Editor configs
9494
*.code-workspace
9595
*.vscode
96+
97+
# Allow Git Tracking for these directories
98+
!reactium_modules/@reactium
99+
!reactium_modules/@reactium/graphql
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "@reactium/graphql",
3+
"version": "0.0.1",
4+
"description": "GraphQL Support for Reactium",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "John Dillick <john@dillick.us>",
10+
"license": "MIT",
11+
"dependencies": {
12+
"graphql-playground-middleware-express": "^1.7.23"
13+
},
14+
"devDependencies": {
15+
"@apollo/client": "^3.11.2"
16+
}
17+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ApolloClient, InMemoryCache } from '@apollo/client';
2+
3+
const cache = new InMemoryCache();
4+
5+
const client = new ApolloClient({
6+
// Provide required constructor fields
7+
cache: cache,
8+
uri: 'http://localhost:4000/',
9+
10+
// Provide some optional constructor fields
11+
name: 'react-web-client',
12+
version: '1.3',
13+
queryDeduplication: false,
14+
defaultOptions: {
15+
watchQuery: {
16+
fetchPolicy: 'cache-and-network',
17+
},
18+
},
19+
});

0 commit comments

Comments
 (0)