[JsonApiClient] getRelationship Utility

Created on 5 March 2024, 9 months ago
Updated 5 May 2024, 7 months ago

Problem/Motivation

Traversing relationships without deserializing data is kind of gross.

Take an image on an Umami article - you end up having to do something like:

const mediaId = article.relationships.field_media_image.data.id;
const mediaEntity = included.find((obj) => obj.id === mediaId);
const fileId = mediaEntity.relationships.field_media_image.data.id;
const fileEntity = included.find((obj) => obj.id === fileId);
const fileUrl = fileEntity.attributes.uri.url;

We could provide a small utility to make this easier.

Steps to reproduce

Proposed resolution

A quick example could be something like:

const getRelationship = (entity, includes, relationship) => {
  const id = entity.relationships[relationship].data.id;
  return included.find((obj) => obj.id === id);
};

const mediaEntity = getRelationship(article, included, "field_media_image");
const fileEntity = getRelationship(mediaEntity, included, "field_media_image");
const fileUrl = fileEntity.attributes.uri.url;

Would be nice if relationship was an array so we could do something like:

getRelationship(article, included, ["field_media_image", "field_media_image"]);

And just return the file entity rather than having to do a two step process.

Remaining tasks

* Implementation
** Consider entity reference fields with multiple values here.
* Docs
* Tests

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States brianperry

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024