Score:1

JSON:API fetch reference data

pm flag

I have a content type (MealDiary), which contains a reference to a paragraph multiple times (Meal: date, title). I do get a paragraph uid I can use to retrieve this data, but I would instead like to fetch the field data of the meal withing the MealDiary request. My goals is 1 request instead of 6.

Anybody could help on this?

unusedspoon avatar
aq flag
Take a look at https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module/fetching-resources-get there are examples to retrieve fields from referenced entities as part of the original request
Score:2
cn flag

I don't use Paragraphs but I've been doing a lot of work with JSON:API recently.

As noted by @unusedspoon, there are examples in the documentation.

However, using what core provides by default leads to really long queries that aren't much fun to work with.

Automatically include entity references

Install JSON:API Extras module and go to /admin/config/services/jsonapi/resource_types/. Select the resource type (entity bundle) you want to automatically include entity references for. Scroll down to the bottom and edit the Default include list.

Note that you can include multiple levels of entity references (for example, to include the file of a media entity: field_media_audio.file).

As noted in the UI, if you attach an include query string parameter, the default includes will be ignored, so this lets you set useful defaults but then override when necessary.

Merge entity reference info with the referencing entity

By default, JSON:API returns the entity references separately from the referencing entity. This might be useful for some people but I find it annoying because I want to access my entity reference data like myEntity.myEntityReferenceField.value. To get this behavior, you can install the JSON:API Include module.

Potney Switters avatar
pm flag
Thanks for the very elaborative reply. How can i do the merging part when the paragraph reference has multiple values?
Potney Switters avatar
pm flag
"JSON:API Include module" did it. Thanks Patrick :)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.