Creating a "registerToCompetition" mutation

Have to add error message if creation duplication was avoided
This commit is contained in:
Nikola Bjelo committed 2018-10-19 16:46:28 +02:00
1 parent 8d036a89f8
commit 758368161b
3 files changed
+58 -3

No files matched your search

+6 -2
View File
@@ -3,7 +3,10 @@ import {
GraphQLSchema
} from 'graphql'
import { HostCompetitionMutation } from './mutations'
import {
HostCompetitionMutation,
RegisterToCompetitionMutation
} from './mutations'
import {
CompetitionsQuery,
CompetitionsByHostQuery
@@ -20,7 +23,8 @@ const RootQuery = new GraphQLObjectType({
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: Object.assign(
HostCompetitionMutation
HostCompetitionMutation,
RegisterToCompetitionMutation
)
})
@@ -0,0 +1,49 @@
import {
GraphQLNonNull,
GraphQLID,
GraphQLString
} from 'graphql'
import { CompetitionType } from '../types'
import Competition from '../../models/competition'
const args = {
codeName: { type: new GraphQLNonNull(GraphQLID) },
devTeam: { type: new GraphQLNonNull(GraphQLString) },
bot: { type: new GraphQLNonNull(GraphQLString) },
release: { type: new GraphQLNonNull(GraphQLString) }
}
const mutation = {
registerToCompetition: {
type: CompetitionType,
args,
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.participants.some(participant => participant.devTeam === args.devTeam)) {
// need to add an error message here TODO
resolve(competition)
} else {
competition.participants.push({
devTeam: args.devTeam,
bot: args.bot,
release: args.release
})
competition.save((err) => {
if (err) reject(err)
else {
resolve(competition)
}
})
}
}
})
})
}
}
}
export default mutation
+3 -1
View File
@@ -1,5 +1,7 @@
import HostCompetitionMutation from './HostCompetition'
import RegisterToCompetitionMutation from './RegisterToCompetition'
export {
HostCompetitionMutation
HostCompetitionMutation,
RegisterToCompetitionMutation
}