Skip to content

Commit 1bb0ab8

Browse files
committed
Upgrade to apollo-server v2
1 parent e2bcd04 commit 1bb0ab8

File tree

9 files changed

+1973
-557
lines changed

9 files changed

+1973
-557
lines changed

.eslintrc.yml

-41
This file was deleted.

knexfile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const defaults = {
1010
connection: {
1111
user: connection.user || 'root',
1212
password: connection.password || '',
13-
host: connection.host,
13+
host: connection.hosts[0].name,
1414
port: connection.port || 5432,
15-
database: connection.segments[0],
15+
database: connection.path[0],
1616
},
1717
migrations: {
1818
directory: `${__dirname}/db/migrations`,

package.json

+25-31
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,33 @@
55
"knex": "ts-node ./node_modules/.bin/knex"
66
},
77
"dependencies": {
8-
"apollo-server-express": "^1.3.2",
9-
"body-parser": "^1.18.2",
10-
"connection-string": "^0.4.1",
11-
"core-js": "^2.5.3",
12-
"dotenv": "^4.0.0",
13-
"express": "^4.16.2",
14-
"graphql": "^0.12.3",
15-
"graphql-tools": "^2.19.0",
16-
"knex": "^0.14.2",
17-
"lodash": "^4.17.4",
18-
"morgan": "^1.9.0",
19-
"pg": "^7.4.1"
8+
"apollo-server": "^2.1.0",
9+
"apollo-server-express": "^2.1.0",
10+
"body-parser": "^1.18.3",
11+
"connection-string": "^1.0.1",
12+
"core-js": "^2.5.7",
13+
"dotenv": "^6.1.0",
14+
"express": "^4.16.4",
15+
"graphql": "^14.0.2",
16+
"graphql-tools": "^4.0.1",
17+
"knex": "^0.15.2",
18+
"lodash": "^4.17.11",
19+
"morgan": "^1.9.1",
20+
"pg": "^7.5.0"
2021
},
2122
"devDependencies": {
22-
"@types/body-parser": "^1.16.8",
23-
"@types/express": "^4.11.0",
24-
"@types/knex": "^0.14.4",
25-
"@types/node": "^9.4.0",
23+
"@types/body-parser": "^1.17.0",
24+
"@types/express": "^4.16.0",
25+
"@types/knex": "^0.14.26",
26+
"@types/node": "^10.11.7",
2627
"ava": "^0.25.0",
27-
"eslint": "^4.16.0",
28-
"eslint-config-airbnb-base": "^12.1.0",
29-
"eslint-config-google": "^0.9.1",
30-
"eslint-config-prettier": "^2.9.0",
31-
"eslint-import-resolver-typescript": "^1.0.2",
32-
"eslint-plugin-import": "^2.8.0",
33-
"eslint-plugin-prettier": "^2.5.0",
34-
"eslint-plugin-security": "^1.4.0",
35-
"eslint-plugin-typescript": "^0.8.1",
36-
"nodemon": "^1.14.11",
37-
"prettier": "^1.10.2",
38-
"ts-node": "^4.1.0",
39-
"tslint": "^5.9.1",
40-
"typescript": "^2.6.2",
41-
"typescript-eslint-parser": "^12.0.0"
28+
"nodemon": "^1.18.4",
29+
"prettier": "^1.14.3",
30+
"ts-node": "^7.0.1",
31+
"tslint": "^5.11.0",
32+
"tslint-config-prettier": "^1.15.0",
33+
"tslint-plugin-prettier": "^2.0.0",
34+
"typescript": "^3.1.3",
35+
"typescript-eslint-parser": "^20.0.0"
4236
}
4337
}

src/database.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import * as _ from 'lodash';
2-
import * as Knex from 'knex';
3-
import * as knexfile from '../knexfile';
1+
import _ from 'lodash';
2+
import Knex from 'knex';
3+
import knexfile from '../knexfile';
44

