From 8f690d5f5ba80395294fa972925bef3b3d07ac2a Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 16 Mar 2019 12:27:08 +0100 Subject: [PATCH] Setting queries --- src/schema/queries/Events.js | 37 ---------------------------- src/schema/queries/EventsByHost.js | 39 ------------------------------ src/schema/queries/StrategyByFB.js | 35 +++++++++++++++++++++++++++ src/schema/queries/index.js | 6 ++--- 4 files changed, 37 insertions(+), 80 deletions(-) delete mode 100644 src/schema/queries/Events.js delete mode 100644 src/schema/queries/EventsByHost.js create mode 100644 src/schema/queries/StrategyByFB.js diff --git a/src/schema/queries/Events.js b/src/schema/queries/Events.js deleted file mode 100644 index fae289a5b..000000000 --- a/src/schema/queries/Events.js +++ /dev/null @@ -1,37 +0,0 @@ -import { - GraphQLInt, - GraphQLList, -} from 'graphql'; -import { EventType } from '../types'; -import { Event } from '../../models'; -import { UNPUBLISHED, ARCHIVED } from '../../enums/EventState'; - -const args = { - minDate: { type: GraphQLInt }, - maxDate: { type: GraphQLInt }, -}; - -const resolve = (parent, { minDate, maxDate }) => Event.find( - Object.assign( - { state: { $nin: [UNPUBLISHED, ARCHIVED] } }, - minDate || maxDate - ? { - startDatetime: - Object.assign( - minDate ? { $gte: minDate } : {}, - maxDate ? { $lte: maxDate } : {}, - ), - } - : {}, - ), -); - -const query = { - events: { - type: new GraphQLList(EventType), - args, - resolve, - }, -}; - -export default query; diff --git a/src/schema/queries/EventsByHost.js b/src/schema/queries/EventsByHost.js deleted file mode 100644 index 6941570d4..000000000 --- a/src/schema/queries/EventsByHost.js +++ /dev/null @@ -1,39 +0,0 @@ -import { - GraphQLInt, - GraphQLList, - GraphQLString, -} from 'graphql'; -import { EventType } from '../types'; -import { Event } from '../../models'; -import { UNPUBLISHED, ARCHIVED } from '../../enums/EventState'; - -const args = { - hostId: { type: GraphQLString }, - minDate: { type: GraphQLInt }, - maxDate: { type: GraphQLInt }, -}; - -const resolve = (parent, { hostId, minDate, maxDate }, context) => Event.find( - Object.assign( - hostId ? { hostId, state: { $nin: [UNPUBLISHED, ARCHIVED] } } : { hostId: context.userId }, - minDate || maxDate - ? { - startDatetime: - Object.assign( - minDate ? { $gte: minDate } : {}, - maxDate ? { $lte: maxDate } : {}, - ), - } - : {}, - ), -); - -const query = { - eventsByHost: { - type: new GraphQLList(EventType), - args, - resolve, - }, -}; - -export default query; diff --git a/src/schema/queries/StrategyByFB.js b/src/schema/queries/StrategyByFB.js new file mode 100644 index 000000000..ba1d86f78 --- /dev/null +++ b/src/schema/queries/StrategyByFB.js @@ -0,0 +1,35 @@ +import { + GraphQLNonNull, + GraphQLID, +} from 'graphql'; +import { DatabaseError } from '../../errors'; +import { StrategyType } from '../types'; +import { Strategy } from '../../models'; + +const args = { + fbId: { type: new GraphQLNonNull(GraphQLID) }, +}; + +const resolve = (parent, { fbId }) => new Promise((res, rej) => { + Strategy.findOne({ fbId }).exec((err, event) => { + if (err) { + rej(err); + return; + } + if (!event) { + rej(new DatabaseError('None of the strategy are linked to that fbId')); + return; + } + res(event); + }); +}); + +const query = { + strategyByFb: { + type: StrategyType, + args, + resolve, + }, +}; + +export default query; diff --git a/src/schema/queries/index.js b/src/schema/queries/index.js index 5b3752be7..dcb6a77a5 100644 --- a/src/schema/queries/index.js +++ b/src/schema/queries/index.js @@ -2,14 +2,12 @@ import { GraphQLObjectType, } from 'graphql'; -import EventsQuery from './Events'; -import EventsByHostQuery from './EventsByHost'; +import StrategyByFbQuery from './StrategyByFB'; const RootQuery = new GraphQLObjectType({ name: 'RootQueryType', fields: Object.assign( - EventsQuery, - EventsByHostQuery, + StrategyByFbQuery, ), });