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

Commit

Permalink
refactor(express-middleware, graphql): Move HealthGraphLoader to context
Browse files Browse the repository at this point in the history
Moved healthgraph data loader from an express middleware and extending inbound requests to being
passed in via context in graphqlHTTP
  • Loading branch information
Simon Wears committed Mar 20, 2017
1 parent a817695 commit 610f288
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 72 deletions.
3 changes: 0 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ const cookieParser = require('cookie-parser');
const express = require('express');
const log = require('./helpers/logging');
const path = require('path');
const HealthGraphLoader = require('./middleware/healthgraph-loader');


/* Create application */
const app = express();
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(HealthGraphLoader);

/* Server settings */
app.set('view engine', 'ejs');
Expand Down
64 changes: 32 additions & 32 deletions src/data-types/fitness-activities.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,43 +107,43 @@ const FitnessItem = new GraphQLObjectType({
fields: {
activity: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.ACTIVITY'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.activity);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.activity);
},
type: GraphQLString
},
average_heart_rate: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.AVERAGE_HEART_RATE'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.average_heart_rate);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.average_heart_rate);
},
type: GraphQLInt
},
calories: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.CALORIES'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.calories);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.calories);
},
type: new GraphQLList(FitnessCalories)
},
climb: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.CLIMB'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.climb);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.climb);
},
type: GraphQLInt
},
comments: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.COMMENTS'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => req.healthGraphLoader.load(d.comments)).then(c => c.comments);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => context.healthGraphLoader.load(d.comments)).then(c => c.comments);
},
type: new GraphQLList(CommentType)
},
distance: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.DISTANCE'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.distance);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.distance);
},
type: new GraphQLList(FitnessDistance)
},
Expand All @@ -158,8 +158,8 @@ const FitnessItem = new GraphQLObjectType({
equipment: {

description: i18n.t('GRAPHQL.FITNESS.ITEM.EQUIPMENT'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.equipment);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.equipment);
},
type: GraphQLString
},
Expand All @@ -169,58 +169,58 @@ const FitnessItem = new GraphQLObjectType({
},
heart_rate: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.HEART_RATE'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.heart_rate);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.heart_rate);
},
type: new GraphQLList(FitnessHeartRate)
},
images: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.IMAGES'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.images);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.images);
},
type: new GraphQLList(FitnessImage)
},
is_live: {

description: i18n.t('GRAPHQL.FITNESS.ITEM.IS_LIVE'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.is_live);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.is_live);
},
type: GraphQLBoolean
},
notes: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.NOTES'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.notes);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.notes);
},
type: GraphQLString
},
path: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.PATH'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.path);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.path);
},
type: new GraphQLList(FitnessPath)
},
secondary_type: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.SECONDARY_TYPE'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.secondary_type);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.secondary_type);
},
type: GraphQLString
},
share: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.SHARE'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.share);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.share);
},
type: GraphQLString
},
share_map: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.SHARE_MAP'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.share_map);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.share_map);
},
type: GraphQLString
},
Expand Down Expand Up @@ -250,8 +250,8 @@ const FitnessItem = new GraphQLObjectType({
},
userID: {
description: i18n.t('GRAPHQL.FITNESS.ITEM.USERID'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.userID);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.userID);
},
type: GraphQLString
},
Expand Down
26 changes: 13 additions & 13 deletions src/data-types/strength-training-activities.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,31 @@ const StrengthTrainingItem = new GraphQLObjectType({
fields: {
comments: {
description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.COMMENTS'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri)
.then(d => req.healthGraphLoader.load(d.comments))
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri)
.then(d => context.healthGraphLoader.load(d.comments))
.then(c => c.comments);
},
type: new GraphQLList(CommentType)
},
exercises: {
description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.EXERCISES'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.exercises);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.exercises);
},
type: new GraphQLList(StrengthTrainingExercise)
},
notes: {
description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.NOTES'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.notes);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.notes);
},
type: GraphQLString
},
source: {
description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.SOURCE'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.source);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.source);
},
type: GraphQLString
},
Expand All @@ -98,15 +98,15 @@ const StrengthTrainingItem = new GraphQLObjectType({
},
total_calories: {
description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.TOTAL_CALORIES'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.total_calories);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.total_calories);
},
type: GraphQLInt
},
userID: {
description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.USERID'),
resolve (parent, args, req) {
return req.healthGraphLoader.load(parent.uri).then(d => d.userID);
resolve (parent, args, context) {
return context.healthGraphLoader.load(parent.uri).then(d => d.userID);
},
type: GraphQLString
}
Expand Down
22 changes: 11 additions & 11 deletions src/data-types/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ module.exports = new GraphQLObjectType({
fields: {
fitness_activities: {
description: i18n.t('GRAPHQL.USER.FITNESS_ACTIVITIES'),
resolve (parent, args, req) {
return req.healthGraphLoader
resolve (parent, args, context) {
return context.healthGraphLoader
.load('/user')
.then(data => req.healthGraphLoader.load(data.fitness_activities));
.then(data => context.healthGraphLoader.load(data.fitness_activities));
},
type: FitnessActivitiesType
},
profile: {
description: i18n.t('GRAPHQL.USER.PROFILE'),
resolve (parent, args, req) {
return req.healthGraphLoader
resolve (parent, args, context) {
return context.healthGraphLoader
.load('/user')
.then(data => req.healthGraphLoader.load(data.profile));
.then(data => context.healthGraphLoader.load(data.profile));
},
type: ProfileType
},
strength_training_activities: {
description: i18n.t('GRAPHQL.USER.STRENGTH_TRAINING_ACTIVITIES'),
resolve (parent, args, req) {
return req.healthGraphLoader
resolve (parent, args, context) {
return context.healthGraphLoader
.load('/user')
.then(data => req.healthGraphLoader.load(data.strength_training_activities));
.then(data => context.healthGraphLoader.load(data.strength_training_activities));
},
type: StrengthTrainingActivitiesType
},
userID: {
description: i18n.t('GRAPHQL.USER.USERID'),
resolve (parent, args, req) {
return req.healthGraphLoader
resolve (parent, args, context) {
return context.healthGraphLoader
.load('/user')
.then(data => data.userID);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const request = require('superagent-promise')(require('superagent'), Promise);
const DataLoader = require('dataloader');
const log = require('./../helpers/logging');
const log = require('./logging');

module.exports = function createLoader (req, res, next) {
module.exports = function createLoader (req) {

const HealthGraphLoader = new DataLoader(
return new DataLoader(
urls => Promise.all(urls.map(uri => {

log.info('HEALTHGRAPH_REQUEST', {uri});
Expand All @@ -20,8 +20,4 @@ module.exports = function createLoader (req, res, next) {
}))
);

req.healthGraphLoader = HealthGraphLoader; // eslint-disable-line no-param-reassign

next();

};
21 changes: 15 additions & 6 deletions src/routes/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ const graphqlHTTP = require('express-graphql');
const graphql = require('graphql');
const GraphQLSchema = graphql.GraphQLSchema;
const UserType = require('../data-types/user');

const createLoader = require('../helpers/healthgraph-loader');
module.exports = function (app) {

app.use('/graphql', graphqlHTTP({
graphiql: true,
schema: new GraphQLSchema({
query: UserType
app.use('/graphql', graphqlHTTP(req => {

const healthGraphLoader = createLoader(req);

return {
context: {
healthGraphLoader
},
graphiql: true,
schema: new GraphQLSchema({
query: UserType
})
}
})
}));
);

};

0 comments on commit 610f288

Please sign in to comment.