diff --git a/server/src/schema.js b/server/src/schema.js deleted file mode 100644 index e06d93252..000000000 --- a/server/src/schema.js +++ /dev/null @@ -1,166 +0,0 @@ -import { - GraphQLObjectType, - GraphQLSchema, - GraphQLID, - GraphQLString, - GraphQLInt, - GraphQLList, - GraphQLNonNull -} from 'graphql' -import { slugify } from './utils/functions' -import Competition from './models/competition' - -const PlotterType = new GraphQLObjectType({ - name: 'Plotter', - fields: () => ({ - codeName: { type: GraphQLString }, - host: { type: GraphQLString }, - repo: { type: GraphQLString }, - moduleName: { type: GraphQLString } - }) -}) - -const RuleType = new GraphQLObjectType({ - name: 'Rule', - fields: () => ({ - number: { type: GraphQLInt }, - title: { type: GraphQLString }, - description: { type: GraphQLString } - }) -}) - -const ParticipantType = new GraphQLObjectType({ - name: 'Participant', - fields: () => ({ - devTeam: { type: GraphQLString }, - bot: { type: GraphQLString }, - release: { type: GraphQLString } - }) -}) - -const OtherPrizeType = new GraphQLObjectType({ - name: 'OtherPrize', - fields: () => ({ - condition: { type: GraphQLString }, - amount: { type: GraphQLInt }, - asset: { type: GraphQLString } - }) -}) - -const PrizeType = new GraphQLObjectType({ - name: 'Prize', - fields: () => ({ - position: { type: GraphQLInt }, - algoPrize: { type: GraphQLInt }, - otherPrizes: { - type: new GraphQLList(OtherPrizeType), - resolve (parent, args) { - return parent.otherPrizes - } - } - }) -}) - -const CompetitionType = new GraphQLObjectType({ - name: 'Competition', - fields: () => ({ - codeName: { type: GraphQLID }, - displayName: { type: GraphQLString }, - host: { type: GraphQLString }, - description: { type: GraphQLString }, - startDatetime: { type: GraphQLString }, - finishDatetime: { type: GraphQLString }, - formula: { type: GraphQLString }, - plotter: { - type: PlotterType, - resolve (parent, args) { - return parent.plotter - } - }, - rules: { - type: new GraphQLList(RuleType), - resolve (parent, args) { - return parent.rules - } - }, - participants: { - type: new GraphQLList(ParticipantType), - resolve (parent, args) { - return parent.participants - } - }, - prizes: { - type: new GraphQLList(PrizeType), - resolve (parent, args) { - return parent.prizes - } - } - }) -}) - -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 - }) - } - } - } -}) - -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) - } - }) - }) - } - } - } -}) - -const Schema = new GraphQLSchema({ - query: RootQuery, - mutation: Mutation -}) - -export default Schema diff --git a/server/src/schema/index.js b/server/src/schema/index.js new file mode 100644 index 000000000..79102d8fe --- /dev/null +++ b/server/src/schema/index.js @@ -0,0 +1,78 @@ +import { + GraphQLObjectType, + GraphQLSchema, + GraphQLString, + GraphQLList, + GraphQLNonNull +} from 'graphql' +import { slugify } from '../utils/functions' + +import Competition from '../models/competition' +import { CompetitionType } from './types' + +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 + }) + } + } + } +}) + +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) + } + }) + }) + } + } + } +}) + +const Schema = new GraphQLSchema({ + query: RootQuery, + mutation: Mutation +}) + +export default Schema diff --git a/server/src/schema/types/Competition.js b/server/src/schema/types/Competition.js new file mode 100644 index 000000000..9b4f9728e --- /dev/null +++ b/server/src/schema/types/Competition.js @@ -0,0 +1,51 @@ +import { + GraphQLObjectType, + GraphQLString, + GraphQLList, + GraphQLID +} from 'graphql' +import { + PlotterType, + RuleType, + ParticipantType, + PrizeType +} from './index' + +const CompetitionType = new GraphQLObjectType({ + name: 'Competition', + fields: () => ({ + codeName: { type: GraphQLID }, + displayName: { type: GraphQLString }, + host: { type: GraphQLString }, + description: { type: GraphQLString }, + startDatetime: { type: GraphQLString }, + finishDatetime: { type: GraphQLString }, + formula: { type: GraphQLString }, + plotter: { + type: PlotterType, + resolve (parent, args) { + return parent.plotter + } + }, + rules: { + type: new GraphQLList(RuleType), + resolve (parent, args) { + return parent.rules + } + }, + participants: { + type: new GraphQLList(ParticipantType), + resolve (parent, args) { + return parent.participants + } + }, + prizes: { + type: new GraphQLList(PrizeType), + resolve (parent, args) { + return parent.prizes + } + } + }) +}) + +export default CompetitionType diff --git a/server/src/schema/types/OtherPrize.js b/server/src/schema/types/OtherPrize.js new file mode 100644 index 000000000..6fb186283 --- /dev/null +++ b/server/src/schema/types/OtherPrize.js @@ -0,0 +1,16 @@ +import { + GraphQLObjectType, + GraphQLString, + GraphQLInt +} from 'graphql' + +const OtherPrizeType = new GraphQLObjectType({ + name: 'OtherPrize', + fields: () => ({ + condition: { type: GraphQLString }, + amount: { type: GraphQLInt }, + asset: { type: GraphQLString } + }) +}) + +export default OtherPrizeType diff --git a/server/src/schema/types/Participant.js b/server/src/schema/types/Participant.js new file mode 100644 index 000000000..13f27da4f --- /dev/null +++ b/server/src/schema/types/Participant.js @@ -0,0 +1,15 @@ +import { + GraphQLObjectType, + GraphQLString +} from 'graphql' + +const ParticipantType = new GraphQLObjectType({ + name: 'Participant', + fields: () => ({ + devTeam: { type: GraphQLString }, + bot: { type: GraphQLString }, + release: { type: GraphQLString } + }) +}) + +export default ParticipantType diff --git a/server/src/schema/types/Plotter.js b/server/src/schema/types/Plotter.js new file mode 100644 index 000000000..ee2b3de67 --- /dev/null +++ b/server/src/schema/types/Plotter.js @@ -0,0 +1,16 @@ +import { + GraphQLObjectType, + GraphQLString +} from 'graphql' + +const PlotterType = new GraphQLObjectType({ + name: 'Plotter', + fields: () => ({ + codeName: { type: GraphQLString }, + host: { type: GraphQLString }, + repo: { type: GraphQLString }, + moduleName: { type: GraphQLString } + }) +}) + +export default PlotterType diff --git a/server/src/schema/types/Prize.js b/server/src/schema/types/Prize.js new file mode 100644 index 000000000..5588afd9a --- /dev/null +++ b/server/src/schema/types/Prize.js @@ -0,0 +1,22 @@ +import { + GraphQLObjectType, + GraphQLList, + GraphQLInt +} from 'graphql' +import { OtherPrizeType } from './index' + +const PrizeType = new GraphQLObjectType({ + name: 'Prize', + fields: () => ({ + position: { type: GraphQLInt }, + algoPrize: { type: GraphQLInt }, + otherPrizes: { + type: new GraphQLList(OtherPrizeType), + resolve (parent, args) { + return parent.otherPrizes + } + } + }) +}) + +export default PrizeType diff --git a/server/src/schema/types/Rule.js b/server/src/schema/types/Rule.js new file mode 100644 index 000000000..f8a6b98ef --- /dev/null +++ b/server/src/schema/types/Rule.js @@ -0,0 +1,16 @@ +import { + GraphQLObjectType, + GraphQLInt, + GraphQLString +} from 'graphql' + +const RuleType = new GraphQLObjectType({ + name: 'Rule', + fields: () => ({ + number: { type: GraphQLInt }, + title: { type: GraphQLString }, + description: { type: GraphQLString } + }) +}) + +export default RuleType diff --git a/server/src/schema/types/index.js b/server/src/schema/types/index.js new file mode 100644 index 000000000..be10a14df --- /dev/null +++ b/server/src/schema/types/index.js @@ -0,0 +1,16 @@ +import PlotterType from './Plotter' +import RuleType from './Rule' +import ParticipantType from './Participant' +import OtherPrizeType from './OtherPrize' +import PrizeType from './Prize' +import CompetitionType from './Competition' + +export { + PlotterType, + RuleType, + ParticipantType, + OtherPrizeType, + PrizeType, + CompetitionType +} +export default CompetitionType