Skip to content

Commit

Permalink
Update to Comunica SPARQL Solid
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Oct 1, 2021
1 parent b9693cf commit a6dca0d
Show file tree
Hide file tree
Showing 8 changed files with 2,418 additions and 1,608 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ using the [Comunica](https://github.com/comunica/comunica) query engine
with [Solid](https://solid.mit.edu/) authentication.

This tool is identical to [graphql-ld-comunica](https://github.com/rubensworks/GraphQL-LD-Comunica-Solid.js),
with the additional feature that HTTP requests are authenticated using [solid-auth-client](https://github.com/solid/solid-auth-client/).
with the additional feature that HTTP requests are authenticated
using [Comunica SPARQL Solid](https://github.com/comunica/comunica-feature-solid/tree/master/packages/actor-init-sparql-solid).

## Usage

Expand All @@ -18,6 +19,7 @@ _This requires you to install [graphql-ld-comunica-solid](https://github.com/rub
```javascript
import {Client} from "graphql-ld";
import {QueryEngineComunicaSolid} from "graphql-ld-comunica-solid";
import {interactiveLogin} from "solid-node-interactive-auth";

// Define a JSON-LD context
const context = {
Expand All @@ -32,12 +34,15 @@ const context = {
}
};

// Create a GraphQL-LD client based on a client-side Comunica engine over 3 sources
// Create a GraphQL-LD client based on a client-side Comunica engine over 2 sources
const comunicaConfig = {
sources: [
"https://www.rubensworks.net/",
"http://fragments.dbpedia.org/2016-04/en",
],
// This will open your Web browser to log in.
// Other mechanisms to obtain your session may also be used instead.
session: await interactiveLogin({ oidcIssuer: 'https://solidcommunity.net/' }),
};
const client = new Client({ context, queryEngine: new QueryEngineComunicaSolid(comunicaConfig) });

Expand Down
11 changes: 0 additions & 11 deletions components/context.jsonld

This file was deleted.

25 changes: 0 additions & 25 deletions config/config-default.json

This file was deleted.

29 changes: 0 additions & 29 deletions config/sets/http-solid.json

This file was deleted.

11 changes: 8 additions & 3 deletions lib/QueryEngineComunicaSolid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ActorInitSparql} from "@comunica/actor-init-sparql/lib/ActorInitSparql-browser";
import {newEngine} from "@comunica/actor-init-sparql-solid";
import {IQueryEngine} from "graphql-ld";
import {Algebra} from "sparqlalgebrajs";
import * as stringifyStream from "stream-to-string";
Expand All @@ -12,13 +13,17 @@ export class QueryEngineComunicaSolid implements IQueryEngine {
private readonly context: any;

constructor(context: any) {
this.comunicaEngine = require('./comunica-engine');
this.comunicaEngine = newEngine();
this.context = context;
}

public async query(query: Algebra.Operation, options?: any): Promise<any> {
public async query(query: Algebra.Operation, options: any = {}): Promise<any> {
const context: any = { ...options, ...this.context };
if (context.session) {
context['@comunica/actor-http-inrupt-solid-client-authn:session'] = context.session;
}
const { data } = await this.comunicaEngine.resultToString(
await this.comunicaEngine.query(query, { ...options, ...this.context }),
await this.comunicaEngine.query(query, context),
'application/sparql-results+json',
);
return JSON.parse(await stringifyStream(data));
Expand Down
27 changes: 8 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
],
"main": "index.js",
"typings": "index",
"lsd:module": "https://linkedsoftwaredependencies.org/bundles/npm/graphql-ld-comunica-solid",
"lsd:contexts": {
"https://linkedsoftwaredependencies.org/bundles/npm/graphql-ld-comunica-solid/^2.0.0/components/context.jsonld": "components/context.jsonld"
},
"lsd:importPaths": {
"https://linkedsoftwaredependencies.org/bundles/npm/graphql-ld-comunica-solid/^2.0.0/components/": "components/",
"https://linkedsoftwaredependencies.org/bundles/npm/graphql-ld-comunica-solid/^2.0.0/config/": "config/"
},
"repository": "git@github.com:rubensworks/graphql-ld-comunica-solid.js.git",
"author": "Ruben Taelman <rubensworks@gmail.com>",
"bugs": {
Expand All @@ -43,19 +35,15 @@
"test"
],
"devDependencies": {
"@types/graphql": "^14.0.0",
"@types/jest": "^26.0.0",
"@types/minimist": "^1.2.0",
"@types/n3": "^1.1.4",
"coveralls": "^3.0.0",
"jest": "^26.0.0",
"manual-git-changelog": "^1.0.1",
"pre-commit": "^1.2.2",
"streamify-string": "^1.0.1",
"ts-jest": "^26.0.0",
"tslint": "^6.0.0",
"tslint-eslint-rules": "^5.3.1",
"typescript": "^3.3.1"
"typescript": "^4.4.3"
},
"jest": {
"globals": {
Expand All @@ -80,17 +68,18 @@
"scripts": {
"test": "jest ${1}",
"test-watch": "jest ${1} --watch",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls",
"lint": "tslint index.ts lib/**/*.ts test/**/*.ts --exclude '**/*.d.ts'",
"build": "tsc",
"validate": "npm ls",
"prepare": "npm run build && mkdirp lib && comunica-compile-config config/config-default.json > lib/comunica-engine.js",
"prepare": "npm run build",
"version": "manual-git-changelog onversion"
},
"dependencies": {
"@comunica/actor-init-sparql": "^1.10.0",
"@comunica/actor-http-solid-auth-fetch": "^1.0.0",
"graphql-ld": "^1.0.0",
"sparqlalgebrajs": "^2.2.0"
"@comunica/actor-init-sparql-solid": "^1.0.0",
"graphql-ld": "^1.4.0",
"sparqlalgebrajs": "^3.0.3"
},
"resolutions": {
"@types/yargs": "17.0.3"
}
}
48 changes: 37 additions & 11 deletions test/QueryEngineComunicaSolid-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const comunicaEngine = {
query: jest.fn((query, context) => Promise.resolve(`{ "a": "b" }`)),
resultToString: jest.fn((data) => Promise.resolve({ data: streamifyString(data) })),
};
jest.mock('../lib/comunica-engine', () => comunicaEngine);
jest.mock('@comunica/actor-init-sparql-solid', () => ({ newEngine: () => comunicaEngine }));

import {IQueryEngine} from "graphql-ld";
import {Algebra} from "sparqlalgebrajs";
Expand All @@ -17,18 +17,44 @@ describe('QueryEngineComunicaSolid', () => {
let queryEngine: IQueryEngine;
let sparqlAlgebra: Algebra.Operation;

beforeEach(async () => {
queryEngine = new QueryEngineComunicaSolid({ sources: { type: 'rdfjsSource', value: null } });
sparqlAlgebra = translate('SELECT * WHERE { ?x ?y ?z }');
describe('without a session', () => {
beforeEach(async () => {
queryEngine = new QueryEngineComunicaSolid({ sources: { type: 'rdfjsSource', value: null } });
sparqlAlgebra = translate('SELECT * WHERE { ?x ?y ?z }');
});

describe('query', () => {
it('should return a JSON object', async () => {
expect(await queryEngine.query(sparqlAlgebra)).toEqual({ a: 'b' });
expect(comunicaEngine.query)
.toHaveBeenCalledWith(sparqlAlgebra, { sources: { type: 'rdfjsSource', value: null } });
expect(comunicaEngine.resultToString)
.toHaveBeenCalledWith(`{ "a": "b" }`, 'application/sparql-results+json');
});
});
});

describe('query', () => {
it('should return a JSON object', async () => {
expect(await queryEngine.query(sparqlAlgebra)).toEqual({ a: 'b' });
expect(comunicaEngine.query)
.toHaveBeenCalledWith(sparqlAlgebra, { sources: { type: 'rdfjsSource', value: null } });
expect(comunicaEngine.resultToString)
.toHaveBeenCalledWith(`{ "a": "b" }`, 'application/sparql-results+json');
describe('with a session', () => {
beforeEach(async () => {
queryEngine = new QueryEngineComunicaSolid({
sources: { type: 'rdfjsSource', value: null },
session: 'SESSION',
});
sparqlAlgebra = translate('SELECT * WHERE { ?x ?y ?z }');
});

describe('query', () => {
it('should return a JSON object', async () => {
expect(await queryEngine.query(sparqlAlgebra)).toEqual({ a: 'b' });
expect(comunicaEngine.query)
.toHaveBeenCalledWith(sparqlAlgebra, {
sources: { type: 'rdfjsSource', value: null },
session: 'SESSION',
'@comunica/actor-http-inrupt-solid-client-authn:session': 'SESSION',
});
expect(comunicaEngine.resultToString)
.toHaveBeenCalledWith(`{ "a": "b" }`, 'application/sparql-results+json');
});
});
});
});
Loading

0 comments on commit a6dca0d

Please sign in to comment.