Skip to content

Commit

Permalink
Added mappings for each resource across versions
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenwf committed Nov 2, 2018
1 parent d68cccc commit 8b2a6e4
Show file tree
Hide file tree
Showing 27 changed files with 452 additions and 98 deletions.
9 changes: 9 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2016",
"jsx": "preserve"
},
"exclude": ["node_modules", "**/node_modules/*"],
"js-import.excludeFilesToScan": "packages/*/{es,lib,build}/**"
}
10 changes: 5 additions & 5 deletions packages/iiif-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
"test": "echo \"No tests yet\""
},
"dependencies": {
"apollo-cache-inmemory": "^1.3.5",
"apollo-client": "^2.4.2",
"apollo-cache-inmemory": "^1.3.7",
"apollo-client": "^2.4.4",
"apollo-link": "^1.2.3",
"apollo-link-schema": "^1.1.1",
"apollo-link-set-context": "^0.5.4",
"dataloader": "^1.4.0",
"graphql": "^14.0.2",
"graphql-tag": "^2.10.0",
"graphql-tools": "^4.0.1",
"graphql-tools": "^4.0.2",
"iiif-redux": "1.0.0",
"react": "^16.5.2",
"react": "^16.6.0",
"react-apollo": "^2.2.4",
"react-dom": "^16.5.2"
"react-dom": "^16.6.0"
},
"devDependencies": {
"babel-plugin-graphql-tag": "^1.6.0",
Expand Down
8 changes: 2 additions & 6 deletions packages/iiif-graphql/src/components/HelloWorld.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import gql from 'graphql-tag';

const queryText = gql`
query {
getCollection(
collectionId: "https://view.nls.uk/collections/7446/74466699.json"
) {
collection(id: "https://view.nls.uk/collections/7446/74466699.json") {
id
type
label
Expand All @@ -15,9 +13,7 @@ const queryText = gql`
value
}
}
getManifest(
manifestId: "https://wellcomelibrary.org/iiif/b20432033/manifest"
) {
manifest(id: "https://wellcomelibrary.org/iiif/b20432033/manifest") {
id
type
label
Expand Down
65 changes: 65 additions & 0 deletions packages/iiif-graphql/src/context.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,77 @@
import { createStore } from 'iiif-redux';
import { doesResourceExist } from 'iiif-redux/es/api/dereferenced';
import { iiifResourceRequestUnknownAsync } from 'iiif-redux/es/spaces/iiif-resource';
import { mappings as presentation2Mapping } from 'iiif-redux/es/api/2.x';
import { mappings as presentation3Mapping } from 'iiif-redux/es/api/3.x';
import DataLoader from 'dataloader';

const store = createStore();

const iiifLoader = new DataLoader(keys =>
Promise.all(
keys.map(id => store.dispatch(iiifResourceRequestUnknownAsync(id)))
)
);

function mapping(version) {
if (version === 3) {
return presentation3Mapping;
}
return presentation2Mapping;
}

function select(selector) {
return selector(store.getState());
}

async function loadResource(id, type) {
return await iiifLoader.load(id);
}

const selectById = (id, type) => {
return state => state.resources[type][id];
};

function resourceExists(id, type) {
return select(doesResourceExist(selectById(id, type), type));
}

function fieldExists(id, type, field) {
const value = getFieldValue(id, type, field);

return typeof value !== 'undefined' && value !== null;
}

function resourceDereferenced(id, type) {}

function getFieldValue(id, type, field) {
const version = getPresentationVersionFromResource(
select(selectById(id, type))
);
const api = mapping(version)(selectById(id, type));
const singleField = api[field];
return select(singleField);
}

const combinedResolver = (id, type, field) => {
if (!resourceExists(id, type)) {
return loadResource(id, type).then(() => combinedResolver(id, type, field));
}
if (!fieldExists(id, type, field)) {
if (resourceDereferenced(id, type)) {
return null;
}
return loadResource(id, type).then(() => combinedResolver(id, type, field));
}
return getFieldValue(id, type, field);
};

const context = req => ({
store,
query: selector => {
return selector(store.getState());
},
combinedResolver,
});

export default context;
25 changes: 2 additions & 23 deletions packages/iiif-graphql/src/resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,6 @@ import { getSchemaVersionForResource } from 'iiif-redux/es/api/schema-version';
// - All of the above for parent resource
// - Return resources as simple objects with ID field (and schema/type). Maybe?

function loadResource(id, type) {}

function resourceExists(id, type) {}

function fieldExists(id, type, field) {}

function resourceDereferenced(id, type) {}

function getFieldValue(id, type, field) {}

const combinedResolver = (id, type, field) => {
if (!resourceExists(id, type)) {
return loadResource(id, type).then(() => combinedResolver(id, type, field));
}
if (!fieldExists(id, type, field)) {
if (resourceDereferenced(id, type)) {
return null;
}
return loadResource(id, type).then(() => combinedResolver(id, type, field));
}
return getFieldValue(id, type, field);
};

const reduxResolver = (typeFunc, stateType) => apiFunc => (
data,
opt,
Expand Down Expand Up @@ -184,6 +161,8 @@ const resolvers = {
Manifest: {
type: () => 'Manifest',
summary: manifestResolver('getSummary'),
// label: (obj, _, context) =>
// context.combinedResolver(obj.id, 'manifest', 'label'),
},
Collection: {
type: () => 'Collection',
Expand Down
13 changes: 12 additions & 1 deletion packages/iiif-redux/src/api/2.x/annotation-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import * as linking from './iiif/linking';
import * as paging from './iiif/paging';
import * as structural from './iiif/structural';
import { standardFieldMappingFactory } from '../../utility/new/standardFieldMappingFactory';

const annotationList = memoize(selector => {
/**************************************************
Expand Down Expand Up @@ -50,7 +51,14 @@ const annotationList = memoize(selector => {

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

const getLogo = createSelector(selector, descriptive.getLogo);
const getLogoIds = createSelector(selector, descriptive.getLogo);
const getLogo = createSelector(
getLogoIds,
getAllImages,
(logoIds, allImages) => {
return logoIds.map(logoId => allImages[logoId] || logoId);
}
);

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

Expand Down Expand Up @@ -166,6 +174,7 @@ const annotationList = memoize(selector => {
getDescription,
getMetadata,
getAttribution,
getLogoIds,
getLogo,
getLicense,
getThumbnail,
Expand Down Expand Up @@ -198,4 +207,6 @@ const annotationList = memoize(selector => {
};
});

export const mappings = standardFieldMappingFactory(annotationList);

export default annotationList;
13 changes: 12 additions & 1 deletion packages/iiif-redux/src/api/2.x/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../all';
import * as linking from './iiif/linking';
import { isImageService } from '../../constants/services';
import { standardFieldMappingFactory } from '../../utility/new/standardFieldMappingFactory';
const annotation = memoize(selector => {
/**************************************************
* Technical properties
Expand Down Expand Up @@ -51,7 +52,14 @@ const annotation = memoize(selector => {

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

const getLogo = createSelector(selector, descriptive.getLogo);
const getLogoIds = createSelector(selector, descriptive.getLogo);
const getLogo = createSelector(
getLogoIds,
getAllImages,
(logoIds, allImages) => {
return logoIds.map(logoId => allImages[logoId] || logoId);
}
);

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

Expand Down Expand Up @@ -169,6 +177,7 @@ const annotation = memoize(selector => {
getThumbnail,
getAttribution,
getLicense,
getLogoIds,
getLogo,
// Linking
getSeeAlso,
Expand All @@ -192,4 +201,6 @@ const annotation = memoize(selector => {
};
});

export const mappings = standardFieldMappingFactory(annotation);

export default annotation;
13 changes: 12 additions & 1 deletion packages/iiif-redux/src/api/2.x/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getAllServices,
} from '../all';
import { isImageService } from '../../constants/services';
import { standardFieldMappingFactory } from '../../utility/new/standardFieldMappingFactory';

const canvas = memoize(selector => {
/**************************************************
Expand Down Expand Up @@ -56,7 +57,14 @@ const canvas = memoize(selector => {

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

const getLogo = createSelector(selector, descriptive.getLogo);
const getLogoIds = createSelector(selector, descriptive.getLogo);
const getLogo = createSelector(
getLogoIds,
getAllImages,
(logoIds, allImages) => {
return logoIds.map(logoId => allImages[logoId] || logoId);
}
);

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

Expand Down Expand Up @@ -200,6 +208,7 @@ const canvas = memoize(selector => {
getThumbnail,
getAttribution,
getLicense,
getLogoIds,
getLogo,
// Linking
getWithinIds,
Expand All @@ -222,4 +231,6 @@ const canvas = memoize(selector => {
};
});

export const mappings = standardFieldMappingFactory(canvas);

export default canvas;
13 changes: 12 additions & 1 deletion packages/iiif-redux/src/api/2.x/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '../all';
import mapById from '../../utility/mapById';
import mapAllById from '../../utility/mapAllById';
import { standardFieldMappingFactory } from '../../utility/new/standardFieldMappingFactory';

const collection = memoize(selector => {
/**************************************************
Expand Down Expand Up @@ -53,7 +54,14 @@ const collection = memoize(selector => {

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

const getLogo = createSelector(selector, descriptive.getLogo);
const getLogoIds = createSelector(selector, descriptive.getLogo);
const getLogo = createSelector(
getLogoIds,
getAllImages,
(logoIds, allImages) => {
return logoIds.map(logoId => allImages[logoId] || logoId);
}
);

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

Expand Down Expand Up @@ -200,6 +208,7 @@ const collection = memoize(selector => {
getDescription,
getMetadata,
getAttribution,
getLogoIds,
getLogo,
getLicense,
getThumbnailId,
Expand Down Expand Up @@ -241,4 +250,6 @@ const collection = memoize(selector => {
};
});

export const mappings = standardFieldMappingFactory(collection);

export default collection;
13 changes: 12 additions & 1 deletion packages/iiif-redux/src/api/2.x/external-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getAllLayers,
getAllServices,
} from '../all';
import { standardFieldMappingFactory } from '../../../es/utility/new/standardFieldMappingFactory';

const externalResource = memoize(selector => {
/**************************************************
Expand Down Expand Up @@ -55,7 +56,14 @@ const externalResource = memoize(selector => {

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

const getLogo = createSelector(selector, descriptive.getLogo);
const getLogoIds = createSelector(selector, descriptive.getLogo);
const getLogo = createSelector(
getLogoIds,
getAllImages,
(logoIds, allImages) => {
return logoIds.map(logoId => allImages[logoId] || logoId);
}
);

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

Expand Down Expand Up @@ -138,6 +146,7 @@ const externalResource = memoize(selector => {
getDescription,
getMetadata,
getAttribution,
getLogoIds,
getLogo,
getLicense,
getThumbnail,
Expand All @@ -156,4 +165,6 @@ const externalResource = memoize(selector => {
};
});

export const mappings = standardFieldMappingFactory(externalResource);

export default externalResource;
7 changes: 2 additions & 5 deletions packages/iiif-redux/src/api/2.x/iiif/descriptive.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ const getLicense = resource => {

const getLogo = resource => {
if (!resource.logo) {
return null;
}
if (Array.isArray(resource.logo)) {
return resource.logo[0]['@id'] || resource.logo[0];
return [];
}
return resource.logo['@id'] || resource.logo;
return resource.logo;
};

export {
Expand Down
Loading

0 comments on commit 8b2a6e4

Please sign in to comment.