From 03c2abc43ef682e039af7541bf25e929d55a6c57 Mon Sep 17 00:00:00 2001 From: Simon Wears Date: Sat, 25 Mar 2017 21:28:35 +0000 Subject: [PATCH] feat(graphql): Add delete and edit mutations for strength items Added edit and delete mutations for strength training activities --- src/constants/healthgraph.js | 3 +- .../strength-training/create-item.js | 2 +- .../strength-training/delete-item.js | 29 +++++ src/mutations/strength-training/edit-item.js | 117 ++++++++++++++++++ src/routes/graphql.js | 6 +- 5 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 src/mutations/strength-training/delete-item.js create mode 100644 src/mutations/strength-training/edit-item.js diff --git a/src/constants/healthgraph.js b/src/constants/healthgraph.js index 104cded..e1d1202 100644 --- a/src/constants/healthgraph.js +++ b/src/constants/healthgraph.js @@ -3,6 +3,7 @@ module.exports = { ROOT: '/user', CONTENT_TYPES: { - strength_training_activities: 'application/vnd.com.runkeeper.NewStrengthTrainingActivity+json' + new_strength_training_activities: 'application/vnd.com.runkeeper.NewStrengthTrainingActivity+json', + strength_training_activities: 'application/vnd.com.runkeeper.StrengthTrainingActivity+json' } }; \ No newline at end of file diff --git a/src/mutations/strength-training/create-item.js b/src/mutations/strength-training/create-item.js index 1148c8b..0c9aa5d 100644 --- a/src/mutations/strength-training/create-item.js +++ b/src/mutations/strength-training/create-item.js @@ -94,7 +94,7 @@ module.exports = { .load(HEALTHGRAPH.ROOT) .then(data => request .post(`${HEALTHGRAPH.BASE_URL}${data.strength_training_activities}`) - .type(HEALTHGRAPH.CONTENT_TYPES.strength_training_activities) + .type(HEALTHGRAPH.CONTENT_TYPES.new_strength_training_activities) .query({access_token: context.access_token}) .send(Object.assign(args, { start_time: new Date(args.start_time).toGMTString() diff --git a/src/mutations/strength-training/delete-item.js b/src/mutations/strength-training/delete-item.js new file mode 100644 index 0000000..c5564e9 --- /dev/null +++ b/src/mutations/strength-training/delete-item.js @@ -0,0 +1,29 @@ +const graphql = require('graphql'); +const fromGlobalId = require('graphql-relay').fromGlobalId; +const request = require('superagent-promise')(require('superagent'), Promise); +const HEALTHGRAPH = require('../../constants/healthgraph'); + +module.exports = { + + args: { + id: { type: new graphql.GraphQLNonNull(graphql.GraphQLID) } + }, + + resolve (rootVal, args, context) { + const {id} = fromGlobalId(args.id); + + return request + .del(`${HEALTHGRAPH.BASE_URL}${id}`) + .query({access_token: context.access_token}) + .end() + .then(() => ({ id: args.id })); + }, + + type: new graphql.GraphQLObjectType({ + name: 'DeleteStrengthItem', + fields: { + id: { type: graphql.GraphQLID } + } + }) + +}; \ No newline at end of file diff --git a/src/mutations/strength-training/edit-item.js b/src/mutations/strength-training/edit-item.js new file mode 100644 index 0000000..d91edee --- /dev/null +++ b/src/mutations/strength-training/edit-item.js @@ -0,0 +1,117 @@ +const HEALTHGRAPH = require('../../constants/healthgraph'); +const graphql = require('graphql'); +const GraphQLString = graphql.GraphQLString; +const GraphQLInputObjectType = graphql.GraphQLInputObjectType; +const GraphQLNonNull = graphql.GraphQLNonNull; +const GraphQLInt = graphql.GraphQLInt; +const GraphQLBoolean = graphql.GraphQLBoolean; +const GraphQLList = graphql.GraphQLList; +const GraphQLFloat = graphql.GraphQLFloat; +const GraphQLID = graphql.GraphQLID; +const i18n = require('../../helpers/i18n'); +const fromGlobalId = require('graphql-relay').fromGlobalId; +const request = require('superagent-promise')(require('superagent'), Promise); +const StrengthTrainingItem = require('../../data-types/strength-training/item'); +const MuscleTypes = require('../../data-types/strength-training/muscle-types'); +const ExerciseTypes = require('../../data-types/strength-training/exercise-types'); + + +module.exports = { + + args: { + exercises: { + type: new GraphQLList(new GraphQLInputObjectType({ + name: 'StrengthTrainingEditableExerciseMutation', + fields: { + notes: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.EXERCISE.NOTES'), + type: GraphQLString + }, + primary_muscle_group: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.EXERCISE.PRIMARY_MUSCLE_GROUP'), + type: MuscleTypes + }, + primary_type: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.EXERCISE.PRIMARY_TYPE'), + type: ExerciseTypes + }, + routine: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.EXERCISE.ROUTINE'), + type: GraphQLString + }, + secondary_muscle_group: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.EXERCISE.SECONDARY_MUSCLE_GROUP'), + type: MuscleTypes + }, + secondary_type: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.EXERCISE.SECONDARY_TYPE'), + type: GraphQLString + }, + sets: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.EXERCISE.SETS'), + type: new GraphQLList(new GraphQLInputObjectType({ + name: 'StrengthTrainingEditableSetMutation', + fields: { + notes: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.SET.NOTES'), + type: GraphQLString + }, + repetitions: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.SET.REPETITIONS'), + type: GraphQLInt + }, + weight: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.SET.WEIGHT'), + type: GraphQLFloat + } + } + })) + } + } + })) + }, + id: {type: new GraphQLNonNull(GraphQLID)}, + notes: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.NOTES'), + type: GraphQLString + }, + post_to_facebook: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.POST_TO_FACEBOOK'), + type: GraphQLBoolean + }, + post_to_twitter: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.POST_TO_TWITTER'), + type: GraphQLBoolean + }, + start_time: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.START_TIME'), + type: GraphQLString + }, + total_calories: { + description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.TOTAL_CALORIES'), + type: GraphQLFloat + } + }, + + resolve (rootVal, args, context) { + + const {id} = fromGlobalId(args.id); + + return request + .put(`${HEALTHGRAPH.BASE_URL}${id}`) + .type(HEALTHGRAPH.CONTENT_TYPES.strength_training_activities) + .query({access_token: context.access_token}) + .send(Object.assign(args, args.start_time ? { + start_time: new Date(args.start_time).toGMTString() + } : {})) + .end() + .then((res) => + // fetch the newly saved response + context.healthGraphLoader.load(id) + ) + + }, + + type: StrengthTrainingItem + +}; \ No newline at end of file diff --git a/src/routes/graphql.js b/src/routes/graphql.js index e40171f..2c103c7 100644 --- a/src/routes/graphql.js +++ b/src/routes/graphql.js @@ -4,6 +4,8 @@ const GraphQLSchema = graphql.GraphQLSchema; const UserType = require('../data-types/user'); const createLoader = require('../helpers/healthgraph-loader'); const createStrengthItem = require('../mutations/strength-training/create-item'); +const deleteStrengthItem = require('../mutations/strength-training/delete-item'); +const editStrengthItem = require('../mutations/strength-training/edit-item'); module.exports = function (app) { @@ -23,7 +25,9 @@ module.exports = function (app) { mutation: new graphql.GraphQLObjectType({ name: 'healthgraph', fields: { - create_strength_activity: createStrengthItem + create_strength_activity: createStrengthItem, + delete_strength_activity: deleteStrengthItem, + edit_strength_activity: editStrengthItem } }) })