55
class Database {
66
private knexInstance: Knex;
7-
private config: Object;
7+
private config: object;
88

99
public connect(options = {}): void {
1010
if (this.knexInstance) {

src/schema/index.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import * as _ from 'lodash';
2-
import { makeExecutableSchema } from 'graphql-tools';
2+
import { makeExecutableSchema } from 'apollo-server';
33
import { getDirectories } from '../utils';
44

5-
interface QueryMutation {
6-
[name: string]: Resolver;
5+
interface IResolver {
6+
definition: string;
7+
resolve: () => void;
78
}
89

9-
interface Resolver {
10-
definition: string;
11-
resolve: Function;
10+
interface IQueryMutation {
11+
[name: string]: IResolver;
1212
}
1313

14-
interface Schema {
14+
interface ISchema {
1515
type: string;
16-
queries: QueryMutation;
17-
mutations: QueryMutation;
18-
resolvers?: Object;
16+
queries: IQueryMutation;
17+
mutations: IQueryMutation;
18+
resolvers?: object;
1919
}
2020

2121
const directories = getDirectories(__dirname);
2222

23-
const schemas: Array<Schema> = directories.reduce((schemaList, directory) => {
23+
const schemas: ISchema[] = directories.reduce((schemaList, directory) => {
2424
const schema = require(directory); // eslint-disable-line
2525
return [...schemaList, schema];
2626
}, []);
@@ -30,7 +30,7 @@ const resolverDefinitionsToString = (resolvers = {}): string =>
3030
.map(({ definition }) => definition)
3131
.join('\n');
3232

33-
const buildResolver = (schema: Schema): Object => {
33+
const buildResolver = (schema: ISchema): Object => {
3434
const { queries = {}, mutations = {}, resolvers = {} } = schema;
3535

3636
return {

src/server.ts

+28-24
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,63 @@
1-
import * as http from 'http';
2-
import * as express from 'express';
3-
import * as bodyParser from 'body-parser';
4-
import * as logger from 'morgan';
5-
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
1+
import http from 'http';
2+
import express from 'express';
3+
import bodyParser from 'body-parser';
4+
import logger from 'morgan';
5+
import { ApolloServer } from 'apollo-server-express';
66
import schema from './schema';
77

8+
const apollo = new ApolloServer({
9+
schema,
10+
playground: true,
11+
});
12+
813
// Creates and configures an ExpressJS web server.
914
class Server {
10-
public express: express.Application;
11-
public server: http.Server;
15+
app: express.Application;
16+
server: http.Server;
1217

1318
// Run configuration methods on the Express instance.
1419
constructor() {
15-
this.express = express();
20+
this.app = express();
1621
this.middleware();
1722
this.routes();
1823
}
1924

2025
// Configure Express middleware.
21-
private middleware(): void {
22-
this.express.use(logger('dev'));
23-
this.express.use(bodyParser.json());
24-
this.express.use(bodyParser.urlencoded({ extended: true }));
26+
middleware() {
27+
this.app.use(logger('dev'));
28+
this.app.use(bodyParser.json());
29+
this.app.use(bodyParser.urlencoded({ extended: true }));
30+
31+
apollo.applyMiddleware({
32+
app: this.app,
33+
});
2534
}
2635

2736
// Configure API endpoints.
28-
private routes(): void {
37+
routes() {
2938
/* This is just to get up and running, and to make sure what we've got is
3039
* working so far. This function will change when we start to add more
3140
* API endpoints */
3241
const router = express.Router();
3342

34-
// The GraphQL endpoint
35-
router.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
36-
37-
// GraphiQL, a visual editor for queries
38-
router.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
39-
this.express.use('/', router);
43+
this.app.use('/', router);
4044
}
4145

42-
public start(done = () => {}): void {
46+
start(cb = () => null) {
4347
const port = process.env.PORT || 3000;
4448

45-
this.server = this.express.listen(port, err => {
49+
this.server = this.app.listen(port, err => {
4650
if (err) {
4751
throw err;
4852
}
4953
console.log(`🔥 Server running on port ${port}...`); // eslint-disable-line
50-
done();
54+
cb();
5155
});
5256
}
5357

54-
public stop(done = () => {}): void {
58+
stop(cb = () => null) {
5559
if (this.server) {
56-
this.server.close(done);
60+
this.server.close(cb);
5761
}
5862
}
5963
}

tsconfig.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
22
"compilerOptions": {
3-
"target": "es6",
3+
"target": "esnext",
44
"module": "commonjs",
5+
"allowSyntheticDefaultImports": true,
6+
"esModuleInterop": true,
7+
"noEmitOnError": true,
58
"lib": ["es2015", "es2017"]
69
},
710
"include": [
8-
"src/**/*.ts"
11+
"src/**/*"
912
],
1013
"exclude": [
1114
"node_modules"

tslint.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"extends": [
3+
"tslint:latest",
4+
"tslint-config-prettier",
5+
"tslint-plugin-prettier"
6+
],
7+
"rules": {
8+
"ordered-imports": false,
9+
"no-submodule-imports": false,
10+
"object-literal-sort-keys": false,
11+
"member-ordering": false,
12+
"member-access": false,
13+
"prettier": [true, {
14+
"printWidth": 120,
15+
"semi": true,
16+
"useTabs": false,
17+
"singleQuote": true,
18+
"trailingComma": "es5",
19+
"bracketSpacing": true,
20+
"jsxBracketSameLine": true
21+
}]
22+
}
23+
}

0 commit comments

Comments
 (0)