Skip to content

Commit

Permalink
finish all custom business design.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoutk committed Apr 11, 2019
1 parent 2af69bc commit e8b7bde
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 35 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 23 additions & 9 deletions src/graphql/post.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
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: {
posts: async () => {
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 }
export { customDefs, customResolvers }
31 changes: 19 additions & 12 deletions src/graphql/reviseResult.ts
Original file line number Diff line number Diff line change
@@ -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 }
export { customDefs, customResolvers }
8 changes: 2 additions & 6 deletions src/middlewares/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 11 additions & 7 deletions src/middlewares/graphql/schema_generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down

0 comments on commit e8b7bde

Please sign in to comment.