Extracting properties of a hydra:class or members of a hydra:collection with heracles.ts

666 views Asked by At

I'm looking for guidance in working with hydra:class and hydra:collection/hydra:member object properties from responses gotten from a custom Hydra endpoint requested with the reference client in TypeScript, Heracles.ts.

For example, consider the following JSON-LD response from my server:

{
  "context": {...},
  "member": [
    {
      "@id": "example:2bfd5353-6842-48f7-9f8c-111474f57dfb",
      "@type": "schema:person",
      "label": "Person 1"
    },
    {
      "@id": "example:108d8384-a55a-4b21-8511-654fb3830d5f",
      "@type": "schema:person",
      "label": "Person 2"
    },
  ],
}

I can get this response and loop over it just fine with the following code:

const data = await Client.getResource(
  "http://example.com/api/persons"
);
for (const person of data.members) {
  const iri = person.iri;
  const label = person.label; // How can I do this?
}

I could not figure out how to extract properties apart from the iri and type from the response. The documentation and use cases in the Hydra repository only talk about things like do something with the object (for example in the Use Case Draft 4) without showing how to do it. I also couldn't find concrete examples in the tests and other source code in the repo.

Any help is greatly appreciated.

Best, Daniel

1

There are 1 answers

0
yngwi On BEST ANSWER

I have asked the same question on the Heracles GitHub Repository and have gotten the answer that accessors for content apart from the Hydra Metadata need to be implemented manually, similar to this:

const data = await Client.getResource(
  "http://localhost:4000/persons"
);
const graph = await jsonld.expand(await data.json());
for (const person of data.members) {
  const personResource = graph["@graph"].find(_ => _["@id"] === person.iri);
  const label = personResource["http://some.uri/vocab#label][0]["@value"];
}

Source: alien-mcl on GitHub