Skip to content

Commit

Permalink
[relay] Refactor and move type recognition to type files.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Jul 2, 2016
1 parent 700b09d commit d9c78ef
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 64 deletions.
2 changes: 2 additions & 0 deletions schema/article.js
Expand Up @@ -58,6 +58,8 @@ const Article = {
},
},
resolve: (root, { id }) => positron(`articles/${id}`),
// ObjectIdentification
isType: (obj) => obj.title !== undefined && obj.author !== undefined,
};

export default Article;
2 changes: 2 additions & 0 deletions schema/artist/index.js
Expand Up @@ -491,6 +491,8 @@ const Artist = {
},
},
resolve: (root, { id }) => gravity(`artist/${id}`),
// ObjectIdentification
isType: (obj) => obj.birthday !== undefined && obj.artworks_count !== undefined,
};

export default Artist;
2 changes: 2 additions & 0 deletions schema/artwork/index.js
Expand Up @@ -462,6 +462,8 @@ const Artwork = {
},
},
resolve: (root, { id }) => gravity(`artwork/${id}`),
// ObjectIdentification
isType: (obj) => obj.title !== undefined && obj.artists !== undefined,
};

export default Artwork;
41 changes: 16 additions & 25 deletions schema/object_identification.js
@@ -1,6 +1,4 @@
import gravity from '../lib/loaders/gravity';
import positron from '../lib/loaders/positron';

import _ from 'lodash';
import {
fromGlobalId,
toGlobalId,
Expand All @@ -11,6 +9,12 @@ import {
GraphQLInterfaceType,
} from 'graphql';

const SupportedTypes = {
types: ['Article', 'Artist', 'Artwork', 'PartnerShow'],
// To prevent circular dependencies, when this file is loaded, the modules are lazily loaded.
typeModule: _.memoize(type => require(`./${_.snakeCase(type)}`).default),
};

// Because we use a custom Node ID, we duplicate and slightly adjust the code from:
// https://github.com/graphql/graphql-relay-js/blob/master/src/node/node.js

Expand All @@ -24,16 +28,11 @@ const NodeInterface = new GraphQLInterfaceType({
},
}),
resolveType: (obj) => {
if (obj.birthday !== undefined && obj.artworks_count !== undefined) {
return require('./artist').default.type;
} else if (obj.title !== undefined && obj.artists !== undefined) {
return require('./artwork').default.type;
} else if (obj.title !== undefined && obj.author !== undefined) {
return require('./article').default.type;
} else if (obj.partner !== undefined && obj.display_on_partner_profile !== undefined) {
return require('./partner_show').default.type;
}
return null;
const mod = _.chain(SupportedTypes.types)
.map(type => SupportedTypes.typeModule(type))
.find(m => m.isType(obj))
.value();
return mod && mod.type;
},
});

Expand All @@ -47,19 +46,11 @@ const NodeField = {
description: 'The ID of the object',
},
},
resolve: (_, { __id }) => {
resolve: (root, { __id }) => {
const { type, id } = fromGlobalId(__id);
switch (type) {
case 'Artist':
return gravity(`artist/${id}`);
case 'Artwork':
return gravity(`artwork/${id}`);
case 'Article':
return positron(`articles/${id}`);
case 'PartnerShow':
return gravity(`show/${id}`);
default:
return null;
if (_.includes(SupportedTypes.types, type)) {
// Re-uses (slightly abuses) the existing GraphQL `resolve` function.
return SupportedTypes.typeModule(type).resolve(null, { id });
}
},
};
Expand Down
2 changes: 2 additions & 0 deletions schema/partner_show.js
Expand Up @@ -268,6 +268,8 @@ const PartnerShow = {
return show;
});
},
// ObjectIdentification
isType: (obj) => obj.partner !== undefined && obj.display_on_partner_profile !== undefined,
};

export default PartnerShow;
68 changes: 29 additions & 39 deletions test/schema/object_identification.js
Expand Up @@ -8,27 +8,23 @@ describe('Global Identification', () => {
const Artist = schema.__get__('Artist');
const Artwork = schema.__get__('Artwork');
const PartnerShow = schema.__get__('PartnerShow');
const ObjectIdentification = schema.__get__('ObjectIdentification');

afterEach(() => {
Article.__ResetDependency__('positron');
Artist.__ResetDependency__('gravity');
Artwork.__ResetDependency__('gravity');
PartnerShow.__ResetDependency__('gravity');
ObjectIdentification.__ResetDependency__('gravity');
});

describe('for an Article', () => {
beforeEach(() => {
[Article, ObjectIdentification].forEach((mod) => {
mod.__Rewire__('positron', sinon.stub().returns(
Promise.resolve({
id: 'foo-bar',
title: 'Nightlife at the Foo Bar',
author: 'Artsy Editorial',
})
));
});
Article.__Rewire__('positron', sinon.stub().returns(
Promise.resolve({
id: 'foo-bar',
title: 'Nightlife at the Foo Bar',
author: 'Artsy Editorial',
})
));
});

it('generates a Global ID', () => {
Expand Down Expand Up @@ -76,15 +72,13 @@ describe('Global Identification', () => {

describe('for an Artist', () => {
beforeEach(() => {
[Artist, ObjectIdentification].forEach((mod) => {
mod.__Rewire__('gravity', sinon.stub().returns(
Promise.resolve({
id: 'foo-bar',
birthday: null,
artworks_count: 42,
})
));
});
Artist.__Rewire__('gravity', sinon.stub().returns(
Promise.resolve({
id: 'foo-bar',
birthday: null,
artworks_count: 42,
})
));
});

it('generates a Global ID', () => {
Expand Down Expand Up @@ -132,15 +126,13 @@ describe('Global Identification', () => {

describe('for an Artwork', () => {
beforeEach(() => {
[Artwork, ObjectIdentification].forEach((mod) => {
mod.__Rewire__('gravity', sinon.stub().returns(
Promise.resolve({
id: 'foo-bar',
title: 'For baz',
artists: null,
})
));
});
Artwork.__Rewire__('gravity', sinon.stub().returns(
Promise.resolve({
id: 'foo-bar',
title: 'For baz',
artists: null,
})
));
});

it('generates a Global ID', () => {
Expand Down Expand Up @@ -188,16 +180,14 @@ describe('Global Identification', () => {

describe('for a PartnerShow', () => {
beforeEach(() => {
[PartnerShow, ObjectIdentification].forEach((mod) => {
mod.__Rewire__('gravity', sinon.stub().returns(
Promise.resolve({
id: 'foo-bar',
displayable: true,
partner: { id: 'for-baz' },
display_on_partner_profile: true,
})
));
});
PartnerShow.__Rewire__('gravity', sinon.stub().returns(
Promise.resolve({
id: 'foo-bar',
displayable: true,
partner: { id: 'for-baz' },
display_on_partner_profile: true,
})
));
});

it('generates a Global ID', () => {
Expand Down

0 comments on commit d9c78ef

Please sign in to comment.