Skip to content

Commit 8b2a6e4

Browse files
committed
Added mappings for each resource across versions
1 parent d68cccc commit 8b2a6e4

27 files changed

+452
-98
lines changed

jsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es2016",
5+
"jsx": "preserve"
6+
},
7+
"exclude": ["node_modules", "**/node_modules/*"],
8+
"js-import.excludeFilesToScan": "packages/*/{es,lib,build}/**"
9+
}

packages/iiif-graphql/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
"test": "echo \"No tests yet\""
1313
},
1414
"dependencies": {
15-
"apollo-cache-inmemory": "^1.3.5",
16-
"apollo-client": "^2.4.2",
15+
"apollo-cache-inmemory": "^1.3.7",
16+
"apollo-client": "^2.4.4",
1717
"apollo-link": "^1.2.3",
1818
"apollo-link-schema": "^1.1.1",
1919
"apollo-link-set-context": "^0.5.4",
2020
"dataloader": "^1.4.0",
2121
"graphql": "^14.0.2",
2222
"graphql-tag": "^2.10.0",
23-
"graphql-tools": "^4.0.1",
23+
"graphql-tools": "^4.0.2",
2424
"iiif-redux": "1.0.0",
25-
"react": "^16.5.2",
25+
"react": "^16.6.0",
2626
"react-apollo": "^2.2.4",
27-
"react-dom": "^16.5.2"
27+
"react-dom": "^16.6.0"
2828
},
2929
"devDependencies": {
3030
"babel-plugin-graphql-tag": "^1.6.0",

packages/iiif-graphql/src/components/HelloWorld.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import gql from 'graphql-tag';
44

55
const queryText = gql`
66
query {
7-
getCollection(
8-
collectionId: "https://view.nls.uk/collections/7446/74466699.json"
9-
) {
7+
collection(id: "https://view.nls.uk/collections/7446/74466699.json") {
108
id
119
type
1210
label
@@ -15,9 +13,7 @@ const queryText = gql`
1513
value
1614
}
1715
}
18-
getManifest(
19-
manifestId: "https://wellcomelibrary.org/iiif/b20432033/manifest"
20-
) {
16+
manifest(id: "https://wellcomelibrary.org/iiif/b20432033/manifest") {
2117
id
2218
type
2319
label

packages/iiif-graphql/src/context.js

+65
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,77 @@
11
import { createStore } from 'iiif-redux';
2+
import { doesResourceExist } from 'iiif-redux/es/api/dereferenced';
3+
import { iiifResourceRequestUnknownAsync } from 'iiif-redux/es/spaces/iiif-resource';
4+
import { mappings as presentation2Mapping } from 'iiif-redux/es/api/2.x';
5+
import { mappings as presentation3Mapping } from 'iiif-redux/es/api/3.x';
6+
import DataLoader from 'dataloader';
27

38
const store = createStore();
49

10+
const iiifLoader = new DataLoader(keys =>
11+
Promise.all(
12+
keys.map(id => store.dispatch(iiifResourceRequestUnknownAsync(id)))
13+
)
14+
);
15+
16+
function mapping(version) {
17+
if (version === 3) {
18+
return presentation3Mapping;
19+
}
20+
return presentation2Mapping;
21+
}
22+
23+
function select(selector) {
24+
return selector(store.getState());
25+
}
26+
27+
async function loadResource(id, type) {
28+
return await iiifLoader.load(id);
29+
}
30+
31+
const selectById = (id, type) => {
32+
return state => state.resources[type][id];
33+
};
34+
35+
function resourceExists(id, type) {
36+
return select(doesResourceExist(selectById(id, type), type));
37+
}
38+
39+
function fieldExists(id, type, field) {
40+
const value = getFieldValue(id, type, field);
41+
42+
return typeof value !== 'undefined' && value !== null;
43+
}
44+
45+
function resourceDereferenced(id, type) {}
46+
47+
function getFieldValue(id, type, field) {
48+
const version = getPresentationVersionFromResource(
49+
select(selectById(id, type))
50+
);
51+
const api = mapping(version)(selectById(id, type));
52+
const singleField = api[field];
53+
return select(singleField);
54+
}
55+
56+
const combinedResolver = (id, type, field) => {
57+
if (!resourceExists(id, type)) {
58+
return loadResource(id, type).then(() => combinedResolver(id, type, field));
59+
}
60+
if (!fieldExists(id, type, field)) {
61+
if (resourceDereferenced(id, type)) {
62+
return null;
63+
}
64+
return loadResource(id, type).then(() => combinedResolver(id, type, field));
65+
}
66+
return getFieldValue(id, type, field);
67+
};
68+
569
const context = req => ({
670
store,
771
query: selector => {
872
return selector(store.getState());
973
},
74+
combinedResolver,
1075
});
1176

1277
export default context;

packages/iiif-graphql/src/resolvers/index.js

+2-23
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,6 @@ import { getSchemaVersionForResource } from 'iiif-redux/es/api/schema-version';
3232
// - All of the above for parent resource
3333
// - Return resources as simple objects with ID field (and schema/type). Maybe?
3434

35-
function loadResource(id, type) {}
36-
37-
function resourceExists(id, type) {}
38-
39-
function fieldExists(id, type, field) {}
40-
41-
function resourceDereferenced(id, type) {}
42-
43-
function getFieldValue(id, type, field) {}
44-
45-
const combinedResolver = (id, type, field) => {
46-
if (!resourceExists(id, type)) {
47-
return loadResource(id, type).then(() => combinedResolver(id, type, field));
48-
}
49-
if (!fieldExists(id, type, field)) {
50-
if (resourceDereferenced(id, type)) {
51-
return null;
52-
}
53-
return loadResource(id, type).then(() => combinedResolver(id, type, field));
54-
}
55-
return getFieldValue(id, type, field);
56-
};
57-
5835
const reduxResolver = (typeFunc, stateType) => apiFunc => (
5936
data,
6037
opt,
@@ -184,6 +161,8 @@ const resolvers = {
184161
Manifest: {
185162
type: () => 'Manifest',
186163
summary: manifestResolver('getSummary'),
164+
// label: (obj, _, context) =>
165+
// context.combinedResolver(obj.id, 'manifest', 'label'),
187166
},
188167
Collection: {
189168
type: () => 'Collection',

packages/iiif-redux/src/api/2.x/annotation-list.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import * as linking from './iiif/linking';
1414
import * as paging from './iiif/paging';
1515
import * as structural from './iiif/structural';
16+
import { standardFieldMappingFactory } from '../../utility/new/standardFieldMappingFactory';
1617

1718
const annotationList = memoize(selector => {
1819
/**************************************************
@@ -50,7 +51,14 @@ const annotationList = memoize(selector => {
5051

5152
const getAttribution = createSelector(selector, descriptive.getAttribution);
5253

53-
const getLogo = createSelector(selector, descriptive.getLogo);
54+
const getLogoIds = createSelector(selector, descriptive.getLogo);
55+
const getLogo = createSelector(
56+
getLogoIds,
57+
getAllImages,
58+
(logoIds, allImages) => {
59+
return logoIds.map(logoId => allImages[logoId] || logoId);
60+
}
61+
);
5462

5563
const getLicense = createSelector(selector, descriptive.getLicense);
5664

@@ -166,6 +174,7 @@ const annotationList = memoize(selector => {
166174
getDescription,
167175
getMetadata,
168176
getAttribution,
177+
getLogoIds,
169178
getLogo,
170179
getLicense,
171180
getThumbnail,
@@ -198,4 +207,6 @@ const annotationList = memoize(selector => {
198207
};
199208
});
200209

210+
export const mappings = standardFieldMappingFactory(annotationList);
211+
201212
export default annotationList;

packages/iiif-redux/src/api/2.x/annotation.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '../all';
1313
import * as linking from './iiif/linking';
1414
import { isImageService } from '../../constants/services';
15+
import { standardFieldMappingFactory } from '../../utility/new/standardFieldMappingFactory';
1516
const annotation = memoize(selector => {
1617
/**************************************************
1718
* Technical properties
@@ -51,7 +52,14 @@ const annotation = memoize(selector => {
5152

5253
const getAttribution = createSelector(selector, descriptive.getAttribution);
5354

54-
const getLogo = createSelector(selector, descriptive.getLogo);
55+
const getLogoIds = createSelector(selector, descriptive.getLogo);
56+
const getLogo = createSelector(
57+
getLogoIds,
58+
getAllImages,
59+
(logoIds, allImages) => {
60+
return logoIds.map(logoId => allImages[logoId] || logoId);
61+
}
62+
);
5563

5664
const getLicense = createSelector(selector, descriptive.getLicense);
5765

@@ -169,6 +177,7 @@ const annotation = memoize(selector => {
169177
getThumbnail,
170178
getAttribution,
171179
getLicense,
180+
getLogoIds,
172181
getLogo,
173182
// Linking
174183
getSeeAlso,
@@ -192,4 +201,6 @@ const annotation = memoize(selector => {
192201
};
193202
});
194203

204+
export const mappings = standardFieldMappingFactory(annotation);
205+
195206
export default annotation;

packages/iiif-redux/src/api/2.x/canvas.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getAllServices,
1414
} from '../all';
1515
import { isImageService } from '../../constants/services';
16+
import { standardFieldMappingFactory } from '../../utility/new/standardFieldMappingFactory';
1617

1718
const canvas = memoize(selector => {
1819
/**************************************************
@@ -56,7 +57,14 @@ const canvas = memoize(selector => {
5657

5758
const getAttribution = createSelector(selector, descriptive.getAttribution);
5859

59-
const getLogo = createSelector(selector, descriptive.getLogo);
60+
const getLogoIds = createSelector(selector, descriptive.getLogo);
61+
const getLogo = createSelector(
62+
getLogoIds,
63+
getAllImages,
64+
(logoIds, allImages) => {
65+
return logoIds.map(logoId => allImages[logoId] || logoId);
66+
}
67+
);
6068

6169
const getLicense = createSelector(selector, descriptive.getLicense);
6270

@@ -200,6 +208,7 @@ const canvas = memoize(selector => {
200208
getThumbnail,
201209
getAttribution,
202210
getLicense,
211+
getLogoIds,
203212
getLogo,
204213
// Linking
205214
getWithinIds,
@@ -222,4 +231,6 @@ const canvas = memoize(selector => {
222231
};
223232
});
224233

234+
export const mappings = standardFieldMappingFactory(canvas);
235+
225236
export default canvas;

packages/iiif-redux/src/api/2.x/collection.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from '../all';
1717
import mapById from '../../utility/mapById';
1818
import mapAllById from '../../utility/mapAllById';
19+
import { standardFieldMappingFactory } from '../../utility/new/standardFieldMappingFactory';
1920

2021
const collection = memoize(selector => {
2122
/**************************************************
@@ -53,7 +54,14 @@ const collection = memoize(selector => {
5354

5455
const getAttribution = createSelector(selector, descriptive.getAttribution);
5556

56-
const getLogo = createSelector(selector, descriptive.getLogo);
57+
const getLogoIds = createSelector(selector, descriptive.getLogo);
58+
const getLogo = createSelector(
59+
getLogoIds,
60+
getAllImages,
61+
(logoIds, allImages) => {
62+
return logoIds.map(logoId => allImages[logoId] || logoId);
63+
}
64+
);
5765

5866
const getLicense = createSelector(selector, descriptive.getLicense);
5967

@@ -200,6 +208,7 @@ const collection = memoize(selector => {
200208
getDescription,
201209
getMetadata,
202210
getAttribution,
211+
getLogoIds,
203212
getLogo,
204213
getLicense,
205214
getThumbnailId,
@@ -241,4 +250,6 @@ const collection = memoize(selector => {
241250
};
242251
});
243252

253+
export const mappings = standardFieldMappingFactory(collection);
254+
244255
export default collection;

packages/iiif-redux/src/api/2.x/external-resource.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getAllLayers,
1010
getAllServices,
1111
} from '../all';
12+
import { standardFieldMappingFactory } from '../../../es/utility/new/standardFieldMappingFactory';
1213

1314
const externalResource = memoize(selector => {
1415
/**************************************************
@@ -55,7 +56,14 @@ const externalResource = memoize(selector => {
5556

5657
const getAttribution = createSelector(selector, descriptive.getAttribution);
5758

58-
const getLogo = createSelector(selector, descriptive.getLogo);
59+
const getLogoIds = createSelector(selector, descriptive.getLogo);
60+
const getLogo = createSelector(
61+
getLogoIds,
62+
getAllImages,
63+
(logoIds, allImages) => {
64+
return logoIds.map(logoId => allImages[logoId] || logoId);
65+
}
66+
);
5967

6068
const getLicense = createSelector(selector, descriptive.getLicense);
6169

@@ -138,6 +146,7 @@ const externalResource = memoize(selector => {
138146
getDescription,
139147
getMetadata,
140148
getAttribution,
149+
getLogoIds,
141150
getLogo,
142151
getLicense,
143152
getThumbnail,
@@ -156,4 +165,6 @@ const externalResource = memoize(selector => {
156165
};
157166
});
158167

168+
export const mappings = standardFieldMappingFactory(externalResource);
169+
159170
export default externalResource;

packages/iiif-redux/src/api/2.x/iiif/descriptive.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ const getLicense = resource => {
2626

2727
const getLogo = resource => {
2828
if (!resource.logo) {
29-
return null;
30-
}
31-
if (Array.isArray(resource.logo)) {
32-
return resource.logo[0]['@id'] || resource.logo[0];
29+
return [];
3330
}
34-
return resource.logo['@id'] || resource.logo;
31+
return resource.logo;
3532
};
3633

3734
export {

0 commit comments

Comments
 (0)