Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos committed Jun 15, 2017
1 parent c444794 commit 1344bd3
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .babelrc
@@ -0,0 +1,10 @@
{
"passPerPreset": true,
"presets": [
"es2015",
"stage-0"
],
"plugins": [
"babel-plugin-inline-import"
]
}
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@

.idea/
node_modules/
19 changes: 19 additions & 0 deletions database/index.js
@@ -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;
5 changes: 5 additions & 0 deletions database/pagination/schema.graphql
@@ -0,0 +1,5 @@
interface Pagination {
pageSize: Int
pageNum: Int
total: Int,
}
23 changes: 23 additions & 0 deletions database/resolver.js
@@ -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;
10 changes: 10 additions & 0 deletions database/schema.graphql
@@ -0,0 +1,10 @@
type Query {

user(id: ID!): User

users(filters: UserQuery, pageNum: Int, pageSize: Int): UserPage
}

schema {
query: Query
}
21 changes: 21 additions & 0 deletions database/user/connector.js
@@ -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;
}
19 changes: 19 additions & 0 deletions database/user/schema.graphql
@@ -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]
}
47 changes: 47 additions & 0 deletions mock/user/index.js
@@ -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'
}
]

});

});

}
29 changes: 29 additions & 0 deletions package.json
@@ -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"
}
}
29 changes: 29 additions & 0 deletions server.js
@@ -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}`));
14 changes: 14 additions & 0 deletions utils/http.js
@@ -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;

0 comments on commit 1344bd3

Please sign in to comment.