Skip to content

How to serialize relationship referencing an external resource? #412

Closed
@jelhan

Description

@jelhan

One of my models has a belongs-to relationship, which references a model managed by another microservice. I'm wondering how to include resource linkage data for that one? As it's a belongs-to relationship the services knows the ID. There is no need to request the other microservice and I like to avoid that one to prevent coupling.

Let me give an example to make it more clear. Lets assume I've a posts resource, which has a author relationship. The authors resources referenced are hold by another service.

The resource object for a post should look like this:

{
  "type": "posts",
  "id": "1",
  "relationships": {
    "author": {
      "data": {
        "type": "authors",
        "id": "2"
      }
    }
  }
}

I don't want to include the authors resource as a compound document.

I think I need to return the reference in getRelationships method of posts's schema. But how? I've noticed that neomerx/json-api includes a Neomerx\JsonApi\Schema\Identifier, which should serve this use case. But it's not available in v1 used by Laravel JSON API. Is there any other trick? If possible I would like to avoid creating a model for authors as it's not owned by this microservice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions