timacdonald / json-api

A lightweight API resource for Laravel that helps you adhere to the JSON:API standard. Supports sparse fieldsets, compound documents, and more.
586 stars 40 forks source link

included section not showing in my json response #52

Closed Nelwhix closed 6 months ago

Nelwhix commented 6 months ago

Hi Tim, great work on your package. It makes implementing the JSON:API spec a breeze. Please I have a issue with the adding relationships section. I think I followed the docs verbatim but the outcome is different.

So this is from my controller

      $flow = QueryBuilder::for(Flow::class)
            ->allowedIncludes(['items'])->find($flowId);
        if (null === $flow) {
            return new NotFound();
        }

        return new OK(
            message: __('responses.ok.show', ['resourceType' => 'flow']),
            data: FlowResource::make($flow)
        );

Then in my FlowResource I have:

    class FlowResource extends JsonApiResource
{
    protected array $relationships = [
        'items' => FlowItemResource::class,
    ];

    /**
     * @return array<string, mixed>
     */
    public function toAttributes(Request $request): array
    {
        return [
            'name' => $this->name,
            'description' => $this->description,
            'num_requests' => $this->num_requests,
            'num_views' => $this->num_views,
        ];
    }
}

Then in my FlowItemResource I have:

class FlowItemResource extends JsonApiResource
{
    /**
     * @return array<string, mixed>
     */
    public function toAttributes(Request $request): array
    {
        return [
            'sort_order' => $this->sort_order,
            'request_id' => $this->request_id,
        ];
    }
}

But in my response I have no included section rather I have

array:2 [
  "message" => "Get flow."
  "data" => array:4 [
    "id" => "01hx1c998n7asa2zc707ea0f7b"
    "type" => "flows"
    "attributes" => array:4 [
      "name" => "maiores"
      "description" => "Delectus distinctio corporis et odit quia quas dolor. Iste qui non libero et et expedita. Qui et dolorum perspiciatis et sed soluta."
      "num_requests" => 683068
      "num_views" => 434863
    ]
    "relationships" => array:1 [
      "items" => array:1 [
        "data" => array:5 [
          0 => array:2 [
            "type" => "flowItems"
            "id" => "01hx1c99av13hcag94hr0wwdnb"
          ]
          1 => array:2 [
            "type" => "flowItems"
            "id" => "01hx1c99av13hcag94hr0wwdnc"
          ]
          2 => array:2 [
            "type" => "flowItems"
            "id" => "01hx1c99awfs77jg58tj52whdv"
          ]
          3 => array:2 [
            "type" => "flowItems"
            "id" => "01hx1c99awfs77jg58tj52whdw"
          ]
          4 => array:2 [
            "type" => "flowItems"
            "id" => "01hx1c99awfs77jg58tj52whdx"
          ]
        ]
      ]
    ]
  ]
]

In the docs link there was a included section with the full data from the relationships section. Please can you help me point out what I may be doing wrong?

timacdonald commented 6 months ago

Hey there,

You need to return the JsonResource directly, just as you would with Laravel's JSON resource objects.

I can see you are wrapping it in an OK object. This is likely the issue.