Update ApiClient Serializer Type to Include serialize method

Created on 26 July 2023, over 1 year ago
Updated 4 March 2024, 9 months ago

Problem/Motivation

Did some experimentation now that we have both deserialization and also create, update and delete methods. Based on that experimentation, I think:

* Having a complimentary serializer would be useful. The main use case would be getting a deserialized resource, updating a field on the deserialized object, and then updating the resource in the CMS using updateResource. Drupal won't accept the deserialized version of the resource, so you would need to serialize it before using it in the body of the updateResource method. Doing this would be a lot more convenient if the client provided access to a complimentary serializer for the deserializer that was used.
* I don't think it makes sense at this point to 'automatically' serialize data similar to how we automatically deserialize data. There are too many different libraries in this space (and many are old or not maintained), most have pretty different signatures, and from my testing none of them serialize perfectly for Drupal - you'll need to make a few small tweaks before trying to update a resource in the CMS. If someday there was a serializer library that worked perfectly with Drupal, this could be a different story.

Proposed resolution

* Update the ApiClient Serializer type to also include a serialize property which accepts a serialize function. This function type will need to be pretty open ended as the signatures of these serializers vary quite a bit (for Jsona you have to pass the data in as 'stuff' LOL).
* client.serializer.serialize should be callable, but as of now won't be used automatically internally in the JsonApiClient.
* For sake of example, we should make sure this works with https://github.com/olosegres/jsona (probably the most common option) and https://github.com/andersondanilo/jsonapi-fractal (a good example of a functional alternative with a different signature)

Remaining tasks

* Update the Serializer type.
* Test serializing with Jsona and jsonapi-fractal.
* Add tests.
Update documentation.

API changes

* serializer option may now have a serialize method which will be callable.

✨ Feature request
Status

Fixed

Component

Code

Created by

🇺🇸United States brianperry

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024