Refactoring queries and mutations, removing default exports

This commit is contained in:
Nikola Bjelo committed 2018-10-19 14:23:25 +02:00
1 parent e07f0ba077
commit 039bca1571
7 files changed
+111 -60

No files matched your search

+13 -59
View File
@@ -1,73 +1,27 @@
import {
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
GraphQLList,
GraphQLNonNull
GraphQLSchema
} from 'graphql'
import { slugify } from '../utils/functions'
import Competition from '../models/competition'
import { CompetitionType } from './types'
import { HostCompetitionMutation } from './mutations'
import {
CompetitionsQuery,
CompetitionsByHostQuery
} from './queries'
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
competitions: {
type: new GraphQLList(CompetitionType),
resolve (parent, args, context) {
return Competition.find()
}
},
competitionsByHost: {
type: new GraphQLList(CompetitionType),
args: {
host: { type: new GraphQLNonNull(GraphQLString) }
},
resolve (parent, args, context) {
return Competition.find({
host: args.host
})
}
}
}
fields: Object.assign(
CompetitionsQuery,
CompetitionsByHostQuery
)
})
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
hostCompetition: {
type: CompetitionType,
args: {
displayName: { type: new GraphQLNonNull(GraphQLString) },
description: { type: new GraphQLNonNull(GraphQLString) },
startDatetime: { type: new GraphQLNonNull(GraphQLString) },
finishDatetime: { type: new GraphQLNonNull(GraphQLString) },
formula: { type: new GraphQLNonNull(GraphQLString) }
},
resolve (parent, args, context) {
// let authIdOnSession = context.user.sub
let authIdOnSession = 'some-id'
let newCompetition = new Competition({
host: authIdOnSession,
displayName: args.displayName,
description: args.description,
// startDatetime: args.startDatetime,
// finishDatetime: args.finishDatetime,
formula: args.formula
})
newCompetition.codeName = slugify(newCompetition.displayName) + '-' + newCompetition._id
return new Promise((resolve, reject) => {
newCompetition.save((err) => {
if (err) reject(err)
else {
resolve(newCompetition)
}
})
})
}
}
}
fields: Object.assign(
HostCompetitionMutation
)
})
const Schema = new GraphQLSchema({
@@ -0,0 +1,45 @@
import {
GraphQLNonNull,
GraphQLString
} from 'graphql'
import { CompetitionType } from '../types'
import Competition from '../../models/competition'
import { slugify } from '../../utils/functions'
const args = {
displayName: { type: new GraphQLNonNull(GraphQLString) },
description: { type: new GraphQLNonNull(GraphQLString) },
startDatetime: { type: new GraphQLNonNull(GraphQLString) },
finishDatetime: { type: new GraphQLNonNull(GraphQLString) },
formula: { type: new GraphQLNonNull(GraphQLString) }
}
const mutation = {
hostCompetition: {
type: CompetitionType,
args,
resolve (parent, args, context) {
// let authIdOnSession = context.user.sub
let authIdOnSession = 'some-id'
let newCompetition = new Competition({
host: authIdOnSession,
displayName: args.displayName,
description: args.description,
// startDatetime: args.startDatetime,
// finishDatetime: args.finishDatetime,
formula: args.formula
})
newCompetition.codeName = slugify(newCompetition.displayName) + '-' + newCompetition._id
return new Promise((resolve, reject) => {
newCompetition.save((err) => {
if (err) reject(err)
else {
resolve(newCompetition)
}
})
})
}
}
}
export default mutation
+5
View File
@@ -0,0 +1,5 @@
import HostCompetitionMutation from './HostCompetition'
export {
HostCompetitionMutation
}
+16
View File
@@ -0,0 +1,16 @@
import {
GraphQLList
} from 'graphql'
import { CompetitionType } from '../types'
import Competition from '../../models/competition'
const query = {
competitions: {
type: new GraphQLList(CompetitionType),
resolve (parent, args, context) {
return Competition.find()
}
}
}
export default query
@@ -0,0 +1,25 @@
import {
GraphQLList,
GraphQLString,
GraphQLNonNull
} from 'graphql'
import { CompetitionType } from '../types'
import Competition from '../../models/competition'
const args = {
host: { type: new GraphQLNonNull(GraphQLString) }
}
const query = {
competitionsByHost: {
type: new GraphQLList(CompetitionType),
args,
resolve (parent, args, context) {
return Competition.find({
host: args.host
})
}
}
}
export default query
+7
View File
@@ -0,0 +1,7 @@
import CompetitionsQuery from './Competitions'
import CompetitionsByHostQuery from './CompetitionsByHost'
export {
CompetitionsQuery,
CompetitionsByHostQuery
}
-1
View File
@@ -13,4 +13,3 @@ export {
PrizeType,
CompetitionType
}
export default CompetitionType