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
12 changed files
with
229 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"passPerPreset": true, | ||
"presets": [ | ||
"es2015", | ||
"stage-0" | ||
], | ||
"plugins": [ | ||
"babel-plugin-inline-import" | ||
] | ||
} |
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,3 @@ | ||
|
||
.idea/ | ||
node_modules/ |
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,19 @@ | ||
/** | ||
* @author Kuitos | ||
* @homepage https://github.com/kuitos/ | ||
* @since 2017-06-12 | ||
*/ | ||
import userSchema from './user/schema.graphql'; | ||
import rootSchema from './schema.graphql'; | ||
import paginationSchema from './pagination/schema.graphql'; | ||
import resolvers from './resolver'; | ||
import { addErrorLoggingToSchema, makeExecutableSchema } from 'graphql-tools'; | ||
|
||
const schema = makeExecutableSchema({ typeDefs: [rootSchema, userSchema, paginationSchema], resolvers }); | ||
addErrorLoggingToSchema(schema, { | ||
log(e){ | ||
console.error(e.stack); | ||
} | ||
}); | ||
|
||
export default schema; |
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,5 @@ | ||
interface Pagination { | ||
pageSize: Int | ||
pageNum: Int | ||
total: Int, | ||
} |
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,23 @@ | ||
/** | ||
* @author Kuitos | ||
* @homepage https://github.com/kuitos/ | ||
* @since 2017-06-12 | ||
*/ | ||
|
||
import { getUser, getUsers } from './User/connector'; | ||
|
||
const resolver = { | ||
|
||
Query: { | ||
user(root, { id }) { | ||
return getUser(id); | ||
}, | ||
|
||
users(root, { filters, pageNum, pageSize }) { | ||
return getUsers(filters, pageNum, pageSize); | ||
} | ||
} | ||
|
||
}; | ||
|
||
export default resolver; |
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,10 @@ | ||
type Query { | ||
|
||
user(id: ID!): User | ||
|
||
users(filters: UserQuery, pageNum: Int, pageSize: Int): UserPage | ||
} | ||
|
||
schema { | ||
query: Query | ||
} |
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,21 @@ | ||
/** | ||
* @author Kuitos | ||
* @homepage https://github.com/kuitos/ | ||
* @since 2017-06-12 | ||
*/ | ||
|
||
import http from '../../utils/http'; | ||
import qs from 'querystring'; | ||
|
||
export async function getUser(userId) { | ||
|
||
const user = await http.get(`http://127.0.0.1:8080/users/${userId}`).then(res => res.data); | ||
return user; | ||
} | ||
|
||
export async function getUsers(queryParams, pageNum, pageSize) { | ||
|
||
const params = qs.stringify({ ...queryParams, pageNum, pageSize }); | ||
const users = await http.get(`http://127.0.0.1:8080/users?${params}`).then(res => res.data); | ||
return users; | ||
} |
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,19 @@ | ||
type User { | ||
id: ID! | ||
userName: String | ||
age: Int | ||
gender: String | ||
} | ||
|
||
input UserQuery { | ||
userName: String | ||
age: Int | ||
gender: String | ||
} | ||
|
||
type UserPage implements Pagination { | ||
pageSize: Int | ||
pageNum: Int | ||
total: Int | ||
data: [User] | ||
} |
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,47 @@ | ||
/** | ||
* @author Kuitos | ||
* @homepage https://github.com/kuitos/ | ||
* @since 2017-06-15 | ||
*/ | ||
|
||
export default router => { | ||
|
||
router.get('/users/:id', ({ params }, res) => { | ||
const { id } = params; | ||
res.send({ | ||
id, | ||
userName: `kuitos ${id}`, | ||
age: Number(id), | ||
gender: 'miss' | ||
}); | ||
}); | ||
|
||
router.get('/users', ({ query }, res) => { | ||
|
||
const { pageSize, pageNum, ...params } = query; | ||
|
||
res.send({ | ||
|
||
total: 50, | ||
pageSize, | ||
pageNum, | ||
data: [ | ||
{ | ||
id: params.id || 123, | ||
userName: 'kuitos', | ||
age: 18, | ||
gender: 'miss' | ||
}, | ||
{ | ||
id: params.id || 123, | ||
userName: 'kuitos', | ||
age: 18, | ||
gender: 'miss' | ||
} | ||
] | ||
|
||
}); | ||
|
||
}); | ||
|
||
} |
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,29 @@ | ||
{ | ||
"name": "apollo-server", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "cross-env NODE_ENV=development nodemon ./server.js --exec babel-node" | ||
}, | ||
"dependencies": { | ||
"axios": "^0.16.2", | ||
"body-parser": "~1.17.1", | ||
"cookie-parser": "~1.4.3", | ||
"cors": "^2.8.3", | ||
"debug": "~2.6.3", | ||
"express": "~4.15.2", | ||
"glob": "^7.1.2", | ||
"graphql": "^0.10.1", | ||
"graphql-server-express": "^0.8.0", | ||
"graphql-tools": "^1.0.0", | ||
"morgan": "^1.8.2" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-plugin-inline-import": "^2.0.5", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-stage-0": "^6.24.1", | ||
"cross-env": "^5.0.1", | ||
"nodemon": "^1.11.0" | ||
} | ||
} |
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,29 @@ | ||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
import logger from 'morgan'; | ||
import cors from 'cors'; | ||
import glob from 'glob'; | ||
import path from 'path'; | ||
import { graphiqlExpress, graphqlExpress } from 'graphql-server-express'; | ||
import schema from './database'; | ||
|
||
const PORT = 8080; | ||
|
||
const app = express(); | ||
|
||
app.use(cors()); | ||
app.use(logger('combined')); | ||
|
||
// bodyParser is needed just for POST. | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(bodyParser.json()); | ||
app.use('/graphql', graphqlExpress({ schema })); | ||
app.use('/graphiql', graphiqlExpress({ | ||
endpointURL: '/graphql' | ||
})); | ||
|
||
if (process.env.NODE_ENV === 'development') { | ||
glob(path.resolve(__dirname, './mock/**/*.js'), {}, (er, modules) => modules.forEach(module => require(module).default(app))); | ||
} | ||
|
||
app.listen(PORT, () => console.log(`> Listening at port ${PORT}`)); |
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,14 @@ | ||
/** | ||
* @author Kuitos | ||
* @homepage https://github.com/kuitos/ | ||
* @since 2017-06-15 | ||
*/ | ||
|
||
import axios from 'axios'; | ||
|
||
const http = axios.create({ | ||
baseURL: '/', | ||
headers: { 'Cache-Control': 'no-cache' } | ||
}); | ||
|
||
export default http; |