feat: GraphQL mutations for User + Group

This commit is contained in:
NGPixel 2017-08-08 22:36:29 -04:00
parent e0b788501d
commit 7e1cb3d171
5 changed files with 58 additions and 18 deletions

View File

@ -5,20 +5,17 @@
const gqlTools = require('graphql-tools') const gqlTools = require('graphql-tools')
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const _ = require('lodash')
const typeDefs = fs.readFileSync(path.join(wiki.SERVERPATH, 'schemas/types.graphql'), 'utf8') const typeDefs = fs.readFileSync(path.join(wiki.SERVERPATH, 'schemas/types.graphql'), 'utf8')
const GroupResolvers = require('../schemas/resolvers-group') const GroupResolvers = require('../schemas/resolvers-group')
const UserResolvers = require('../schemas/resolvers-user') const UserResolvers = require('../schemas/resolvers-user')
const resolvers = { const resolvers = _.merge(
Query: { GroupResolvers,
groups: GroupResolvers.Query, UserResolvers
users: UserResolvers.Query )
},
Group: GroupResolvers.Type,
User: UserResolvers.Type
}
const Schema = gqlTools.makeExecutableSchema({ const Schema = gqlTools.makeExecutableSchema({
typeDefs, typeDefs,

View File

@ -3,10 +3,17 @@
/* global wiki */ /* global wiki */
module.exports = { module.exports = {
Query(obj, args, context, info) { Query: {
return wiki.db.Group.findAll({ where: args }) groups(obj, args, context, info) {
return wiki.db.Group.findAll({ where: args })
}
}, },
Type: { Mutation: {
createGroup(obj, args) {
return wiki.db.Group.create(args)
}
},
Group: {
users(grp) { users(grp) {
return grp.getUsers() return grp.getUsers()
} }

View File

@ -3,10 +3,17 @@
/* global wiki */ /* global wiki */
module.exports = { module.exports = {
Query(obj, args, context, info) { Query: {
return wiki.db.User.findAll({ where: args }) users(obj, args, context, info) {
return wiki.db.User.findAll({ where: args })
}
}, },
Type: { Mutation: {
createUser(obj, args) {
return wiki.db.User.create(args)
}
},
User: {
groups(usr) { groups(usr) {
return usr.getGroups() return usr.getGroups()
} }

View File

@ -102,6 +102,7 @@ type Setting implements Base {
config: String! config: String!
} }
# Tags are attached to one or more documents
type Tag implements Base { type Tag implements Base {
id: Int! id: Int!
createdOn: Date createdOn: Date
@ -109,20 +110,20 @@ type Tag implements Base {
key: String! key: String!
} }
# A User
type User implements Base { type User implements Base {
id: Int! id: Int!
createdOn: Date createdOn: Date
updatedOn: Date updatedOn: Date
email: String! email: String!
provider: String provider: String!
providerId: String providerId: String
name: String name: String
role: UserRole! role: UserRole!
groups: [Group] groups: [Group]
} }
# QUERY # Query (Read)
type Query { type Query {
comments(id: Int): [Comment] comments(id: Int): [Comment]
documents(id: Int, path: String): [Document] documents(id: Int, path: String): [Document]
@ -134,3 +135,31 @@ type Query {
tags(key: String): [Tag] tags(key: String): [Tag]
users(id: Int, email: String, provider: String, providerId: String, role: UserRole): [User] users(id: Int, email: String, provider: String, providerId: String, role: UserRole): [User]
} }
# Mutations (Create, Update, Delete)
type Mutation {
assignUserToGroup(
userId: Int!
groupId: Int!
): Boolean
createGroup(
name: String!
): Group
createUser(
email: String!
name: String
provider: String!
providerId: String
role: UserRole!
): User
deleteGroup(
id: Int!
): Boolean
deleteUser(
id: Int!
): Boolean
removeUserFromGroup(
userId: Int!
groupId: Int!
): Boolean
}

View File

@ -140,7 +140,7 @@ globalTasks.then(() => {
nodemon({ nodemon({
exec: (args.d) ? 'node server' : 'node wiki configure', exec: (args.d) ? 'node server' : 'node wiki configure',
ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'], ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
ext: 'js json', ext: 'js json graphql',
watch: (args.d) ? ['server'] : ['server/configure.js'], watch: (args.d) ? ['server'] : ['server/configure.js'],
env: { 'NODE_ENV': 'development' } env: { 'NODE_ENV': 'development' }
}) })