Skip to content

Instantly share code, notes, and snippets.

@tpluscode
Last active April 14, 2021 10:54
Show Gist options
  • Save tpluscode/3b79d0d99bace61f1c8b25fe7f825a3c to your computer and use it in GitHub Desktop.
Save tpluscode/3b79d0d99bace61f1c8b25fe7f825a3c to your computer and use it in GitHub Desktop.
Hydra relative

Proposal for how to describe resources with Hydra to achieve search functionality using relative URL templates

The API Documentation describes a :UserCollection class, which supports a search property :searchUsers to have the collection filtered by constructing a URL Template <api/user-search-template>. The template has a URL identifier to allow sharing it between the API Documentation and actual resources to filter.

Given the two example instances of the collection, the client would be able to construct URLs relative to the subject of :searchUsers predicate like:

/users?first=John
/users/unverified?age=20

The <api/user-search-template> hydra:resolveRelativeTo hydra:LinkContext triple is crucal enabler of that functionality

<api>
a hydra:ApiDocumentation ;
hydra:supportedClass :UserCollection ;
.
:UserCollection
a hydra:Class ;
hydra:supportedProperty [
hydra:property :searchUsers ;
] ;
.
:searchUsers
rdfs:subPropertyOf hydra:search ;
hydra:supportedOperation [
hydrabox:variables <api/user-search-template> ;
hydra:method "GET" ;
] ;
.
<api/user-search-template>
a hydra:IriTemplate ;
hydra:template "{?first,last,age}" ;
hydra:resolveRelativeTo hydra:LinkContext ;
hydra:mapping [
hydra:variable "first" ;
hydra:property schema:givenName ;
] , [
hydra:variable "last" ;
hydra:property schema:familyName ;
] , [
hydra:variable "age" ;
hydra:property schema:age ;
]
.
<users/unverified>
a :UserCollection ;
:searchUsers <api/user-search-template> ;
hydra:manages [
hydra:property rdf:type ;
hydra:object :User ;
] , [
hydra:property :verified ;
hydra:object false ;
]
.
<users>
a :UserCollection ;
:searchUsers <api/user-search-template> ;
hydra:manages [
hydra:property rdf:type ;
hydra:object :User ;
] , [
hydra:property :verified ;
hydra:object true ;
]
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment