mathematic-inc / ts-japi

A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification
Apache License 2.0
202 stars 15 forks source link

[FR] Relationships without included #75

Closed evoactivity closed 7 months ago

evoactivity commented 7 months ago

Is your feature request related to a problem?

Is there a way to create relationships that only adds to the resource relationships object, so no additional documents are included at the top level? I don't see anything in the docs outlining how to do that.

Describe the solution you'd like

An option to turn included off

Additional context

The object I pass into the relator may look something like

{
  id: '1',
  name: 'thing',
  userId: '1'
}

I use this userId to lookup the entity to relate and include, I'd like an option to only relate. So I end up with

{
  data: {
    type: 'thing-model',
    id: '1',
    attributes: {
      name: 'thing'
    }.
    relationships: {
      user: {
        type: 'user',
        id: '1'
      }
    }
  }
}

instead of

{
  data: {
    type: 'thing-model',
    id: '1',
    attributes: {
      name: 'thing'
    }.
    relationships: {
      user: {
        type: 'user',
        id: '1'
      }
    }
  },
  included: [
    {
      type: 'user',
      id: '1',
      attributes: {
        something: 'here'
      }
    }
  ]
}

This way my client knows about the relationship and can decide if it wants to load it.

evoactivity commented 7 months ago

Realised I can do this by setting the depth to 0

evoactivity commented 7 months ago

Actually, a way to include some relationships and not others would be useful. Depth is all or nothing. So I guess I'd want to pass an option to the relator specifically, not the entire serializer.

evoactivity commented 7 months ago

Not sure how I kept missing the include option last night. That works fine :)