-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,541 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
NODE_ENV=development | ||
DB_DEBUG=false | ||
PORT=8081 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { gql } from 'apollo-server'; | ||
import _ from 'lodash'; | ||
import jsonData from './jsonData'; | ||
|
||
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]! | ||
} | ||
# Describes a particular module for a semester | ||
type ModuleInfo { | ||
semester: Int | ||
examDate: String | ||
examOpenBook: Boolean | ||
examDuration: String | ||
examVenue: String | ||
timetable: [Lesson] | ||
} | ||
# 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]); | ||
}, | ||
}, | ||
}; | ||
|
||
export default { | ||
typeDefs, | ||
resolvers, | ||
}; |
2 changes: 1 addition & 1 deletion
2
api/data/src/index.test.js → api/data/src/graphql/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,36 @@ | ||
import { ApolloServer, gql } from 'apollo-server'; | ||
import _ from 'lodash'; | ||
import { ApolloServer } from 'apollo-server'; | ||
import playgroundConfig from './config/graphqlPlayground'; | ||
import log from './util/log'; | ||
import jsonData from './jsonData'; | ||
|
||
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]! | ||
} | ||
# Describes a particular module for a semester | ||
type ModuleInfo { | ||
semester: Int | ||
examDate: String | ||
examOpenBook: Boolean | ||
examDuration: String | ||
examVenue: String | ||
timetable: [Lesson] | ||
} | ||
# 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); | ||
function makeServer() { | ||
// enable hot-reload server side | ||
// eslint-disable-next-line global-require | ||
const graphqlSchema = require('./graphql').default; | ||
return new ApolloServer({ | ||
typeDefs: graphqlSchema.typeDefs, | ||
/* Apollo is mutating resolvers */ | ||
resolvers: { ...graphqlSchema.resolvers }, | ||
playground: playgroundConfig, | ||
formatError: (error) => { | ||
log.error(error); | ||
return error; | ||
}, | ||
module(root, { acadYear, code }) { | ||
return _.get(jsonData, [acadYear, code]); | ||
formatResponse: (response) => { | ||
log.info(response); | ||
return response; | ||
}, | ||
}, | ||
}; | ||
}); | ||
} | ||
|
||
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; | ||
}, | ||
let serverInstance = makeServer(); | ||
serverInstance.listen(process.env.PORT).then(({ url }) => { | ||
log.info(`🚀 Server ready at ${url}`); | ||
}); | ||
|
||
if (process.env.NODE_ENV !== 'test') { | ||
server.listen().then(({ url }) => { | ||
log.info(`🚀 Server ready at ${url}`); | ||
if (module.hot) { | ||
module.hot.accept('./graphql', async () => { | ||
await serverInstance.stop(); | ||
serverInstance = makeServer(); | ||
await serverInstance.listen(process.env.PORT); | ||
}); | ||
} | ||
|
||
/* For testing purposes */ | ||
export default { | ||
typeDefs, | ||
resolvers, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require('dotenv').config(); | ||
const webpack = require('webpack'); | ||
const path = require('path'); | ||
const nodeExternals = require('webpack-node-externals'); | ||
const StartServerPlugin = require('start-server-webpack-plugin'); | ||
|
||
const IS_DEV = process.env.NODE_ENV === 'development'; | ||
|
||
module.exports = { | ||
mode: IS_DEV ? 'development' : 'production', | ||
entry: ['webpack/hot/poll?1000', './src/index'], | ||
watch: IS_DEV, | ||
target: 'node', | ||
stats: 'minimal', | ||
externals: [ | ||
nodeExternals({ | ||
whitelist: ['webpack/hot/poll?1000'], | ||
}), | ||
], | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js?$/, | ||
use: 'babel-loader', | ||
exclude: /node_modules/, | ||
}, | ||
], | ||
}, | ||
plugins: [ | ||
new StartServerPlugin('server.js'), | ||
new webpack.HotModuleReplacementPlugin(), | ||
new webpack.NoEmitOnErrorsPlugin(), | ||
new webpack.EnvironmentPlugin(['NODE_ENV']), | ||
], | ||
output: { | ||
path: path.join(__dirname, 'build'), | ||
filename: 'server.js', | ||
}, | ||
}; |
Oops, something went wrong.