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 fitness activities. Add comments.
Browse files Browse the repository at this point in the history
Added fitness activity schema, and added comments to fitness and strength training activities. Added
before and after date fields to item query.
  • Loading branch information
Simon Wears committed Mar 19, 2017
1 parent 2679b4f commit 353141f
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 26 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
}
},
"root": true,
"globals": {
"Promise": true
},
"rules": {
"camelcase": 0,
"complexity": [
Expand Down
13 changes: 13 additions & 0 deletions src/data-types/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const graphql = require('graphql');
const GraphQLObjectType = graphql.GraphQLObjectType;
const GraphQLString = graphql.GraphQLString;

module.exports = new GraphQLObjectType({
name: 'Comment',
fields: {
timestamp: { type: GraphQLString },
userID: { type: GraphQLString },
name: { type: GraphQLString },
comment: { type: GraphQLString }
}
});
153 changes: 145 additions & 8 deletions src/data-types/fitness-activities.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,173 @@ const GraphQLString = graphql.GraphQLString;
const GraphQLList = graphql.GraphQLList;
const GraphQLInt = graphql.GraphQLInt;
const GraphQLBoolean = graphql.GraphQLBoolean;

const CommentType = require('./comment');
const fetch = require('../helpers/fetch');
const resolveItems = require('../helpers/resolve-items');
const resolve = resolveItems.resolve;
const args = resolveItems.args;

const FitnessDistance = new GraphQLObjectType({
name: 'Distance',
fields: {
timestamp: {type: GraphQLInt},
distance: {type: GraphQLInt}
}
});

const FitnessHeartRate = new GraphQLObjectType({
name: 'HeartRate',
fields: {
timestamp: {type: GraphQLInt},
heart_rate: {type: GraphQLInt}
}
});

const FitnessCalories = new GraphQLObjectType({
name: 'Calories',
fields: {
timestamp: {type: GraphQLInt},
calories: {type: GraphQLInt}
}
});

const FitnessPath = new GraphQLObjectType({
name: 'Path',
fields: {
timestamp: {type: GraphQLInt},
latitude: {type: GraphQLInt},
longitude: {type: GraphQLInt},
altitude: {type: GraphQLInt},
type: {type: GraphQLString}
}
});

const FitnessImage = new GraphQLObjectType({
name: 'Image',
fields: {
timestamp: {type: GraphQLInt},
latitude: {type: GraphQLInt},
longitude: {type: GraphQLInt},
uri: {type: GraphQLString},
thumbnail_uri: {type: GraphQLString}
}
});

const FitnessItem = new GraphQLObjectType({
name: 'FitnessItem',
fields: {
userID: {
type: GraphQLString,
resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.userID);
}
},
type: {type: GraphQLString},
secondary_type: {
type: GraphQLString,
resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.secondary_type);
}
},
equipment: {
type: GraphQLString,
resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.equipment);
}
},
start_time: {type: GraphQLString},
utc_offset: {type: GraphQLInt},
total_distance: {type: GraphQLInt},
distance: {
type: new GraphQLList(FitnessDistance),
resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.distance);
}
},
duration: {type: GraphQLInt},
average_heart_rate: {
type: GraphQLInt, resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.average_heart_rate);
}
},
heart_rate: {
type: new GraphQLList(FitnessHeartRate), resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.heart_rate);
}
},
total_calories: {type: GraphQLInt},
calories: {
type: new GraphQLList(FitnessCalories), resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.calories);
}
},

climb: {
type: GraphQLInt, resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.climb);
}
},
notes: {
type: GraphQLString, resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.notes);
}
},
is_live: {
type: GraphQLBoolean, resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.is_live);
}
},

path: {
type: new GraphQLList(FitnessPath), resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.path);
}
},
images: {
type: new GraphQLList(FitnessImage), resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.images);
}
},

share: {
type: GraphQLString, resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.share);
}
},
share_map: {
type: GraphQLString, resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.share_map);
}
},
source: {type: GraphQLString},
entry_mode: {type: GraphQLString},
tracking_mode: {type: GraphQLString},
has_path: {type: GraphQLBoolean},
uri: {type: GraphQLString}
activity: {
type: GraphQLString, resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.activity);
}
},
comments: {
type: new GraphQLList(CommentType),
resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => fetch(d.comments, req)).then(c => c.comments);
}
}
}
});

module.exports = new GraphQLObjectType({
const FitnessActivities = new GraphQLObjectType({
name: 'FitnessActivities',
fields: {
size: {type: GraphQLInt},
items: {
type: new GraphQLList(FitnessItem),
args,
resolve
args: resolveItems.args,
resolve: resolveItems.resolve
}
}
});
});

