Skip to content

Commit

Permalink
feat: Identity datasets by their URI
Browse files Browse the repository at this point in the history
BREAKING CHANGE: terminology sources are no longer identifier by their
distribution URI. Keep BC by still allowing the distribution URI to be
used when searching. However, the sources query now returns source
instead of distribution URIs.
  • Loading branch information
ddeboer committed Sep 17, 2024
1 parent dda4951 commit a44e1a7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export default {
],
coverageThreshold: {
global: {
lines: 91.37,
statements: 91.37,
branches: 95.08,
functions: 92.56,
lines: 91.39,
statements: 91.39,
branches: 95.45,
functions: 92.62,
},
},
transform: {
Expand Down
6 changes: 2 additions & 4 deletions packages/network-of-terms-graphql/src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ async function queryTerms(object: any, args: any, context: any): Promise<any> {
comunica: context.comunica,
});
const results = await service.queryAll({
sources: args.sources.map(
(distributionIri: string) => new IRI(distributionIri)
),
sources: args.sources.map((datasetIri: string) => new IRI(datasetIri)),
query: args.query,
queryMode: QueryMode[args.queryMode as keyof typeof QueryMode],
timeoutMs: args.timeoutMs,
Expand Down Expand Up @@ -141,7 +139,7 @@ function term(term: Term) {

async function source(distribution: Distribution, dataset: Dataset) {
return {
uri: distribution.iri,
uri: dataset.iri,
name: dataset.name,
alternateName: dataset.alternateName,
description: dataset.description,
Expand Down
54 changes: 51 additions & 3 deletions packages/network-of-terms-graphql/test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ describe('Server', () => {
`
);
expect(body.data.sources).toHaveLength(catalog.datasets.length);
expect(body.data.sources[0].uri).toEqual(
'https://data.netwerkdigitaalerfgoed.nl/rkd/rkdartists/sparql'
);
expect(body.data.sources[0].uri).toEqual('https://data.rkd.nl/rkdartists');
expect(body.data.sources[0].mainEntityOfPage).toEqual([
'https://example.com/rkdartists',
]);
Expand Down Expand Up @@ -92,6 +90,52 @@ describe('Server', () => {
});

it('responds to successful GraphQL terms query', async () => {
const body = await query(
termsQuery(['https://data.rkd.nl/rkdartists'], '.*')
);
expect(body.data.terms).toHaveLength(1); // Source.
expect(body.data.terms[0].source.name).toEqual('RKDartists');
expect(body.data.terms[0].source.uri).toEqual(
'https://data.rkd.nl/rkdartists'
);
expect(body.data.terms[0].source.inLanguage).toEqual(['nl']);
expect(body.data.terms[0].result.__typename).toEqual('Terms');
expect(body.data.terms[0].result.terms).toHaveLength(5); // Terms found.
expect(body.data.terms[0].responseTimeMs).toBeGreaterThan(0);

const artwork = body.data.terms[0].result.terms.find(
(term: {uri: string}) =>
term.uri === 'https://example.com/resources/artwork'
);
expect(artwork.seeAlso).toEqual(['https://example.com/html/artwork']);
expect(artwork.description).toEqual([
'One of the most famous Dutch paintings',
]);
expect(artwork.exactMatch).toEqual([
{
prefLabel: ['Exact match'],
uri: 'https://example.com/resources/match',
},
]);

const prefLabels = body.data.terms[0].result.terms.map(
({prefLabel}: {prefLabel: string[]}) => prefLabel[0] ?? ''
);
expect(prefLabels).toEqual([
'Rembrandt',
'Nachtwacht',
'All things art',
'',
'',
]); // Results with score must come first.

const relatedPrefLabels = artwork.related.map(
({prefLabel}: {prefLabel: string[]}) => prefLabel[0] ?? ''
);
expect(relatedPrefLabels).toEqual(['', 'All things art', 'Rembrandt']); // Sorted alphabetically.
});

it('responds to successful GraphQL terms query with backwards compatible distribution URI', async () => {
const body = await query(
termsQuery(
['https://data.netwerkdigitaalerfgoed.nl/rkd/rkdartists/sparql'],
Expand All @@ -100,6 +144,9 @@ describe('Server', () => {
);
expect(body.data.terms).toHaveLength(1); // Source.
expect(body.data.terms[0].source.name).toEqual('RKDartists');
expect(body.data.terms[0].source.uri).toEqual(
'https://data.rkd.nl/rkdartists'
);
expect(body.data.terms[0].source.inLanguage).toEqual(['nl']);
expect(body.data.terms[0].result.__typename).toEqual('Terms');
expect(body.data.terms[0].result.terms).toHaveLength(5); // Terms found.
Expand Down Expand Up @@ -151,6 +198,7 @@ describe('Server', () => {
const term = body.data.lookup[0];
expect(term.uri).toEqual('https://example.com/resources/art');
expect(term.source.name).toEqual('RKDartists');
expect(term.source.uri).toEqual('https://data.rkd.nl/rkdartists');
expect(term.result.__typename).toEqual('Term');
expect(term.result.uri).toEqual('https://example.com/resources/art');
expect(term.responseTimeMs).toBeGreaterThan(0);
Expand Down
6 changes: 6 additions & 0 deletions packages/network-of-terms-query/src/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export class Dataset {
readonly alternateName?: string
) {}

public getSparqlDistribution(): Distribution | undefined {
return this.distributions.find(
distribution => distribution instanceof SparqlDistribution
);
}

public getDistributionByIri(iri: IRI): Distribution | undefined {
return this.distributions.find(
distribution => distribution.iri.toString() === iri.toString()
Expand Down
6 changes: 4 additions & 2 deletions packages/network-of-terms-query/src/distributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ export class DistributionsService {
async query(options: QueryOptions): Promise<TermsResponse> {
const args = Joi.attempt(options, schemaQuery);
this.logger.info(`Preparing to query source "${args.source}"...`);
const dataset = await this.catalog.getDatasetByDistributionIri(args.source);
const dataset =
this.catalog.getDatasetByIri(args.source) ??
this.catalog.getDatasetByDistributionIri(args.source);
if (dataset === undefined) {
throw Error(`Source with URI "${args.source}" not found`);
}
const distribution = dataset.getDistributionByIri(args.source)!;
const distribution = dataset.getSparqlDistribution()!;
const queryService = new QueryTermsService({
logger: this.logger,
comunica: this.comunica,
Expand Down

0 comments on commit a44e1a7

Please sign in to comment.