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

Enum Support for Morph Types #2543

Closed
skaesdorf opened this issue Apr 19, 2024 · 1 comment
Closed

Enum Support for Morph Types #2543

skaesdorf opened this issue Apr 19, 2024 · 1 comment
Labels
enhancement A feature or improvement

Comments

@skaesdorf
Copy link
Contributor

skaesdorf commented Apr 19, 2024

Hi, currently we are switching a larger project from BenSampo/laravel-enum to native php enums. Now I'm facing the Issue that many nested morph relations wont work anymore since we use Enums for the morph type declaration in our schema. These enums are backed in php by BackedEnums which map to the acutal class name.

enum LinkableType: string
{
    case LIVESTREAM = Livestream::class;
    case VOD        = Vod::class;
}
input LinkableMorphInput
{
    type: LinkableType!
    id: ID!
}

BenSampo/laravel-enums are stringable and resolve to their value when casted to a string. Because of that the NestedMorphTo ArgResolver works fine. Now, when using native enums, casts to string are no longer possible since native enums are not stringable. This results in an error in e.g. the NestedMorphTo ArgResolver:

//nuwave/lighthouse/src/Execution/Arguments/NestedMorphTo.php

$morphToModel = $this->relation->createModelByType(
    (string) $connectArgs->arguments['type']->value,
    //arguments['type']->value is a BackedEnum, this results in:
    //Object of class App\Enums\LinkableType could not be converted to string
);

Since Laravel already supports native Enums in many cases like QueryBuilder or Eloquent it seems consistent to me to support them in lighthouse's morph relations.

I would be happy to submit a PR if this is a desirable feature.

Cheers
Stefan

@spawnia spawnia added the enhancement A feature or improvement label Apr 21, 2024
@spawnia
Copy link
Collaborator

spawnia commented May 1, 2024

@spawnia spawnia closed this as completed May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature or improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants