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 global IDs
Browse files Browse the repository at this point in the history
Added global IDs for strength training items
  • Loading branch information
Simon Wears committed Mar 25, 2017
1 parent 5f91364 commit 44b9fe5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"express": "^4.15.2",
"express-graphql": "^0.6.3",
"graphql": "^0.9.1",
"graphql-relay": "^0.5.1",
"node-polyglot": "^2.2.2",
"superagent": "^3.5.0",
"superagent-promise": "^1.1.0",
Expand Down
3 changes: 3 additions & 0 deletions src/constants/global-id-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
strength_training_activities: 'STRENGTH'
};
16 changes: 16 additions & 0 deletions src/data-types/node-interface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const fromGlobalId = require('graphql-relay').fromGlobalId;
const nodeDefinitions = require('graphql-relay').nodeDefinitions;

const { nodeInterface, nodeField } = nodeDefinitions(
(globalId, context) => {
const { type, id } = fromGlobalId(globalId);
return context.healthGraphLoader.load(id);
},
object => {
if (object.hasOwnProperty('exercises')) {
return 'StrengthTrainingItem';
}
}
);

module.exports = { nodeField, nodeInterface };
12 changes: 11 additions & 1 deletion src/data-types/strength-training/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const GraphQLObjectType = graphql.GraphQLObjectType;
const StrengthTrainingExercise = require('./exercise');
const CommentType = require('../comment');
const i18n = require('../../helpers/i18n');
const nodeInterface = require('../node-interface').nodeInterface;
const toGlobalId = require('graphql-relay').toGlobalId;
const globalIdTypes = require('../../constants/global-id-types');

module.exports = new GraphQLObjectType({
name: 'StrengthTrainingItem',
Expand All @@ -27,6 +30,12 @@ module.exports = new GraphQLObjectType({
},
type: new GraphQLList(StrengthTrainingExercise)
},
id: {
resolve (parent) {
return toGlobalId(globalIdTypes.strength_training_activities, parent.uri);
},
type: new graphql.GraphQLNonNull(graphql.GraphQLID)
},
notes: {
description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.NOTES'),
resolve (parent, args, context) {
Expand Down Expand Up @@ -59,5 +68,6 @@ module.exports = new GraphQLObjectType({
},
type: GraphQLInt
}
}
},
interfaces: [ nodeInterface ]
});
2 changes: 2 additions & 0 deletions src/data-types/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const HEALTHGRAPH = require('../constants/healthgraph');
const ProfileType = require('./profile');
const StrengthTrainingActivitiesType = require('./strength-training/activities');
const FitnessActivitiesType = require('./fitness-activities').FitnessActivities;
const nodeField = require('./node-interface').nodeField;

module.exports = new GraphQLObjectType({
name: 'User',
Expand All @@ -19,6 +20,7 @@ module.exports = new GraphQLObjectType({
},
type: FitnessActivitiesType
},
node: nodeField,
profile: {
description: i18n.t('GRAPHQL.USER.PROFILE'),
resolve (parent, args, context) {
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,10 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.1.9:
version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"

graphql-relay@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/graphql-relay/-/graphql-relay-0.5.1.tgz#3b3e44430a24c0f636e713f43f65bd542fe02ac9"

graphql@^0.9.1:
version "0.9.1"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.9.1.tgz#f4d154cbec054d4a5d3b1be95f23435c09aa86c8"
Expand Down

0 comments on commit 44b9fe5

Please sign in to comment.