Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
GraphQL-LD allows Linked Data to be queried via GraphQL queries and a JSON-LD context.
It is a developer-friendly way to query Linked Data and use the results in a straightforward way.
For example, assuming the following SPARQL query:
SELECT ?id ?starring WHERE {
OPTIONAL {
?id <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Film>;
<http://dbpedia.org/ontology/starring> ?starring.
?starring <http://www.w3.org/2000/01/rdf-schema#label> "Brad Pitt"@en.
}
}
This could be written in a more compact way in GraphQL:
{
id
... on Film {
starring(label: "Brad Pitt")
}
}
And this can be based on the following JSON-LD context:
{
"@context": {
"Film": "http://dbpedia.org/ontology/Film",
"label": { "@id": "http://www.w3.org/2000/01/rdf-schema#label", "@language": "en" },
"starring": "http://dbpedia.org/ontology/starring"
}
}
This library takes a GraphQL-LD query and a JSON-LD context as input, converts it to a SPARQL query, sends the SPARQL query to a SPARQL query engine for execution (local or endpoint), and converts the SPARQL query results into a tree-based structure corresponding to the original GraphQL query.
More information about this approach can be found in our GraphQL-LD article.
$ yarn add graphql-ld
This package also works out-of-the-box in browsers via tools such as webpack and browserify.
import {Client} from "graphql-ld";
or
var Client = require("graphql-ld").Client;
This requires you to install graphql-ld-comunica: yarn add graphql-ld-comunica
.
import {Client} from "graphql-ld";
import {QueryEngineComunica} from "graphql-ld-comunica";
// Define a JSON-LD context
const context = {
"@context": {
"label": { "@id": "http://www.w3.org/2000/01/rdf-schema#label" }
}
};
// Create a GraphQL-LD client based on a client-side Comunica engine over 3 sources
const comunicaConfig = {
sources: [
{ type: "sparql", value: "'http://dbpedia.org/sparql'" },
{ type: "file", value: "https://linkedsoftwaredependencies.org/bundles/npm/componentsjs" },
{ type: "hypermedia", value: "https://fragments.linkedsoftwaredependencies.org/npm" },
],
};
const client = new Client({ context, queryEngine: new QueryEngineComunica(comunicaConfig) });
// Define a query
const query = `
query @single {
label
}`;
// Execute the query
const { data } = await client.query({ query });
This requires you to install graphql-ld-sparqlendpoint: yarn add graphql-ld-sparqlendpoint
.
import {Client} from "graphql-ld";
import {QueryEngineSparqlEndpoint} from "graphql-ld-sparqlendpoint";
// Define a JSON-LD context
const context = {
"@context": {
"label": { "@id": "http://www.w3.org/2000/01/rdf-schema#label" }
}
};
// Create a GraphQL-LD client based on a SPARQL endpoint
const endpoint = 'http://dbpedia.org/sparql';
const client = new Client({ context, queryEngine: new QueryEngineSparqlEndpoint(endpoint) });
// Define a query
const query = `
query @single {
label
}`;
// Execute the query
const { data } = await client.query({ query });
Below, you can find a couple examples of GraphQL-LD queries.
If you want more details on what kind of queries you can write, have a look at the README of the GraphQL-to-SPARQL repository.
Query:
query @single {
label
}
Context:
{
"@context": {
"label": { "@id": "http://www.w3.org/2000/01/rdf-schema#label" }
}
}
Output:
{
"data": {
"label": [
"amateur victory",
"amateur year",
"ambasadóir",
"ambasciatore",
"ambassadeur",
"ambassadeur",
"ambassador",
...
]
}
}
Query:
{
id @single
... on Film {
starring(label: "Brad Pitt") @single
}
}
Context:
{
"@context": {
"Film": "http://dbpedia.org/ontology/Film",
"label": { "@id": "http://www.w3.org/2000/01/rdf-schema#label", "@language": "en" },
"starring": "http://dbpedia.org/ontology/starring"
}
}
Output:
{
"data": [
{
"id": "http://dbpedia.org/resource/Ocean's_Eleven",
"starring": "http://dbpedia.org/resource/Brad_Pitt"
},
{
"id": "http://dbpedia.org/resource/The_Favor",
"starring": "http://dbpedia.org/resource/Brad_Pitt"
},
{
"id": "http://dbpedia.org/resource/The_Assassination_of_Jesse_James_by_the_Coward_Robert_Ford",
"starring": "http://dbpedia.org/resource/Brad_Pitt"
},
{
"id": "http://dbpedia.org/resource/True_Romance",
"starring": "http://dbpedia.org/resource/Brad_Pitt"
},
...
]
}
Query:
{
softwareName: label @single
developer @single(scope: all) {
label
country(label_en: "Belgium")
}
}
Context:
{
"@context": {
"label": { "@id": "http://www.w3.org/2000/01/rdf-schema#label" },
"label_en": { "@id": "http://www.w3.org/2000/01/rdf-schema#label", "@language": "en" },
"developer": { "@id": "http://dbpedia.org/ontology/developer" },
"country": { "@id": "http://dbpedia.org/ontology/locationCountry" }
}
}
Output:
{
"data": [
{
"softwareName": "Divinity: Original Sin II",
"developer": {
"label": "Larian Studios",
"country": "http://dbpedia.org/resource/Belgium"
}
},
{
"softwareName": "Divinity: Original Sin II",
"developer": {
"label": "Larian Studios",
"country": "http://dbpedia.org/resource/Belgium"
}
},
{
"softwareName": "BioNumerics",
"developer": {
"label": "Applied Maths",
"country": "http://dbpedia.org/resource/Belgium"
}
},
...
]
}
This software is written by Ruben Taelman.
This code is released under the MIT license.
[v1.0.0] - 2019-06-13
Initial release
FAQs
Linked Data Querying with GraphQL
The npm package graphql-ld receives a total of 293 weekly downloads. As such, graphql-ld popularity was classified as not popular.
We found that graphql-ld demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.