module.exports = {
FitnessItem,
FitnessActivities
};
27 changes: 19 additions & 8 deletions src/data-types/strength-training-activities.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ const GraphQLString = graphql.GraphQLString;
const GraphQLList = graphql.GraphQLList;
const GraphQLInt = graphql.GraphQLInt;
const fetch = require('../helpers/fetch');

const CommentType = require('./comment');
const resolveItems = require('../helpers/resolve-items');
const resolve = resolveItems.resolve;
const args = resolveItems.args;


const StrengthTrainingSet = new GraphQLObjectType({
name: 'StrengthTrainingSet',
Expand Down Expand Up @@ -35,7 +34,6 @@ const StrengthTrainingItem = new GraphQLObjectType({
name: 'StrengthTrainingItem',
fields: {
start_time: { type: GraphQLString },
uri: { type: GraphQLString },
userID: {
type: GraphQLString,
resolve (parent, args, req) {
Expand Down Expand Up @@ -65,19 +63,32 @@ const StrengthTrainingItem = new GraphQLObjectType({
resolve (parent, args, req) {
return fetch(parent.uri, req).then(d => d.exercises);
}
},
comments: {
type: new GraphQLList(CommentType),
resolve (parent, args, req) {
return fetch(parent.uri, req)
.then(d => fetch(d.comments, req))
.then(c => c.comments);
}
}
}
});


module.exports = new GraphQLObjectType({
const StrengthTrainingActivities = new GraphQLObjectType({
name: 'StrengthTrainingActivities',
fields: {
size: { type: GraphQLInt },
items: {
type: new GraphQLList(StrengthTrainingItem),
args,
resolve
args: resolveItems.args,
resolve: resolveItems.resolve
}
}
});
});

module.exports = {
StrengthTrainingItem,
StrengthTrainingActivities
};
4 changes: 2 additions & 2 deletions src/data-types/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const GraphQLString = graphql.GraphQLString;

const fetch = require('../helpers/fetch');
const ProfileType = require('./profile');
const StrengthTrainingActivitiesType = require('./strength-training-activities');
const FitnessActivitiesType = require('./fitness-activities');
const StrengthTrainingActivitiesType = require('./strength-training-activities').StrengthTrainingActivities;
const FitnessActivitiesType = require('./fitness-activities').FitnessActivities;

module.exports = new GraphQLObjectType({
name: 'User',
Expand Down
49 changes: 41 additions & 8 deletions src/helpers/resolve-items.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const fetch = require('./fetch');
const graphql = require('graphql');
const GraphQLInt = graphql.GraphQLInt;
const GraphQLString = graphql.GraphQLString;

module.exports = {

args: {
count: {type: GraphQLInt}
count: {type: GraphQLInt},
before: { type: GraphQLString },
after: { type: GraphQLString }
},

resolve (parent, args, req) {
Expand All @@ -16,24 +19,54 @@ module.exports = {
count: 25
}, args);

let items = parent.items;
function filterItems (item) {
let include = true;

if (args.before) {
include = (new Date(item.start_time) - new Date(args.before)) < 0;
}

if ((!args.before && args.after) || (args.before && args.after && include)) {
include = (new Date(item.start_time) - new Date(args.after)) > 0;
}

return include;
}

let items = parent.items.filter(filterItems);

function getItems (count, next) {
return new Promise(resolve => {
return new Promise(res => {

fetch(next, req).then(data => { // eslint-disable-line consistent-return
items = items.concat(data.items).filter(filterItems);

fetch(next, req).then(data => {
items = items.concat(data.items);
let stopGettingItems = false;

if (count > items.length && data.next) {
// Stop scrolling back in time if the last item is before the "after" arg
if (args.after) {
const item = data.items[data.items.length - 1];
stopGettingItems = (new Date(item.start_time) - new Date(args.after)) < 0;
}

if (!stopGettingItems && count > items.length && data.next) {
return getItems(count, data.next);
}

}).then(resolve);
}).then(res);

});
}

if (opts.count > items.length && parent.next) {
let stop = false;

// Stop scrolling back in time if the last item is before the "after" arg
if (args.after) {
const item = parent.items[parent.items.length - 1];
stop = (new Date(item.start_time) - new Date(args.after)) < 0;
}

if (!stop && opts.count > items.length && parent.next) {

getItems(opts.count, parent.next).then(() => {
resolve(items.slice(0, opts.count));
Expand Down

0 comments on commit 353141f

Please sign in to comment.