Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure defaultQuery for YASGUI #366

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/rare-forks-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"trifid-plugin-yasgui": minor
---

It is now possible to configure the default SPARQL query to use in each new YASGUI tab, by configuring the `defaultQuery` configuration field.

This feature is useful to provide a starting point for users who are not familiar with SPARQL queries and want to explore the data available in the SPARQL endpoint.

You can define the default query in order to show a specific set of triples, or to show a specific number of triples, or to show a specific set of properties, etc. based on your data model.

It can be also useful to also include common prefixes in the default query, so users can start writing their queries without having to remember the prefixes.
8 changes: 8 additions & 0 deletions packages/yasgui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ plugins:
config:
endpointUrl: https://example.com/query
urlShortener: https://example.com/api/v1/shorten
defaultQuery: |
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT * WHERE {
?sub ?pred ?obj .
} LIMIT 10
# …other configuration fields
```

Expand All @@ -35,3 +42,4 @@ The following options are supported:
- `urlShortener`: URL of an URL Shortener service. It will be called like this (assuming `urlShortener` is `https://example.com/api/v1/shorten`): `https://example.com/api/v1/shorten?url=url-to-your-query` and should return a short URL as plain text (`https://example.com/s/x8Z1a`). If `urlShortener` is not defined, the short URL feature will be disabled in YASGUI.
- `template`: Path to an alternative template (default: `views/yasgui.hbs`)
- `catalog`: Array of SPARQL endpoints that will be shown in the YASGUI interface.
- `defaultQuery`: Default query that will be shown in the YASGUI interface.
5 changes: 4 additions & 1 deletion packages/yasgui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const currentDir = dirname(fileURLToPath(import.meta.url))
/** @type {import('../core/types/index.js').TrifidPlugin} */
const trifidFactory = async (trifid) => {
const { config, render, server } = trifid
const { template, endpointUrl, urlShortener, catalog } = config
const { template, endpointUrl, urlShortener, catalog, defaultQuery } = config

const endpoint = endpointUrl || '/query'
const view = !template ? `${currentDir}/views/yasgui.hbs` : template
Expand All @@ -19,6 +19,8 @@ const trifidFactory = async (trifid) => {
throw new Error('"catalog" option must be an array')
}

const defaultQueryOption = defaultQuery || ''

// Serve static files for YASGUI
const yasguiPath = resolve('@zazuko/yasgui/build/', import.meta.url)
server.register(fastifyStatic, {
Expand Down Expand Up @@ -85,6 +87,7 @@ const trifidFactory = async (trifid) => {
endpointUrl: endpointUrl.toString(),
catalogueEndpoints,
urlShortener,
defaultQuery: JSON.stringify(defaultQueryOption),
},
{ title: 'YASGUI' },
)
Expand Down
4 changes: 4 additions & 0 deletions packages/yasgui/views/yasgui.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@
{{/if}}

<script>
const defaultQuery = {{{ defaultQuery }}}
const catalogueEndpoints = [...new Set(JSON.parse('{{{ catalogueEndpoints }}}'))]
Yasgui.defaults.requestConfig.endpoint = '{{ endpointUrl }}'
Yasgui.defaults.endpointCatalogueOptions.getData = () => {
return catalogueEndpoints.map((endpoint) => {
return { endpoint }
})
}
if (defaultQuery) {
Yasgui.Yasqe.defaults.value = defaultQuery
}
const yasgui = new Yasgui(document.getElementById('yasgui'), config)
</script>
</div>
Loading