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 include relationships via MorphTo->MorphTo relationship chain #303

Open
Repsys opened this issue Jan 13, 2025 · 1 comment
Open

Comments

@Repsys
Copy link

Repsys commented Jan 13, 2025

I have four schemas:

class LikeSchema extends Schema
{
    protected int $maxDepth = 4;

    public function fields(): array
    {
        return [
            ID::make(),
            Str::make('userId'),
            MorphTo::make('likeable', 'likeable')
                ->types('contents', 'products'),
        ];
    }
}
class ContentSchema extends Schema
{
    public function fields(): array
    {
        return [
            ID::make(),
            Str::make('title'),
            HasMany::make('likes')->type('likes'),
            MorphTo::make('contentable', 'contentable')
                ->types('articles', 'videos'),
        ];
    }
}
class ArticleSchema extends Schema
{
    public function fields(): array
    {
        return [
            ID::make(),
            Str::make('body'),
            HasOne::make('content', 'content'),
        ];
    }
}
class VideoSchema extends Schema
{
    public function fields(): array
    {
        return [
            ID::make(),
            HasOne::make('content', 'content'),
            HasMany::make('media'),
        ];
    }
}

I need to get the videos.media relationship via the likes resource, so I'm trying to do the following query:
GET localhost/api/v1/likes?include=likeable.contentable.media
but I get the error “Include path likeable.contentable.media is not allowed.”.

At the same time, if I want to get the videos.media relation via the contents resource, I make the following request:
GET localhost/api/v1/contents?include=contentable.media and it works as it should!

At first, I thought it was the depth of the relationship, but changing the $maxDepth property in LikeSchema didn't work.
Moreover, if you try to replace MorphTo relationships (likes.likeable, contents.contentable) with any other ones, for example BelongsTo, everything works correctly.

The problem is in the MorphTo relation; After the second MorphTo relation in the include chain, the next relations in the chain do not want to be loaded.

@efmjackhardcastle
Copy link

efmjackhardcastle commented Feb 10, 2025

I don't think this is anything specific to the schemas and/or relationships themselves rather a limitation of the JsonApiRule::includePaths(), on the collection query class.

I don't have this issue when using the following workaround:

                JsonApiRule::includePaths(
                    request()->has('include')
                        ? explode(',', request()->get('include'))
                        : []
                ),

Can you post yours (if you have this rule set)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants