Skip to content

Commit

Permalink
Upgrade to apollo-server-2 (#1107)
Browse files Browse the repository at this point in the history
This removes koa, but we can add it back should we need it again
Apolo-server-2 comes with playground, which is better than graphiql
  • Loading branch information
li-kai authored Aug 26, 2018
1 parent 99f2e1b commit 9a09950
Show file tree
Hide file tree
Showing 13 changed files with 824 additions and 889 deletions.
11 changes: 9 additions & 2 deletions api/data/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
[
"env",
{
"targets": { "node": "current" },
"targets": {
"node": "current"
},
"modules": false,
"loose": true,
"useBuiltIns": true
Expand All @@ -19,5 +21,10 @@
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
},
"plugins": [
["transform-object-rest-spread", {
"useBuiltIns": true
}]
],
}
8 changes: 4 additions & 4 deletions api/data/__mocks__/fs-extra.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import R from 'ramda';
import _ from 'lodash';

const fs = jest.genMockFromModule('fs-extra');

Expand All @@ -21,7 +21,7 @@ fs.readFile = async (filePath) => fs.readFileSync(filePath);
// file list set via setMock
fs.readdirSync = (directoryPath) => {
const pathArr = directoryPath.split(path.sep);
return Object.keys(R.path(pathArr, mockFiles)) || [];
return Object.keys(_.get(mockFiles, pathArr)) || [];
};

// A custom version of `readdir` that reads from the special mocked out
Expand All @@ -38,7 +38,7 @@ fs.readdir = async (directoryPath) => fs.readdirSync(directoryPath);
fs.readJson = async (directoryPath) => {
const pathArr = directoryPath.split(path.sep);
try {
return JSON.parse(R.path(pathArr, mockFiles));
return JSON.parse(_.get(mockFiles, pathArr));
} catch (error) {
return Promise.reject(error);
}
Expand All @@ -50,7 +50,7 @@ fs.readJson = async (directoryPath) => {
*/
fs.readJsonSync = (directoryPath) => {
const pathArr = directoryPath.split(path.sep);
return JSON.parse(R.path(pathArr, mockFiles));
return JSON.parse(_.get(mockFiles, pathArr));
};

/**
Expand Down
15 changes: 3 additions & 12 deletions api/data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"scripts": {
"ci": "yarn lint && yarn test",
"start": "cross-env NODE_ENV=development nodemon src --exec babel-node --watch src | bunyan -o short --color",
"start": "cross-env NODE_ENV=development babel-node src | bunyan -o short --color",
"test": "jest --config jest.config.js --coverage",
"test:watch": "jest --config jest.config.js --watch",
"lint": "eslint . --ignore-path ../../.gitignore",
Expand All @@ -27,25 +27,18 @@
],
"private": true,
"dependencies": {
"apollo-server-koa": "^1.3.2",
"apollo-server": "^2.0.5",
"axios": "^0.18.0",
"bluebird": "^3.5.1",
"boom": "^7.2.0",
"bunyan": "^1.8.12",
"cross-env": "^5.1.3",
"dotenv": "^5.0.1",
"fs-extra": "^5.0.0",
"graphql": "^0.13.1",
"graphql-playground-middleware-koa": "^1.4.2",
"graphql": "^0.13.2",
"graphql-tools": "^2.21.0",
"knex": "^0.14.4",
"koa": "^2.5.0",
"koa-bodyparser": "^4.2.0",
"koa-bunyan-logger": "^2.0.0",
"koa-compose": "^4.0.0",
"koa-router": "^7.4.0",
"lodash": "^4.17.5",
"ramda": "^0.25.0",
"sanitize-filename": "^1.6.1",
"sqlite3": "^3.1.13"
},
Expand All @@ -65,8 +58,6 @@
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-prettier": "^2.6.0",
"jest": "^22.4.2",
"nock": "^9.2.3",
"nodemon": "^1.17.1",
"prettier": "^1.11.1"
}
}
10 changes: 10 additions & 0 deletions api/data/src/config/graphqlPlayground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const hours = new Date().getHours();
const isDayTime = hours >= 6 && hours <= 18;

export default {
settings: {
'editor.theme': isDayTime ? 'light' : 'dark',
'editor.cursorShape': 'underline',
'tracing.hideTracingResponse': false,
},
};
94 changes: 0 additions & 94 deletions api/data/src/graphql/index.js

This file was deleted.

146 changes: 108 additions & 38 deletions api/data/src/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,111 @@
import Koa from 'koa';
import { ApolloServer, gql } from 'apollo-server';
import _ from 'lodash';
import playgroundConfig from './config/graphqlPlayground';
import log from './util/log';
import jsonData from './jsonData';

import Router from 'koa-router';
import bodyParser from 'koa-bodyparser';
import { graphqlKoa, graphiqlKoa } from 'apollo-server-koa';
import koaPlayground from 'graphql-playground-middleware-koa';
const typeDefs = gql`
# Describes a module for, may span different semesters
type Module {
code: String!
title: String!
department: String
description: String
credit: Float
workload: String
prerequisite: String
corequisite: String
corsBiddingStats: [CorsBiddingStats]
# Refers to the history of the module throughout semesters
history: [ModuleInfo]!
}
import Boom from 'boom';
import loggerMiddleware from 'koa-bunyan-logger';
import errorMiddleware from './middleware/error';
# Describes a particular module for a semester
type ModuleInfo {
semester: Int
examDate: String
examOpenBook: Boolean
examDuration: String
examVenue: String
timetable: [Lesson]
}
import log from './util/log';
import schema from './graphql';

const app = new Koa();
const router = new Router();

// Register middleware
app.use(bodyParser());
app.use(loggerMiddleware(log));
app.use(loggerMiddleware.requestIdContext());
app.use(loggerMiddleware.requestLogger());
app.use(errorMiddleware());

// Registers routes
router.post('/graphql', graphqlKoa({ schema, tracing: true }));
router.get('/graphiql', graphiqlKoa({ endpointURL: '/graphql' }));
router.get('/playground', koaPlayground({ endpoint: '/graphql' }));

app.use(router.routes());
app.use(
router.allowedMethods({
throw: true,
notImplemented: () => new Boom.notImplemented(), // eslint-disable-line new-cap
methodNotAllowed: () => new Boom.methodNotAllowed(), // eslint-disable-line new-cap
}),
);

log.info('current environment: %s', process.env.NODE_ENV);
log.info('server started at port: %d', process.env.PORT || 3600);
app.listen(process.env.PORT || 3600);
# Bidding stats for Cors
type CorsBiddingStats {
quota: Int
bidders: Int
lowestBid: Int
lowestSuccessfulBid: Int
highestBid: Int
faculty: String
studentAcctType: String
acadYear: String
semester: Int
round: String
group: String
}
# A lesson conducted, may it be a lecture, laboratory or lecture
type Lesson {
classNo: String!
lessonType: String!
weekText: String!
dayText: String!
startTime: String!
endTime: String!
venue: String!
}
# the schema allows the following query:
type Query {
modules(acadYear: String!, first: Int, offset: Int): [Module!]!
module(acadYear: String!, code: String!): Module
}
schema {
query: Query
}
`;

const resolvers = {
Query: {
modules(root, { acadYear, first, offset }) {
const yearData = jsonData[acadYear];
if (yearData == null) {
return [];
}
const modules = Object.values(yearData);
return modules.slice(offset, offset ? offset + first : first);
},
module(root, { acadYear, code }) {
return _.get(jsonData, [acadYear, code]);
},
},
};

const server = new ApolloServer({
typeDefs,
/* Apollo is mutating resolvers */
resolvers: { ...resolvers },
playground: playgroundConfig,
formatError: (error) => {
log.error(error);
return error;
},
formatResponse: (response) => {
log.info(response);
return response;
},
});

if (process.env.NODE_ENV !== 'test') {
server.listen().then(({ url }) => {
log.info(`🚀 Server ready at ${url}`);
});
}

/* For testing purposes */
export default {
typeDefs,
resolvers,
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { graphql } from 'graphql';
import schema from './index';
import { makeExecutableSchema } from 'graphql-tools';
import index from './index';

const schema = makeExecutableSchema(index);

const gql = (x) => x.raw[0]; // identify function for template literals

Expand Down Expand Up @@ -110,9 +113,9 @@ describe('graphql', () => {
}
}
`;
const { data } = await graphql(schema, query);
const { data: { module } } = await graphql(schema, query);

expect(data).toBeNull();
expect(module).toBeNull();
});

it('should not be null when module is valid', async () => {
Expand Down
File renamed without changes.
Loading

0 comments on commit 9a09950

Please sign in to comment.