module.exports = {1: {id: 1,avatar: 'https://static001.geekbang.org/account/avatar/00/0f/52/62/1b3ebed5.jpg',name: '僵尸浩',isTop: true,content: '哈哈哈哈',publishDate: '今天',commentNum: 10,praiseNum: 5},2: {id: 2,avatar: 'https://static001.geekbang.org/account/avatar/00/0f/52/62/1b3ebed5.jpg',name: '极客主编',isTop: true,content: '我来送大礼了!!',publishDate: '上周',commentNum: 10,praiseNum: 2},3: {id: 3,avatar: 'https://static001.geekbang.org/account/avatar/00/0f/52/62/1b3ebed5.jpg',name: '极客老板',isTop: true,content: '我来发股票了!!!',publishDate: '十年前',commentNum: 10,praiseNum: 0}}
const { graphql, buildSchema } = require('graphql');const mockDatabase = require('./mock-database')const schema = buildSchema(`type Comment {id: Intavatar: Stringname: StringisTop: Booleancontent: StringpublishDate: StringcommentNum: IntpraiseNum: Int}type Query {comment: [Comment]}type Mutation {praise(id: Int): Int}`)schema.getQueryType().getFields().comment.resolve = () => {return Object.keys(mockDatabase).map(key=> {return mockDatabase[key];})}schema.getMutationType().getFields().praise.resolve = (args0, { id }) => {mockDatabase[id].praiseNum++;return mockDatabase[id].praiseNum}module.exports = schema;
const fs = require('fs');const app = new (require('koa'));const mount = require('koa-mount');const static = require('koa-static');const graphqlHTTP = require('koa-graphql');app.use(// 给koa-graphql传一个graphql的协议文件,就会自动帮你生成graphql-apimount('/api', graphqlHTTP({schema: require('./schema')})))app.use(mount('/static', static(`${__dirname}/source/static`)))app.use(mount('/', async (ctx) => {ctx.status = 200;ctx.body = fs.readFileSync(`${__dirname}/source/index.htm`, 'utf-8')}))// module.exports = app;app.listen(3000)