Skip to content

Commit

Permalink
fix: Adding skolemize to the SparqlHandler (#265)
Browse files Browse the repository at this point in the history
* Adding skolemize to the SparqlHandler

* Adding a test to check if the skolemized node is used inside the query

* Adding regression test for skolemization in mutations
  • Loading branch information
danielbeeke committed Sep 18, 2022
1 parent 502a7c5 commit b2e497e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/SparqlHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class SparqlHandler {
let subject, where;
// If the only condition is a subject, we need no WHERE clause
if (conditions.length === 1) {
subject = this.termToString(conditions[0].subject);
subject = this.termToString(skolemize(conditions[0].subject));
where = [];
}
// Otherwise, create a WHERE clause from all conditions
Expand All @@ -76,7 +76,7 @@ export default class SparqlHandler {
for (const { predicate, reverse, objects } of predicateObjects) {
// Mutate either only the specified objects, or all of them
const objectStrings = objects ?
objects.map(o => this.termToString(o)) :
objects.map(o => this.termToString(skolemize(o))) :
[this.createVar(predicate.value, scope)];
// Generate a triple pattern for all subjects
mutations.push(...this.triplePatterns(subject, predicate, objectStrings, reverse));
Expand Down
22 changes: 21 additions & 1 deletion test/integration/mutation-expressions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import PathProxy from '../../src/PathProxy';
import PathExpressionHandler from '../../src/PathExpressionHandler';
import InsertFunctionHandler from '../../src/InsertFunctionHandler';
import DeleteFunctionHandler from '../../src/DeleteFunctionHandler';
import SparqlHandler from '../../src/SparqlHandler';
import MutationExpressionsHandler from '../../src/MutationExpressionsHandler';
import JSONLDResolver from '../../src/JSONLDResolver';
import context from '../context';
import { namedNode, literal } from '@rdfjs/data-model';
import { namedNode, literal, blankNode } from '@rdfjs/data-model';

describe('a query path with a path expression handler', () => {
const handlers = {
Expand Down Expand Up @@ -224,4 +225,23 @@ describe('a query path with a path expression handler', () => {
},
]);
});

it('skolemizes blank nodes inside mutationExpressions', async () => {
const handler = new SparqlHandler();
const mutationExpressions = [
{
mutationType: 'INSERT',
conditions: [{ subject: blankNode('a') }],
predicateObjects: [{
predicate: namedNode('https://example.org/p'),
objects: [blankNode('b')],
}],
},
];

const query = await handler.handle({}, { mutationExpressions });

expect(query).toContain('urn:ldflex:sk0');
expect(query).toContain('urn:ldflex:sk1');
});
});

0 comments on commit b2e497e

Please sign in to comment.