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
Browse files Browse the repository at this point in the history
Added beginning of fitness activities schema. Added resolve item functionality to call 'next'
properties when needed to get correct count of items. Set default item count to 25.
  • Loading branch information
Simon Wears committed Mar 17, 2017
1 parent aea4923 commit 2679b4f
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 4 deletions.
3 changes: 2 additions & 1 deletion res/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"SERVER_START": "Server started on port %{port}"
"SERVER_START": "Server started on port %{port}",
"RUNKEEPER_REQUEST": "Request made to Runkeeper API %{endpoint}"
}
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const cookieParser = require('cookie-parser');
const express = require('express');
const log = require('./logging');
const log = require('./helpers/logging');
const path = require('path');


Expand Down
39 changes: 39 additions & 0 deletions src/data-types/fitness-activities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const graphql = require('graphql');
const GraphQLObjectType = graphql.GraphQLObjectType;
const GraphQLString = graphql.GraphQLString;
const GraphQLList = graphql.GraphQLList;
const GraphQLInt = graphql.GraphQLInt;
const GraphQLBoolean = graphql.GraphQLBoolean;

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

const FitnessItem = new GraphQLObjectType({
name: 'FitnessItem',
fields: {
type: {type: GraphQLString},
start_time: {type: GraphQLString},
utc_offset: {type: GraphQLInt},
total_distance: {type: GraphQLInt},
duration: {type: GraphQLInt},
total_calories: {type: GraphQLInt},
source: {type: GraphQLString},
entry_mode: {type: GraphQLString},
tracking_mode: {type: GraphQLString},
has_path: {type: GraphQLBoolean},
uri: {type: GraphQLString}
}
});

module.exports = new GraphQLObjectType({
name: 'FitnessActivities',
fields: {
size: {type: GraphQLInt},
items: {
type: new GraphQLList(FitnessItem),
args,
resolve
}
}
});
8 changes: 7 additions & 1 deletion src/data-types/strength-training-activities.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const GraphQLList = graphql.GraphQLList;
const GraphQLInt = graphql.GraphQLInt;
const fetch = require('../helpers/fetch');

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

const StrengthTrainingSet = new GraphQLObjectType({
name: 'StrengthTrainingSet',
fields: {
Expand Down Expand Up @@ -71,7 +75,9 @@ module.exports = new GraphQLObjectType({
fields: {
size: { type: GraphQLInt },
items: {
type: new GraphQLList(StrengthTrainingItem)
type: new GraphQLList(StrengthTrainingItem),
args,
resolve
}
}
});
7 changes: 7 additions & 0 deletions src/data-types/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const GraphQLString = graphql.GraphQLString;
const fetch = require('../helpers/fetch');
const ProfileType = require('./profile');
const StrengthTrainingActivitiesType = require('./strength-training-activities');
const FitnessActivitiesType = require('./fitness-activities');

module.exports = new GraphQLObjectType({
name: 'User',
Expand All @@ -23,6 +24,12 @@ module.exports = new GraphQLObjectType({
resolve (data, args, req) {
return fetch(data.strength_training_activities, req);
}
},
fitness_activities: {
type: FitnessActivitiesType,
resolve (data, args, req) {
return fetch(data.fitness_activities, req);
}
}
}
});
4 changes: 4 additions & 0 deletions src/helpers/fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const request = require('superagent-promise')(require('superagent'), Promise);
const log = require('./logging');

module.exports = function fetch (endpoint, req) {

log.info('RUNKEEPER_REQUEST', { endpoint });

return request
.get(`https://api.runkeeper.com${endpoint}`)
.query({access_token: req.cookies.auth})
Expand Down
2 changes: 1 addition & 1 deletion src/i18n.js → src/helpers/i18n.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Polyglot = require('node-polyglot');
const polyglot = new Polyglot();
const strings = require('../res/i18n/en.json');
const strings = require('../../res/i18n/en.json');

polyglot.extend(strings);

Expand Down
File renamed without changes.
49 changes: 49 additions & 0 deletions src/helpers/resolve-items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const fetch = require('./fetch');
const graphql = require('graphql');
const GraphQLInt = graphql.GraphQLInt;

module.exports = {

args: {
count: {type: GraphQLInt}
},

resolve (parent, args, req) {

return new Promise(resolve => {

const opts = Object.assign({
count: 25
}, args);

let items = parent.items;

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

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

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

}).then(resolve);

});
}

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

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

} else {
resolve(items.slice(0, opts.count));
}

});

}
};

0 comments on commit 2679b4f

Please sign in to comment.