3

I need to create a rest service that delivers the current data:

getNode(nid)
-> all node fields
   -> if field is entity reference (like file, human readable urls)
       -> include subfields 
   -> if field is type of paragraph, include sub fields again in human readable form

It seems that Drupal is not yet ready for Rest without having to create new requests for every peace of content that is referenced with an id.

For example I have a Pinterest wall kind of output. Each item is created by using a paragraph element (sometimes with other references like taxonomy etc)

I want a clean, json list. Structured correctly without missing nested elements like paragraphs, file or taxonomy references. In this moment I just get the id's of the referenced elements.

Do I have to call now 100+ requests to collect all data for this wall?

1) Or should I make one request for collecting the node:

2) Another request to collect all 1st level entity references. Afterwards to store them in an array.

3) Afterwards making request for 2nd level entity references.

This seems horrible to me when it comes to questions about overhead and time.

Of course I have checked out some modules that should help with this issue:

  • rest_export_nested module which does not work with paragraphs entity types

  • rest_views does seem to output the fields as html instead of json. There is no real solution so far.

So what to do? Another way could be to create a view of paragraphs with target id of [NODE_ID]. But this would work just for 1st level paragraphs. The problem would still exist for nested paragraphs.

Is really the only way to create a custom module that creates it's own rest service?

Is the JSON API maybe another solution I haven't looked at so far?

Any input is appreciated. Thanks.

  • Views “rest export” isn’t quite built for this. – Kevin Jul 16 '18 at 23:34
1

No. Core REST & REST Views aren't the right solution here. You don't need a custom module for this.

As you mentioned, JSON API module supports inclusion of entity references in a single resource request. Each entity requested has a property called relationships which are references to other entities that can be included with via the include parameter. References, within references (e.g. a node referencing a media entity, that references a file entity) are supported too, albeit with some work to follow references (e.g. when you a request inclusion via the include property you're given an array property, included, which you still have to sort through via id).

If you're looking for a Drupal API that follows entity references within returned entity objects, you should use the GraphQL module as it will allow you to define your own data spec that can expand entity references as you see fit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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