1

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?

1

1 Answer 1

2

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.

2
  • Thanks for the very elaborative reply. How can i do the merging part when the paragraph reference has multiple values? Commented Nov 3, 2022 at 13:37
  • "JSON:API Include module" did it. Thanks Patrick :) Commented Nov 3, 2022 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.