cloudcreativity / laravel-json-api

JSON API (jsonapi.org) package for Laravel applications.
http://laravel-json-api.readthedocs.io/en/latest/
Apache License 2.0
778 stars 109 forks source link

Store two resources with one request? #601

Closed ProteanCode closed 3 years ago

ProteanCode commented 3 years ago

Hi there.

Simply put I have to create a multilingual API so that my Role model may have multiple RoleTranslation models attached to it.

I have tried to make it by sending this request expecting that without sending ID a resource would be created and attached, but it failed with 400 error code

My POST request body

{
    "data": {
        "type": "roles",
        "attributes": {},
        "relationships": {
            "roleTranslations": {
                "data": [
                    {
                        "type": "roleTranslations",
                        "descriptor": "name",
                        "value": "Testing role"
                    }
                ]
            }
        }
    }
}

And I got the response

{
  "errors": [
    {
      "status": "400",
      "title": "Non-Compliant JSON API Document",
      "detail": "The member id is required.",
      "source": {
        "pointer": "\/data\/relationships\/roleTranslations\/data\/0"
      }
    }
  ]
}

I understand what response says, but I wish I could avoid that since rolling back multiple requests and a distributed database transaction is a complex thing in most of cases.

My expectation was to create a Role resource first, then create a RoleTranslation resource and attach it to the Role.

Is this possible without making multiple requests?

Sidenote: ids are database generated uuids, but storing single resources works anyway so its not the case I guess

lindyhopchris commented 3 years ago

This is not supported by the JSON:API spec 1.0 - it is looking like it will be added in 1.1, but that's been the case for a while and I'm not sure when that new version will be released.

My tip would be for the moment set up your own non-JSON:API endpoint for handling this. I.e. have a route that expects application/json and can have any structure that you want. It can of course return a JSON:API resource if that is desired. That's how I've implemented this use case as an interim solution until it gets added to the spec.

ProteanCode commented 3 years ago

This is not supported by the JSON:API spec 1.0 - it is looking like it will be added in 1.1, but that's been the case for a while and I'm not sure when that new version will be released.

My tip would be for the moment set up your own non-JSON:API endpoint for handling this. I.e. have a route that expects application/json and can have any structure that you want. It can of course return a JSON:API resource if that is desired. That's how I've implemented this use case as an interim solution until it gets added to the spec.

Or I should make that two requests to store role first and assign the translation later. I guess I will do that this way even if it is not the greatest one, it still follows the pattern