Skip to content

Commit d1c7a4b

Browse files
committed
feat: enable to define query used to retrieve examples when defining the html elements
1 parent f7f15f7 commit d1c7a4b

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

packages/sparql-editor/src/metadata.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -265,21 +265,23 @@ export async function getPredicatesFallback(endpoint: string) {
265265
return ["http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://www.w3.org/2000/01/rdf-schema#label"];
266266
}
267267

268+
const DEFAULT_QUERY_GET_EXAMPLES = `PREFIX sh: <http://www.w3.org/ns/shacl#>
269+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
270+
SELECT DISTINCT ?sq ?comment ?query
271+
WHERE {
272+
?sq a sh:SPARQLExecutable ;
273+
rdfs:comment ?comment ;
274+
sh:select|sh:ask|sh:construct|sh:describe ?query .
275+
} ORDER BY ?sq`;
276+
268277
// Retrieve example queries from the SPARQL endpoint
269-
export async function getExampleQueries(endpoint: string): Promise<ExampleQuery[]> {
278+
export async function getExampleQueries(
279+
endpoint: string,
280+
queryToGetExamples: string = DEFAULT_QUERY_GET_EXAMPLES,
281+
): Promise<ExampleQuery[]> {
270282
const exampleQueries: ExampleQuery[] = [];
271283
try {
272-
const queryResults = await queryEndpointMeta(
273-
`PREFIX sh: <http://www.w3.org/ns/shacl#>
274-
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
275-
SELECT DISTINCT ?sq ?comment ?query
276-
WHERE {
277-
?sq a sh:SPARQLExecutable ;
278-
rdfs:comment ?comment ;
279-
sh:select|sh:ask|sh:construct|sh:describe ?query .
280-
} ORDER BY ?sq`,
281-
endpoint,
282-
);
284+
const queryResults = await queryEndpointMeta(endpoint, queryToGetExamples);
283285
queryResults.forEach((b, index) => {
284286
exampleQueries.push({comment: b.comment.value, query: b.query.value, index: index + 1, iri: b.sq.value});
285287
});

packages/sparql-editor/src/sparql-editor.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class SparqlEditor extends HTMLElement {
5959
examplesRepoAddUrl: string | undefined;
6060
addLimit: number | undefined;
6161
dialogElOpen: HTMLDialogElement | undefined;
62+
queryToGetExamples: string | undefined;
6263
// TODO: make exampleQueries a dict with the query IRI as key, so if the window.location matches a key, it will load the query?
6364

6465
constructor() {
@@ -121,7 +122,11 @@ export class SparqlEditor extends HTMLElement {
121122
this.meta[endpoint].classes,
122123
this.meta[endpoint].predicates,
123124
],
124-
] = await Promise.all([getExampleQueries(endpoint), getPrefixes(endpoint), getVoidDescription(endpoint)]);
125+
] = await Promise.all([
126+
getExampleQueries(endpoint, this.queryToGetExamples),
127+
getPrefixes(endpoint),
128+
getVoidDescription(endpoint),
129+
]);
125130
this.meta[endpoint].retrievedAt = new Date().toISOString();
126131

127132
if (Object.keys(this.meta[endpoint].prefixes).length === 0) {
@@ -201,6 +206,8 @@ export class SparqlEditor extends HTMLElement {
201206
this.endpoints = (this.getAttribute("endpoint") || "").split(",").map(e => e.trim());
202207
this.meta = this.loadMetaFromLocalStorage();
203208

209+
this.queryToGetExamples = this.getAttribute("query-to-get-examples") || undefined;
210+
204211
// NOTE: will need to be removed at some point I guess
205212
// Check if examples contain the index field, if not reset cache
206213
if (this.currentEndpoint() && this.currentEndpoint().examples?.some(example => example.iri === undefined)) {

0 commit comments

Comments
 (0)