Skip to content

Commit

Permalink
[fix] optionals in federated query with service
Browse files Browse the repository at this point in the history
  • Loading branch information
amivanoff committed Oct 6, 2021
1 parent 00660e8 commit 6dc7fcc
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agentlab/sparql-jsld-client",
"version": "5.0.0-rc.15",
"version": "5.0.0-rc.16",
"description": "SPARQL JSON Schema Linked Data Client",
"license": "GPL-3.0",
"author": "Alexey Ivanov <[email protected]>",
Expand Down
10 changes: 7 additions & 3 deletions src/SparqlClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,19 @@ export async function sendPostStatements(
* @param {*} url
* @param {*} query
*/
export async function executeUpdate(url: string, query: string, queryParams: JsObject = {}): Promise<AxiosResponse> {
export async function executeUpdate(
url: string,
query: string,
queryParams: JsObject = {},
): Promise<AxiosResponse<any>> {
let queryParamsInUrl = '';
if (Object.keys(queryParams).length > 0) {
queryParamsInUrl = `?${Object.keys(queryParams)
.map((param) => `${param}=${encodeURIComponent(queryParams[param])}`)
.join('&')}`;
}
url = url + queryParamsInUrl;
const response = await axios.request<string, AxiosResponse>({
const response = await axios.request<string, AxiosResponse<any>>({
method: 'post',
url,
headers: {
Expand All @@ -233,7 +237,7 @@ export interface SparqlClient {

sparqlSelect(query: string, queryParams?: JsObject): Promise<Results>;
sparqlConstruct(query: string, queryParams?: JsObject): Promise<JsObject[]>;
sparqlUpdate(query: string, queryParams?: JsObject): Promise<AxiosResponse>;
sparqlUpdate(query: string, queryParams?: JsObject): Promise<AxiosResponse<any>>;

clearGraph(graph?: string): Promise<any>;

Expand Down
2 changes: 1 addition & 1 deletion src/SparqlClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class SparqlClientImpl implements SparqlClient {
}
}

async sparqlUpdate(query: string, queryParams: JsObject = {}): Promise<AxiosResponse> {
async sparqlUpdate(query: string, queryParams: JsObject = {}): Promise<AxiosResponse<any>> {
//console.debug(() => `sparqlUpdate url=${this.repositoryUrl} queryParams=${json2str(queryParams)}`);
return executeUpdate(this.statementsUrl, query, queryParams);
}
Expand Down
5 changes: 3 additions & 2 deletions src/SparqlGenSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,16 +653,17 @@ function constructQueryFromEntConstrs(entConstrs: EntConstrInternal[], collConst
where = [
{
type: 'service',
patterns: where,
patterns: [...where, ...entConstr.query.options],
name: {
termType: 'NamedNode',
value: entConstrJs.service,
},
silent: false,
},
];
} else {
allOptions = [...allOptions, ...entConstr.query.options];
}
allOptions = [...allOptions, ...entConstr.query.options];
}
query.where = [...(query.where || []), ...where];
}
Expand Down
22 changes: 11 additions & 11 deletions test/SparqlClientImplMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AxiosResponse } from 'axios';
import { SparqlClient, Results } from '../src/SparqlClient';
import { JsObject, JsStrObj } from '../src/ObjectProvider';

const HttpResponse200: AxiosResponse = {
const HttpResponse200: AxiosResponse<any> = {
data: {},
status: 200,
statusText: 'Ok',
Expand Down Expand Up @@ -60,33 +60,33 @@ export class SparqlClientImplMock implements SparqlClient {
createRepositoryAndSetCurrentParams = {};
createRepositoryParams = {};

setServerUrl(url: string) {
setServerUrl(url: string): void {
this.serverUrl = url;
this.regenerateUrls();
}

setRepositoryId(repId: string) {
setRepositoryId(repId: string): void {
this.repId = repId;
this.regenerateUrls();
}

createRepositoryUrl(repId: string) {
createRepositoryUrl(repId: string): string {
return `${this.serverUrl}/repositories/${repId}`;
}
createStatementsUrl(repId: string) {
createStatementsUrl(repId: string): string {
return `${this.serverUrl}/repositories/${repId}/statements`;
}

regenerateUrls() {
regenerateUrls(): void {
this.repositoryUrl = this.createRepositoryUrl(this.repId);
this.statementsUrl = this.createStatementsUrl(this.repId);
}

async loadNs() {
async loadNs(): Promise<JsStrObj> {
return this.nsReturn;
}

async uploadStatements(statements: string, baseURI?: string, graph?: string) {
async uploadStatements(statements: string, baseURI?: string, graph?: string): Promise<void> {
this.uploadStatementsParams = {
statements: statements.replace(/^#.*$/gm, ''),
baseURI,
Expand All @@ -99,23 +99,23 @@ export class SparqlClientImplMock implements SparqlClient {
//return Promise.reject('');
//}

async sparqlSelect(query: string, queryParams: JsObject = {}) {
async sparqlSelect(query: string, queryParams: JsObject = {}): Promise<Results> {
this.sparqlSelectParams = {
query,
queryParams,
};
return this.sparqlSelectReturn;
}

async sparqlConstruct(query: string, queryParams: JsObject = {}) {
async sparqlConstruct(query: string, queryParams: JsObject = {}): Promise<JsObject[]> {
this.sparqlConstructParams = {
query,
queryParams,
};
return this.sparqlConstructReturn;
}

async sparqlUpdate(query: string, queryParams: JsObject = {}) {
async sparqlUpdate(query: string, queryParams: JsObject = {}): Promise<AxiosResponse<any>> {
this.sparqlUpdateParams = {
query,
queryParams,
Expand Down
20 changes: 12 additions & 8 deletions test/SparqlGen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,9 @@ describe('constructObjectsQuery', () => {
{
schema: HSObservationShapeSchema,
conditions: {
product: 'https://www.wildberries.ru/catalog/10322023/detail.aspx',
product: 'https://www.wildberries.ru/catalog/10477067/detail.aspx',
parsedAt: {
relation: 'after',
relation: 'before',
value: ['2021-07-01T00:00:00'],
},
},
Expand All @@ -739,22 +739,26 @@ describe('constructObjectsQuery', () => {
expect(coll).not.toBeUndefined();
//expect(coll.length).toBe(0);
const genQueryStr = client.sparqlConstructParams.query;
console.log(genQueryStr);
//console.log(genQueryStr);
const correctQuery = `PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX hs: <https://huntersales.ru/schema#>
CONSTRUCT {
?eIri0 rdf:type hs:HSObservation ;
hs:product <https://www.wildberries.ru/catalog/10322023/detail.aspx> ;
hs:product <https://www.wildberries.ru/catalog/10477067/detail.aspx> ;
hs:parsedAt ?parsedAt0 ;
hs:price ?price0 .
hs:price ?price0 ;
hs:totalSales ?totalSales0 ;
hs:categoryPopularity ?categoryPopularity0 .
} WHERE {
SERVICE <http://192.168.1.33:8090/sparql> {
?eIri0 rdf:type hs:HSObservation ;
hs:product <https://www.wildberries.ru/catalog/10322023/detail.aspx> ;
hs:product <https://www.wildberries.ru/catalog/10477067/detail.aspx> ;
hs:parsedAt ?parsedAt0 ;
hs:price ?price0 .
filter(?parsedAt0 >= "2021-07-01T00:00:00"^^xsd:dateTime)
hs:totalSales ?totalSales0 .
FILTER(?parsedAt0 <= "2021-07-01T00:00:00"^^xsd:dateTime)
OPTIONAL{ ?eIri0 hs:price ?price0 . }
OPTIONAL{ ?eIri0 hs:categoryPopularity ?categoryPopularity0 . }
}
}
ORDER BY (?parsedAt0)`;
Expand Down
19 changes: 18 additions & 1 deletion test/schema/TestSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,14 @@ export const HSObservationShapeSchema: JSONSchema6forRdf = {
'@id': 'hs:price',
'@type': 'xsd:int',
},
totalSales: {
'@id': 'hs:totalSales',
'@type': 'xsd:int',
},
categoryPopularity: {
'@id': 'hs:categoryPopularity',
'@type': 'xsd:double',
},
},
properties: {
'@id': {
Expand All @@ -686,11 +694,20 @@ export const HSObservationShapeSchema: JSONSchema6forRdf = {
type: 'string',
format: 'date-time',
},
// optional int
price: {
type: 'integer',
},
// mandatory int
totalSales: {
type: 'integer',
},
// optional double
categoryPopularity: {
type: 'number',
},
},
required: ['@id', '@type', 'product', 'parsedAt', 'price'],
required: ['@id', '@type', 'product', 'parsedAt', 'totalSales'],
};

export const ProductCardShapeSchemaForCardsList: JSONSchema6forRdf = {
Expand Down

0 comments on commit 6dc7fcc

Please sign in to comment.