From e8b7bdee6bca4bede3544dad7e46d12a89cb38f5 Mon Sep 17 00:00:00 2001 From: zhoutk Date: Thu, 11 Apr 2019 11:46:03 +0800 Subject: [PATCH] finish all custom business design. --- package.json | 1 - src/graphql/post.ts | 32 ++++++++++++++++------ src/graphql/reviseResult.ts | 31 +++++++++++++-------- src/middlewares/graphql/index.ts | 8 ++---- src/middlewares/graphql/schema_generate.ts | 18 +++++++----- 5 files changed, 55 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index f00ea06..2910cea 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,6 @@ "cli-color": "^1.3.0", "debug": "^4.0.1", "graphql": "^14.2.0", - "graphql-tools": "^4.0.4", "jsonwebtoken": "^8.3.0", "koa": "^2.5.3", "koa-body": "^4.0.4", diff --git a/src/graphql/post.ts b/src/graphql/post.ts index 834823d..535a677 100644 --- a/src/graphql/post.ts +++ b/src/graphql/post.ts @@ -1,13 +1,16 @@ import BaseDao from '../db/baseDao' -const customDefs = ` - type Post { - id: Int! - title: String - } -` - -let queryDefs = ['posts: [Post]'] +const customDefs = { + textDefs: ` + type Post { + id: Int! + title: String + author: Author + } + `, + queryDefs: ['posts: [Post]'], + mutationDefs: ['addPost(title: String!, author_id: Int!): ReviseResult'] +} const customResolvers = { Query: { @@ -15,7 +18,18 @@ const customResolvers = { let rs = await new BaseDao('book').retrieve({}) return rs.data } + }, + Mutation: { + addPost: (_, args) => { + return new BaseDao('book').create(args) + } + }, + Post: { + author: async (element) => { + let rs = await new BaseDao('author').retrieve({ id: element.author_id }) + return rs.data[0] + } } } -export { customDefs, queryDefs, customResolvers } \ No newline at end of file +export { customDefs, customResolvers } \ No newline at end of file diff --git a/src/graphql/reviseResult.ts b/src/graphql/reviseResult.ts index 1f14b8e..54ae016 100644 --- a/src/graphql/reviseResult.ts +++ b/src/graphql/reviseResult.ts @@ -1,14 +1,21 @@ -const customDefs = ` - type ReviseResult { - id: Int - affectedRows: Int - status: Int - message: String - } -` - -let queryDefs = [] +const customDefs = { + textDefs: ` + type ReviseResult { + id: Int + affectedRows: Int + status: Int + message: String + } + `, + queryDefs: [], + mutationDefs: [] +} -const customResolvers = { Query: {} } +const customResolvers = { + Query: { + }, + Mutation: { + } + } -export { customDefs, queryDefs, customResolvers } \ No newline at end of file +export { customDefs, customResolvers } \ No newline at end of file diff --git a/src/middlewares/graphql/index.ts b/src/middlewares/graphql/index.ts index dec7a66..cd5289d 100644 --- a/src/middlewares/graphql/index.ts +++ b/src/middlewares/graphql/index.ts @@ -1,16 +1,12 @@ import { getInfoFromSql } from './schema_generate' const { ApolloServer } = require('apollo-server-koa') -const { makeExecutableSchema } = require('graphql-tools') export default async (app) => { let { typeDefs, resolvers } = await getInfoFromSql() - const schema = makeExecutableSchema({ - typeDefs, - resolvers, - }) if (!G.ApolloServer) { G.ApolloServer = new ApolloServer({ - schema, + typeDefs, + resolvers, context: ({ ctx }) => ({ ...ctx, ...app.context diff --git a/src/middlewares/graphql/schema_generate.ts b/src/middlewares/graphql/schema_generate.ts index 9bdd20f..82b5773 100644 --- a/src/middlewares/graphql/schema_generate.ts +++ b/src/middlewares/graphql/schema_generate.ts @@ -129,23 +129,27 @@ async function getInfoFromSql() { } } - let typeDefs = '' + let typeDefs = [] let dirGraphql = requireDir('../../graphql') G.L.each(dirGraphql, (item, name) => { - if (item && item.customDefs && item.queryDefs && item.customResolvers) { - typeDefs += item.customDefs - typeDefObj.query = typeDefObj.query.concat(item.queryDefs) - Object.assign(resolvers.Query, item.customResolvers.Query) + if (item && item.customDefs && item.customResolvers) { + typeDefs.push(item.customDefs.textDefs || '') + typeDefObj.query = typeDefObj.query.concat(item.customDefs.queryDefs || []) + typeDefObj.mutation = typeDefObj.mutation.concat(item.customDefs.mutationDefs || []) + let { Query, Mutation, ...Other } = item.customResolvers + Object.assign(resolvers.Query, Query) + Object.assign(resolvers.Mutation, Mutation) + Object.assign(resolvers, Other) } }) - typeDefs += Object.entries(typeDefObj).reduce((total, cur) => { + typeDefs.push(Object.entries(typeDefObj).reduce((total, cur) => { return total += ` type ${G.tools.bigCamelCase(cur[0])} { ${cur[1].join('')} } ` - }, '') + }, '')) return { typeDefs, resolvers } }