Creating a "addRuleToCompetition" mutation
This commit is contained in:
1 parent
2fa444757b
commit
eeda497f97
2 files changed
+57
-1
No files matched your search
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
GraphQLNonNull,
|
||||
GraphQLID,
|
||||
GraphQLString
|
||||
} from 'graphql'
|
||||
import { CompetitionType } from '../types'
|
||||
import Competition from '../../models/competition'
|
||||
|
||||
const args = {
|
||||
codeName: { type: new GraphQLNonNull(GraphQLID) },
|
||||
title: { type: new GraphQLNonNull(GraphQLString) },
|
||||
description: { type: new GraphQLNonNull(GraphQLString) }
|
||||
}
|
||||
|
||||
const resolve = (parent, args, context) => {
|
||||
// let authIdOnSession = context.user.sub
|
||||
return new Promise((resolve, reject) => {
|
||||
Competition.findOne({ codeName: args.codeName }).exec((err, competition) => {
|
||||
if (err) reject(err)
|
||||
else {
|
||||
if (competition.rules.some(rule => rule.title === args.title)) {
|
||||
// need to add an error message here TODO
|
||||
resolve(competition)
|
||||
} else {
|
||||
let number = 1
|
||||
while (competition.rules.some(rule => rule.number === number)) {
|
||||
number++
|
||||
}
|
||||
competition.rules.push({
|
||||
number,
|
||||
title: args.title,
|
||||
description: args.description
|
||||
})
|
||||
competition.save((err) => {
|
||||
if (err) reject(err)
|
||||
else {
|
||||
resolve(competition)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const mutation = {
|
||||
addRuleToCompetition: {
|
||||
type: CompetitionType,
|
||||
args,
|
||||
resolve
|
||||
}
|
||||
}
|
||||
|
||||
export default mutation
|
||||
@@ -4,12 +4,14 @@ import {
|
||||
|
||||
import HostCompetitionMutation from './HostCompetition'
|
||||
import RegisterToCompetitionMutation from './RegisterToCompetition'
|
||||
import AddRuleToCompetitionMutation from './AddRuleToCompetition'
|
||||
|
||||
const Mutation = new GraphQLObjectType({
|
||||
name: 'Mutation',
|
||||
fields: Object.assign(
|
||||
HostCompetitionMutation,
|
||||
RegisterToCompetitionMutation
|
||||
RegisterToCompetitionMutation,
|
||||
AddRuleToCompetitionMutation
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user