Skip to content

Instantly share code, notes, and snippets.

@rubensworks
Last active September 28, 2023 15:50
Show Gist options
  • Save rubensworks/9d6eccce996317677d71944ed1087ea6 to your computer and use it in GitHub Desktop.
Save rubensworks/9d6eccce996317677d71944ed1087ea6 to your computer and use it in GitHub Desktop.

GraphQL-LD

GraphQL-LD is a way to query Linked Data using GraphQL.

Instead of querying GraphQL interfaces, Linked Data interfaces are queried, such as SPARQL endpoints, TPF interfaces, Linked Data documents, ... This is done by semantifying GraphQL queries using a JSON-LD context.

Try it out from your browser: http://query.linkeddatafragments.org/

Alternatively, install GraphQL-LD or Comunica SPARQL and execute GraphQL-LD queries on your machine

The basics

In order to execute queries, you'll need to parts:

  1. A GraphQL query: to select data
  2. A JSON-LD context: to map query fields to URIs

With this, GraphQL queries can be translated into a SPARQL query, using which any Linked Data source can be queried.

Example

Find all labels in a dataset

GraphQL query:

{
  label
}

JSON-LD context:

{
  "@context": {
    "label": { "@id": "http://www.w3.org/2000/01/rdf-schema#label" }
  }
}

This query will be translated into the following triple pattern:

_:b <http://www.w3.org/2000/01/rdf-schema#label> ?label

More information

If you want more details on how GraphQL-LD works, please refer to our article or our GraphQL-LD tool

Related software

@Rakeshjdev
Copy link

I have jsonld and my requirement is to map jsonld to Grapql schema so that i can do get and post operation with graphql . how will i achieve this. please suggest. please give a reference so that i can achieve this task.

@pebran
Copy link

pebran commented Aug 30, 2022

I haven’t found - in any article about GraphQL-LD - a mentioning about using a GraphQL-replacement for prefixes. Like using ‘foaf_Person’ or ‘skos_prefLabel’ to represent foaf:Person and skos:prefLabel respectively.
Is there a preferred way, a convention to do this?
It would be nice/practical if we could agree on a pattern.

@rubensworks
Copy link
Author

@pebran GraphQL-LD requires users to encode prefixes (or vocabs) via the JSON-LD context.

So rdfs_label would be written as label in the query when the following JSON-LD context is configured:

{
  "@context": {
    "label": { "@id": "http://www.w3.org/2000/01/rdf-schema#label" }
  }
}

This avoids tight coupling between the schema and query layer.

@pebran
Copy link

pebran commented Aug 30, 2022

Thank you for the quick answer.
So a potential conflict between labels given to classes or properties is to be solved in the @context, like:

{
"@context": {
"person_Person": { "@id": "https://www.w3.org/ns/person#Person" },
”foaf_Person” : { "@id": "http://xmlns.com/foaf/0.1/Person" }
}
}

Still hoping for a general agreement to use this kind of notation though ;-)

@LahiruOshara
Copy link

what does the @single annotation do?

@AnasShahab
Copy link

Hi, is there a filter option available? For example, to filter saying the language of a label to be German?

{
movie {
name (lang="de")
}
}

@mrkvon
Copy link

mrkvon commented Feb 23, 2023

➡️ Edit: I'm sorry, i think my example is wrong. Just tried it out, and it doesn't really seem to filter

Hi, is there a filter option available? For example, to filter saying the language of a label to be German?

{ movie { name (lang="de") } }

@AnasShahab

One possible way may be to specify the language in the @context:

"@context": {
    "label": { "@id": "http://www.w3.org/2000/01/rdf-schema#label" },
    "label_de": { "@id": "http://www.w3.org/2000/01/rdf-schema#label", "@language": "de" },
}

and then you'd use it

{ movie { label_de } }

Check out the examples on Comunica website.

I'm completely new to graphql-ld, so there might be other ways, idk... hope this helps, anyways...

@AnasShahab
Copy link

➡️ Edit: I'm sorry, i think my example is wrong. Just tried it out, and it doesn't really seem to filter

Hi, is there a filter option available? For example, to filter saying the language of a label to be German?
{ movie { name (lang="de") } }

@AnasShahab

One possible way may be to specify the language in the @context:

"@context": {
    "label": { "@id": "http://www.w3.org/2000/01/rdf-schema#label" },
    "label_de": { "@id": "http://www.w3.org/2000/01/rdf-schema#label", "@language": "de" },
}

and then you'd use it

{ movie { label_de } }

Check out the examples on Comunica website.

I'm completely new to graphql-ld, so there might be other ways, idk... hope this helps, anyways...

@mrkvon
It provides a lang tag to an item when that item is passed as an argument. However, it does not work with variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment