Added DKG SPARQL query tool and minor code cleanup#13
Conversation
…ing in dev mode, and made some general code cleanup in the plugin-dkg-essentials.
|
|
||
| // Only allow query types supported by the DKG node | ||
| const allowedQueryTypes = ["SELECT", "CONSTRUCT"]; | ||
| if (!allowedQueryTypes.includes(parsed.queryType)) { |
There was a problem hiding this comment.
We could normalize the case here to make sure queryTyoe is always in uppercase. So something like this:
if (!allowedQueryTypes.includes(parsed.queryType?.toUpperCase()))
There was a problem hiding this comment.
The sparqljs parser already returns queryType in uppercase (it's part of the library's contract). So if you parse "select * where { ?s ?p ?o }", the parser returns { queryType: "SELECT", ... } - but I it doesn't hurt to add another check in case the library changes.
| } | ||
|
|
||
| // Use validated query type | ||
| const queryType = validation.queryType || "SELECT"; |
There was a problem hiding this comment.
if query passes validation, queryType should always be set so this fallback might hide potential bugs
There was a problem hiding this comment.
Good point, this has been updated.
| const queryResult = await ctx.dkg.graph.query(query, queryType); | ||
|
|
||
| const resultText = JSON.stringify(queryResult, null, 2); | ||
|
|
||
| return { | ||
| content: [ | ||
| { | ||
| type: "text", | ||
| text: `✅ Query executed successfully\n\n**Results:**\n\`\`\`json\n${resultText}\n\`\`\``, | ||
| }, |
There was a problem hiding this comment.
Should we use JSON.stringify when a CONSTRUCT query is called since construct returns a raw string of N-triples?
There was a problem hiding this comment.
Good catch, updated so that SELECT and CONSTRUCT queries are handled differently based on their output format. Tested it out and works well now.
Added DKG SPARQL query tool to plugin-dkg-essentials, fixed the concurrency issue when running in dev mode, and made some general code cleanup in the plugin-dkg-essentials.