Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to do nested eager loading using #[ResponseFromApiResource] #786

Open
1 task done
issakujitsuk opened this issue Jan 15, 2024 · 2 comments
Open
1 task done
Labels
bug Something isn't working

Comments

@issakujitsuk
Copy link

Scribe version

4.29.0

PHP version

8.2.8

Framework

Laravel

Framework version

10.17.0

Scribe config

try_it_out.enabled => false
auth.enabled => true
auth.default => true
postman.enabled => false
openapi.enabled => false
last_updated => "Last updated: {date:Y-m-d}"
strategies.urlParameters => added Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromLumenAPI
strategies.headers => added Knuckles\Scribe\Extracting\Strategies\Headers\GetFromRouteRules: removed [
    'override',
    [
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
    ],
]
strategies.responses => added Knuckles\Scribe\Extracting\Strategies\Responses\ResponseCalls: removed [
    'Knuckles\\Scribe\\Extracting\\Strategies\\Responses\\ResponseCalls',
    [
        'only' => [
            'GET *',
        ],
    ],
]

What happened?

Thanks for Scribe.

I declared an attribute like this.

#[ResponseFromApiResource(
    BookResource::class, 
    Book::class,
    with: ["author.contacts"],
)]

Expected (at BookResource):

$this->resource->relationLoaded("author"); // true
$this->resource->author->relationLoaded("contacts"); // true

As is:

$this->resource->relationLoaded("author"); // 👍 true
$this->resource->author->relationLoaded("contacts"); // 🆖 false

I see a problem here.

return $factory->create()->load($relations)->refresh();

I am able to load nested relations as expected until $factory->create()->load($relations),
but the contacts relation is lost in refresh().

Sorry if my language is poor and rude.

Docs

@issakujitsuk issakujitsuk added bug Something isn't working triage labels Jan 15, 2024
@shalvah
Copy link
Contributor

shalvah commented Jan 18, 2024

Not to worry, your language isn't poor or rude.

First off, disclaimer: I don't have much experience with API resources, so I can't help much.
But here's the PR where we added refresh: #753
I don't fully understand, but it looks like there was a good reason. Maybe the refresh should be before the load?🤔

@shalvah shalvah removed the triage label Jan 18, 2024
@issakujitsuk
Copy link
Author

Hi, @shalvah. Thanks for your reply.

As you guessed, $factory->create()->refresh()->load($relations) worked fine for me 😃
The issues fixed in #753 may have been as follows.

Book table:

CREATE TABLE `books` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  `volume` tinyint unsigned NOT NULL DEFAULT '1',   -- ⚠ volume has default value
  `updated_at` datetime NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
)

BookFactory:

public function definition(): array
{
    return [
        "title" => fake()->sentence(),
        // ⚠ volume is undefined.
    ];
}

Creating a model from a factory:

$book = Book::factory()->create();
/*
= App\Models\Book {
    title: "Eveniet nihil temporibus aut.",
    updated_at: "2024-01-18 00:00:00",
    created_at: "2024-01-18 00:00:00",
    id: 1,
  }
*/
// ⚠ volume is missing

$book->refresh();
/*
= App\Models\Book {
    id: 1,
    title: "Eveniet nihil temporibus aut.",
    volume: 1,
    updated_at: "2024-01-18 00:00:00",
    created_at: "2024-01-18 00:00:00",
  }
*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants