Skip to content

Commit 567ae66

Browse files
authored
Merge pull request #32 from jacoscaz/feature/query-separate-interfaces
Modularizes Queryable and SparqlQueryable interfaces
2 parents 762a892 + d47c8b1 commit 567ae66

File tree

2 files changed

+114
-66
lines changed

2 files changed

+114
-66
lines changed

query/queryable.d.ts

Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ import * as RDF from '../data-model';
55
import { Bindings, Query, ResultStream } from './common';
66

77
/**
8-
* Context objects provide a way to pass additional bits information to the query engine when executing a query.
8+
* Context properties provide a way to pass additional bits information to the query engine when executing a query.
99
*/
10-
export interface QueryContext<SourceType> {
11-
/**
12-
* An array of data sources the query engine must use.
13-
*/
14-
sources?: [SourceType, ...SourceType[]];
10+
export interface QueryContext {
1511
/**
1612
* The date that should be used by SPARQL operations such as NOW().
1713
*/
@@ -23,9 +19,9 @@ export interface QueryContext<SourceType> {
2319
}
2420

2521
/**
26-
* Context object in the case the passed query is a string.
22+
* Context properties in the case the passed query is a string.
2723
*/
28-
export interface QueryStringContext<SourceType> extends QueryContext<SourceType> {
24+
export interface QueryStringContext extends QueryContext {
2925
/**
3026
* The format in which the query string is defined.
3127
* Defaults to { language: 'sparql', version: '1.1' }
@@ -38,9 +34,19 @@ export interface QueryStringContext<SourceType> extends QueryContext<SourceType>
3834
}
3935

4036
/**
41-
* Context object in the case the passed query is an algebra object.
37+
* Context properties in the case the passed query is an algebra object.
4238
*/
43-
export type QueryAlgebraContext<SourceType> = QueryContext<SourceType>;
39+
export type QueryAlgebraContext = QueryContext;
40+
41+
/**
42+
* Context properties for engines that can query upon dynamic sets of sources.
43+
*/
44+
export interface QuerySourceContext<SourceType> {
45+
/**
46+
* An array of data sources the query engine must use.
47+
*/
48+
sources: [SourceType, ...SourceType[]];
49+
}
4450

4551
/**
4652
* Represents a specific query format
@@ -61,83 +67,91 @@ export interface QueryFormat {
6167
extensions?: string[];
6268
}
6369

64-
/**
65-
* Placeholder to represent SPARQL Algebra trees.
66-
* Algebra typings are TBD. Reference implementations include:
67-
* - https://www.npmjs.com/package/sparqlalgebrajs
68-
*/
69-
export type Algebra = any;
70-
7170
/**
7271
* Generic query engine interfaces.
73-
* It allow engines to return any type of result object for any type of query.
74-
* @param QueryFormatTypesAvailable The format of the query, either string or algebra object.
75-
* @param SourceType The allowed sources over which queries can be executed.
72+
* It allow engines to return any type of result object for string queries.
7673
* @param SupportedMetadataType The allowed metadata types.
77-
* @param QueryType The allowed query types.
7874
* @param QueryStringContextType Type of the string-based query context.
79-
* @param QueryAlgebraContextType Type of the algebra-based query context.
8075
*/
81-
export interface Queryable<
82-
QueryFormatTypesAvailable extends string | Algebra,
83-
SourceType,
76+
export interface StringQueryable<
8477
SupportedMetadataType,
85-
QueryType extends Query<SupportedMetadataType>,
86-
QueryStringContextType extends QueryStringContext<SourceType>,
87-
QueryAlgebraContextType extends QueryAlgebraContext<SourceType>,
78+
QueryStringContextType extends QueryStringContext = QueryStringContext,
8879
> {
8980
/**
90-
* Initiate a given query.
81+
* Initiate a given query provided as a string.
9182
*
9283
* This will produce a future to a query result, which has to be executed to obtain the query results.
9384
*
9485
* This can reject given an unsupported or invalid query.
9586
*
9687
* @see Query
9788
*/
98-
query<QueryFormatType extends QueryFormatTypesAvailable>(
99-
query: QueryFormatType,
100-
context?: QueryFormatType extends string ? QueryStringContextType : QueryAlgebraContextType,
101-
): Promise<QueryType>;
89+
query(query: string, context?: QueryStringContextType): Promise<Query<SupportedMetadataType>>;
10290
}
10391

10492
/**
105-
* SPARQL-constrained query interface.
93+
* Generic query engine interfaces.
94+
* It allow engines to return any type of result object for Algebra queries.
95+
* @param AlgebraType The supported algebra types.
96+
* @param SupportedMetadataType The allowed metadata types.
97+
* @param QueryStringContextType Type of the algebra-based query context.
98+
*/
99+
export interface AlgebraQueryable<
100+
AlgebraType,
101+
SupportedMetadataType,
102+
QueryAlgebraContextType extends QueryAlgebraContext = QueryAlgebraContext,
103+
> {
104+
/**
105+
* Initiate a given query provided as an Algebra object.
106+
*
107+
* This will produce a future to a query result, which has to be executed to obtain the query results.
108+
*
109+
* This can reject given an unsupported or invalid query.
110+
*
111+
* @see Query
112+
*/
113+
query(query: AlgebraType, context?: QueryAlgebraContextType): Promise<Query<SupportedMetadataType>>;
114+
}
115+
116+
/**
117+
* SPARQL-constrained query interface for queries provided as strings.
106118
*
107119
* This interface guarantees that result objects are of the expected type as defined by the SPARQL spec.
108120
*/
109-
export type SparqlQueryable<
110-
QueryFormatTypesAvailable extends string | Algebra,
111-
SourceType,
112-
QueryStringContextType extends QueryStringContext<SourceType>,
113-
QueryAlgebraContextType extends QueryAlgebraContext<SourceType>,
114-
SupportedResultType,
115-
> = unknown
121+
export type StringSparqlQueryable<SupportedResultType, QueryStringContextType extends QueryStringContext = QueryStringContext> = unknown
116122
& (SupportedResultType extends BindingsResultSupport ? {
117-
queryBindings<QueryFormatType extends QueryFormatTypesAvailable>(
118-
query: QueryFormatType,
119-
context?: QueryFormatType extends string ? QueryStringContextType : QueryAlgebraContextType,
120-
): Promise<ResultStream<Bindings>>;
123+
queryBindings(query: string, context?: QueryStringContextType): Promise<ResultStream<Bindings>>;
121124
} : unknown)
122125
& (SupportedResultType extends BooleanResultSupport ? {
123-
queryBoolean<QueryFormatType extends QueryFormatTypesAvailable>(
124-
query: QueryFormatType,
125-
context?: QueryFormatType extends string ? QueryStringContextType : QueryAlgebraContextType,
126-
): Promise<boolean>;
126+
queryBoolean(query: string, context?: QueryStringContextType): Promise<boolean>;
127127
} : unknown)
128128
& (SupportedResultType extends QuadsResultSupport ? {
129-
queryQuads<QueryFormatType extends QueryFormatTypesAvailable>(
130-
query: QueryFormatType,
131-
context?: QueryFormatType extends string ? QueryStringContextType : QueryAlgebraContextType,
132-
): Promise<ResultStream<RDF.Quad>>;
129+
queryQuads(query: string, context?: QueryStringContextType): Promise<ResultStream<RDF.Quad>>;
133130
} : unknown)
134131
& (SupportedResultType extends VoidResultSupport ? {
135-
queryVoid<QueryFormatType extends QueryFormatTypesAvailable>(
136-
query: QueryFormatType,
137-
context?: QueryFormatType extends string ? QueryStringContextType : QueryAlgebraContextType,
138-
): Promise<void>;
132+
queryVoid(query: string, context?: QueryStringContextType): Promise<void>;
133+
} : unknown)
134+
;
135+
136+
/**
137+
* SPARQL-constrainted query interface for queries provided as Algebra objects.
138+
*
139+
* This interface guarantees that result objects are of the expected type as defined by the SPARQL spec.
140+
*/
141+
export type AlgebraSparqlQueryable<AlgebraType, SupportedResultType, QueryAlgebraContextType extends QueryAlgebraContext = QueryAlgebraContext> = unknown
142+
& (SupportedResultType extends BindingsResultSupport ? {
143+
queryBindings(query: AlgebraType, context?: QueryAlgebraContextType): Promise<ResultStream<Bindings>>;
144+
} : unknown)
145+
& (SupportedResultType extends BooleanResultSupport ? {
146+
queryBoolean(query: AlgebraType, context?: QueryAlgebraContextType): Promise<boolean>;
147+
} : unknown)
148+
& (SupportedResultType extends QuadsResultSupport ? {
149+
queryQuads(query: AlgebraType, context?: QueryAlgebraContextType): Promise<ResultStream<RDF.Quad>>;
150+
} : unknown)
151+
& (SupportedResultType extends VoidResultSupport ? {
152+
queryVoid(query: AlgebraType, context?: QueryAlgebraContextType): Promise<void>;
139153
} : unknown)
140-
;
154+
;
141155

142156
export type SparqlResultSupport = BindingsResultSupport & VoidResultSupport & QuadsResultSupport & BooleanResultSupport;
143157
export type BindingsResultSupport = { bindings: true };

rdf-js-query-tests.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@ import {
44
BindingsFactory,
55
Bindings,
66
Term,
7-
Queryable,
7+
StringQueryable,
8+
AlgebraQueryable,
89
SparqlResultSupport,
910
MetadataOpts,
1011
QueryStringContext,
1112
QueryAlgebraContext,
12-
AllMetadataSupport, Query, Variable, ResultStream, Quad, SparqlQueryable, BindingsResultSupport, QuadsResultSupport
13+
AllMetadataSupport,
14+
Query,
15+
Variable,
16+
ResultStream,
17+
Quad,
18+
StringSparqlQueryable,
19+
AlgebraSparqlQueryable,
20+
BindingsResultSupport,
21+
QuadsResultSupport,
1322
} from ".";
1423

1524
function test_bindings() {
@@ -38,8 +47,8 @@ function test_bindings() {
3847
}
3948
}
4049

41-
async function test_queryable() {
42-
const engine: Queryable<string, string, AllMetadataSupport, Query<SparqlResultSupport>, QueryStringContext<string>, QueryAlgebraContext<string>> = <any> {};
50+
async function test_stringqueryable() {
51+
const engine: StringQueryable<AllMetadataSupport, QueryAlgebraContext> = <any> {};
4352

4453
const query: Query<SparqlResultSupport> = await engine.query('SELECT * WHERE { ... }');
4554
switch (query.resultType) {
@@ -61,17 +70,30 @@ async function test_queryable() {
6170
}
6271
}
6372

64-
async function test_sparqlqueryable() {
65-
const engine: SparqlQueryable<string, string, QueryStringContext<string>, QueryAlgebraContext<string>, SparqlResultSupport> = <any> {};
73+
async function test_stringsparqlqueryable() {
74+
const engine: StringSparqlQueryable<SparqlResultSupport> = <any> {};
6675

6776
const bindings: ResultStream<Bindings> = await engine.queryBindings('SELECT * WHERE { ... }');
6877
const quads: ResultStream<Quad> = await engine.queryQuads('CONSTRUCT WHERE { ... }');
6978
const bool: boolean = await engine.queryBoolean('ASK WHERE { ... }');
7079
const done: void = await engine.queryVoid('INSERT WHERE { ... }');
7180
}
7281

73-
async function test_sparqlqueryable_partial() {
74-
const engine: SparqlQueryable<string, string, QueryStringContext<string>, QueryAlgebraContext<string>, BindingsResultSupport & QuadsResultSupport> = <any> {};
82+
async function test_algebrasparqlqueryable() {
83+
interface AlgebraType { mock: 'algebra' }
84+
const engine: AlgebraSparqlQueryable<AlgebraType, SparqlResultSupport> = <any> {};
85+
86+
const bindings: ResultStream<Bindings> = await engine.queryBindings({ mock: 'algebra' });
87+
const quads: ResultStream<Quad> = await engine.queryQuads({ mock: 'algebra' });
88+
const bool: boolean = await engine.queryBoolean({ mock: 'algebra' });
89+
const done: void = await engine.queryVoid({ mock: 'algebra' });
90+
91+
// @ts-ignore
92+
await engine.queryBoolean('ASK WHERE { ... }'); // Query type doesn't match AlgebraType
93+
}
94+
95+
async function test_stringsparqlqueryable_partial() {
96+
const engine: StringSparqlQueryable<BindingsResultSupport & QuadsResultSupport> = <any> {};
7597

7698
const bindings: ResultStream<Bindings> = await engine.queryBindings('SELECT * WHERE { ... }');
7799
const quads: ResultStream<Quad> = await engine.queryQuads('CONSTRUCT WHERE { ... }');
@@ -80,3 +102,15 @@ async function test_sparqlqueryable_partial() {
80102
// @ts-ignore
81103
const done: void = await engine.queryVoid('INSERT WHERE { ... }'); // Unsupported
82104
}
105+
106+
async function test_algebrasparqlqueryable_partial() {
107+
interface AlgebraType { mock: 'algebra' }
108+
const engine: AlgebraSparqlQueryable<AlgebraType, BindingsResultSupport & QuadsResultSupport> = <any> {};
109+
110+
const bindings: ResultStream<Bindings> = await engine.queryBindings({ mock: 'algebra' });
111+
const quads: ResultStream<Quad> = await engine.queryQuads({ mock: 'algebra' });
112+
// @ts-ignore
113+
const bool: boolean = await engine.queryBoolean({ mock: 'algebra' }); // Unsupported
114+
// @ts-ignore
115+
const done: void = await engine.queryVoid({ mock: 'algebra' }); // Unsupported
116+
}

0 commit comments

Comments
 (0)