Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

Commit

Permalink
feat(graphql): Add delete and edit mutations for strength items
Browse files Browse the repository at this point in the history
Added edit and delete mutations for strength training activities
  • Loading branch information
Simon Wears committed Mar 25, 2017
1 parent 44b9fe5 commit 03c2abc
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/constants/healthgraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
};
2 changes: 1 addition & 1 deletion src/mutations/strength-training/create-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
29 changes: 29 additions & 0 deletions src/mutations/strength-training/delete-item.js
Original file line number Diff line number Diff line change
@@ -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 }
}
})

};
117 changes: 117 additions & 0 deletions src/mutations/strength-training/edit-item.js
Original file line number Diff line number Diff line change
@@ -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

};
6 changes: 5 additions & 1 deletion src/routes/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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
}
})
})
Expand Down

0 comments on commit 03c2abc

Please sign in to comment.