Master-detail view in Json-LD

On the forhand: sorry if I misunderstood hypermedia or Restfull concepts: this is a work in progress ...)

I am trying to compute hypermedia and hydra ( http://www.markus-lanthaler.com/hydra ) and you have questions about returning information to the client before creating the api.

Let's say I have an online store located at www.myshop.com.

An HTTP GET for the root can return (for example) a list of resources provided as a link (in the json-ld doc):

...
"@id": "/api",
"products" : "www.myshop.com/api/products",
"customers":"www.myshop.com/api/customers"
...

      

First hydra question , how can I add actions here? it seems like the client has to download another document before downloading the app. What I mean is that the potential action is not listed in the document obtained from www.myshop.com/api. Or am I missing something?


Further, I said that the products are hydra: Link so that the client can follow this link (interact with it) using HTTP GET and get the list of products. this will be the following list:

....
{
  "@id": "/api/products/123",
  "@type": "vocab:Product"
},
{
  "@id": "/api/products/124",
  "@type": "vocab:Product"
},
....

      

here the client gets a list of products (it could be a collection being uploaded). But if the customer wants to display it to the user, say a table with [product id, price, name] (not all product properties)

Second question . How could I do this without the client sending a request to the server for each product, but still provide a link to get the product details (or even there are four links here: one for detailed information, one for deleting and one for sharing it with friend and last to add it to cart)?

Actually I'm having a hard time figuring out how hydra comes into play without having links in the document itself? I think Hal is using this approach, having links in the document itself (if I'm right) and I'm trying to find how hydra makes this link ...

considers

+3


source to share


2 answers


A bit late, but I'll still try to answer your Cedric questions.

Let's say I have an online store located at www.myshop.com.

An HTTP GET for the root can return (for example) a list of resources provided as a link (in the json-ld document):

 ... "@id": "/api",
 "products" : "www.myshop.com/api/products",
 "customers":"www.myshop.com/api/customers" ...

      

First hydra question , how can I add actions here? it seems the client has to download another document before downloading the app. I mean the potential actions are not listed in the document retrieved from www.myshop.com/api Or am I missing something?

Basically you have two options: 1) embed operations directly into the response, or 2) attach operations to properties ( products

, customers

).

Approach 1) looks something like this:

...
"@id": "/api",
"products" : {
  "@id": "http://www.myshop.com/api/products",
  "operation": {
    "@type": "Operation",
    "method": "POST",
    "expects": "Product"
  }
}
...

      

Whereas approach 2) attaches the same operation to a property products

in the mentioned Hydra ApiDocumentation:

...
"@id": "...products",
"supportedOperation": {
  "@type": "Operation",
  "method": "POST",
  "expects": "Product"
}
...

      

Note that in 1) I used operation

, and in 2) I used supportedOperation

. Also, you must use a more specific type than operation

.



Regarding your second question:

using HTTP GET and getting a list of products. this will be the following list:

....
{
  "@id": "/api/products/123",
  "@type": "vocab:Product"
},
{
  "@id": "/api/products/124",
  "@type": "vocab:Product"
},
....

      

here the customer gets a list of products (this could be a page collection). But if the customer wants to display it to the user, let's say a table with [product id, price, name] (not all properties)

The second question . How can I do this without the client sending to query the server for each product, but still provide a link to get the product details (or even here, having four links: one for getting detailed information, one for deleting and one for sharing it with a friend and last to add it to your cart)?

You can add as much information (including links) as you like directly to the collection.

....
{
  "@id": "/api/products/123",
  "@type": "vocab:Product",
  "name": "Product 123",
  "price": "9.99"
},
{
  "@id": "/api/products/124",
  "@type": "vocab:Product",
  "name": "Product 124",
  "price": "19.99"
},
....

      

Thus, the client only needs to dereference the element if the collection does not contain this required information.

Actually I'm having a hard time figuring out how hydra comes in to play without having links in the document itself?

Of course, you have links in the document too. Links are just properties whose values ​​are URLs (objects with a property @id

, unless you set the type of the property @id

in the context to get rid of it), instead of treating them specifically.

+4


source


Note: part of Hydra's answers I'm not sure about is JSON-LD and REST I guess.

You can use @base

relative IRIs from JSON-LD as well, or you can define namespaces in @context

, so after that you can use relative IRIs like ns:relativeIRI

. Each one is better than returning a full IRI. (It is easier to parse the results with a generic client-side JSON-LD parser rather than a simple JSON parser.)

You can define your own @vocab

using the Diva guide, or you can add definitions for "actions" in @context

. If you want to "add actions" you must use subcategories hydra:Operation

in your vocab. Something like this (but I'm not a Hydra expert):

{
    "@id": "vocab:ProductList",
    //...
    "hydra:supportedOperations": [
        {
            "@type": "hydra:CreateResourceOperation",
            "method": "POST",
            "expects": "vocab:Product"
        }
        //...
    ]
}

      



Generally, the REST, if you need the same resource with fewer features, you need to add a new IRI for this resource, for example: /myresource?fewer=1

. For example, in your case: /api/products/?fields="id, price, name"

ok.

By Hydra you have 2 options if you want multiple links; you can add a new one hydra:Link

as a property, or you can add a new one hydra:Operation

as a supportedOperation

with method: GET

. I assume the operations are done for something like a search that has user input, but if you don't want to add a new property for each link, I think you have no other choice.

In fact, Hydra has link and operation support. It may be confusing, but JSON-LD is an RDF format, you can define three times in RDF. So the IRIs you used for example "customers":"www.myshop.com/api/customers"

are just resource IDs, not links. The link must have an IRI, title, method (GET), language, content-type, iana: relation, etc., so it is not possible to describe a link that you can follow with only one IRI (resource identifier). When processing a REST resource, the client should never check the IRI structure to know how to display what it received from you. You have to check other properties of the links, especially iana: relations or Hydra, perhaps the type of operation to do this. So, for example, in your casewww.myshop.com/api/dav8ufg723udvbquacvd723fudvg

is a perfectly valid IRI for a client list. We only use good IRIs because they are easier to configure by generating them on the server side and setting up a router for them.

Check Hydra vocab before additional questions . As you can see, Class

can have supportedOperations

and supportedProperties

, which are both collections. A Link

is a subclass Property

that can have one Operation

. For collections, I think you should use a class Collection

that member

contains the elements of the collection ... Please be aware that JSON-LD makes no difference defining one element or multiple elements with the same type. In context, you only need to define the type, and the property value can contain either a single element or an array of elements ... If you want to constrain this, I think you need to add some OWL styles and a validator that validates the values ​​using them.

+2


source







All Articles