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

Commit 44b9fe5

Browse files
author
Simon Wears
committed
feat(graphql): Add global IDs
Added global IDs for strength training items
1 parent 5f91364 commit 44b9fe5

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"express": "^4.15.2",
2727
"express-graphql": "^0.6.3",
2828
"graphql": "^0.9.1",
29+
"graphql-relay": "^0.5.1",
2930
"node-polyglot": "^2.2.2",
3031
"superagent": "^3.5.0",
3132
"superagent-promise": "^1.1.0",

src/constants/global-id-types.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
strength_training_activities: 'STRENGTH'
3+
};

src/data-types/node-interface.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const fromGlobalId = require('graphql-relay').fromGlobalId;
2+
const nodeDefinitions = require('graphql-relay').nodeDefinitions;
3+
4+
const { nodeInterface, nodeField } = nodeDefinitions(
5+
(globalId, context) => {
6+
const { type, id } = fromGlobalId(globalId);
7+
return context.healthGraphLoader.load(id);
8+
},
9+
object => {
10+
if (object.hasOwnProperty('exercises')) {
11+
return 'StrengthTrainingItem';
12+
}
13+
}
14+
);
15+
16+
module.exports = { nodeField, nodeInterface };

src/data-types/strength-training/item.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const GraphQLObjectType = graphql.GraphQLObjectType;
77
const StrengthTrainingExercise = require('./exercise');
88
const CommentType = require('../comment');
99
const i18n = require('../../helpers/i18n');
10+
const nodeInterface = require('../node-interface').nodeInterface;
11+
const toGlobalId = require('graphql-relay').toGlobalId;
12+
const globalIdTypes = require('../../constants/global-id-types');
1013

1114
module.exports = new GraphQLObjectType({
1215
name: 'StrengthTrainingItem',
@@ -27,6 +30,12 @@ module.exports = new GraphQLObjectType({
2730
},
2831
type: new GraphQLList(StrengthTrainingExercise)
2932
},
33+
id: {
34+
resolve (parent) {
35+
return toGlobalId(globalIdTypes.strength_training_activities, parent.uri);
36+
},
37+
type: new graphql.GraphQLNonNull(graphql.GraphQLID)
38+
},
3039
notes: {
3140
description: i18n.t('GRAPHQL.STRENGTH_TRAINING.ITEM.NOTES'),
3241
resolve (parent, args, context) {
@@ -59,5 +68,6 @@ module.exports = new GraphQLObjectType({
5968
},
6069
type: GraphQLInt
6170
}
62-
}
71+
},
72+
interfaces: [ nodeInterface ]
6373
});

src/data-types/user.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const HEALTHGRAPH = require('../constants/healthgraph');
66
const ProfileType = require('./profile');
77
const StrengthTrainingActivitiesType = require('./strength-training/activities');
88
const FitnessActivitiesType = require('./fitness-activities').FitnessActivities;
9+
const nodeField = require('./node-interface').nodeField;
910

1011
module.exports = new GraphQLObjectType({
1112
name: 'User',
@@ -19,6 +20,7 @@ module.exports = new GraphQLObjectType({
1920
},
2021
type: FitnessActivitiesType
2122
},
23+
node: nodeField,
2224
profile: {
2325
description: i18n.t('GRAPHQL.USER.PROFILE'),
2426
resolve (parent, args, context) {

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,10 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.1.9:
14311431
version "4.1.11"
14321432
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
14331433

1434+
graphql-relay@^0.5.1:
1435+
version "0.5.1"
1436+
resolved "https://registry.yarnpkg.com/graphql-relay/-/graphql-relay-0.5.1.tgz#3b3e44430a24c0f636e713f43f65bd542fe02ac9"
1437+
14341438
graphql@^0.9.1:
14351439
version "0.9.1"
14361440
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.9.1.tgz#f4d154cbec054d4a5d3b1be95f23435c09aa86c8"

0 commit comments

Comments
 (0)