Initial commit for Apollo client/server with express.

This commit is contained in:
richburdon
2020-05-23 13:13:45 -04:00
commit 94559bdf47
23 changed files with 13359 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
//
// Copyright 2020 DxOS
//
module.exports = {
presets: [
[
'@babel/preset-env'
]
],
plugins: [
[
'babel-plugin-inline-import', {
extensions: [
'.graphql',
'.proto',
'.txt'
]
}
],
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-export-default-from'
]
};
+4
View File
@@ -0,0 +1,4 @@
{
"port": 4000,
"path": "/graphql"
}
+51
View File
@@ -0,0 +1,51 @@
{
"name": "@dxos/console-server",
"version": "1.0.0-beta.0",
"description": "DxOS Console Server",
"main": "index.js",
"scripts": {
"lint": "semistandard 'src/**/*.js'",
"test": "jest --rootDir ./src --passWithNoTests --no-cache",
"start": "nodemon --exec babel-node ./src/main.js"
},
"author": "DxOS.org",
"license": "GPL-3.0",
"browserslist": [
"> 5%"
],
"jest": {
"testEnvironment": "node"
},
"dependencies": {
"@babel/runtime": "^7.8.7",
"@dxos/console-client": "^1.0.0-beta.0",
"apollo-boost": "^0.4.9",
"apollo-server-express": "^2.13.1",
"debug": "^4.1.1",
"express": "^4.17.1",
"express-graphql": "^0.9.0",
"graphql": "^15.0.0",
"graphql-tag": "^2.10.3",
"source-map-support": "^0.5.12"
},
"devDependencies": {
"@babel/cli": "7.4.4",
"@babel/core": "^7.4.5",
"@babel/node": "^7.8.7",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-proposal-export-default-from": "^7.5.2",
"@babel/preset-env": "^7.4.5",
"babel-eslint": "^10.0.2",
"babel-jest": "^24.8.0",
"babel-plugin-add-module-exports": "^1.0.2",
"babel-plugin-inline-import": "^3.0.0",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-jest": "^23.13.1",
"jest": "^24.8.0",
"nodemon": "^2.0.4",
"semistandard": "^14.2.0"
},
"publishConfig": {
"access": "public"
}
}
@@ -0,0 +1,7 @@
type Status {
version: String
}
type Query {
status: Status
}
@@ -0,0 +1,5 @@
{
status {
version
}
}
+77
View File
@@ -0,0 +1,77 @@
//
// Copyright 2020 DxOS
//
import debug from 'debug';
import express from 'express';
import path from 'path';
import { ApolloServer, gql } from 'apollo-server-express';
import { print } from 'graphql/language';
import SCHEMA from './gql/api.graphql';
import QUERY_STATUS from './gql/status.graphql';
import config from '../config.json';
import { version } from '../package.json';
const log = debug('c2:src');
debug.enable('c2:*');
// Resolver
const resolvers = {
Query: {
status: () => ({
version
})
}
};
// Server
const app = express();
// CORS
// import cors from 'cors'
// https://expressjs.com/en/resources/middleware/cors.html
// https://www.prisma.io/blog/enabling-cors-for-express-graphql-apollo-server-1ef999bfb38d
// app.use(cors({
// origin: true,
// credentials: true
// }));
// React app
// TODO(burdon): Create HTML file.
// TODO(burdon): Load JS.
app.get('/app', (req,res) =>{
res.sendFile(path.join(__dirname + '/../../../node_modules/@dxos/console-client/dist/es/main.js'));
});
// https://www.apollographql.com/docs/apollo-server/api/apollo-server
const server = new ApolloServer({
typeDefs: SCHEMA,
resolvers,
// https://www.apollographql.com/docs/apollo-server/testing/graphql-playground
// https://github.com/prisma-labs/graphql-playground#usage
// introspection: true,
playground: {
settings: {
'editor.theme': 'light'
},
tabs: [
{
endpoint: config.path,
query: print(gql(QUERY_STATUS))
}
]
}
});
// https://www.apollographql.com/docs/apollo-server/api/apollo-server/#apolloserverapplymiddleware
server.applyMiddleware({
app,
path: config.path
});
app.listen({ port: config.port }, () => {
log(`Running: http://localhost:${config.port}`);
});