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

Serializes \UnitEnum and \BackedEnum in GraphQL\Type\Definition\EnumType when resolving return values from a Query or Mutation #1608

Merged
merged 12 commits into from
Sep 10, 2024

Conversation

Dahl99
Copy link
Contributor

@Dahl99 Dahl99 commented Sep 4, 2024

Since PHP has had native enums since version 8.1, there is an annoyance of not being able to return a native enum as a result from a query or mutation. If this is the case, it would have to be handled to return a string either by doing MyEnum::A->name or MyEnum::A->value depending on if it's a backed or unit enum.

This is also a bit of a nuisance if one uses DTOs for returning the data, as the type would have to be a string instead of being an enum which mirrors the enum in the GraphQL schema.

This pull request should address these issues by serializing native PHP enums if they were to be returned from a query or mutation.

…PhpEnumType` resolving return values from a Query or Mutation
@Dahl99 Dahl99 changed the title Serializes \UnitEnum and \BackedEnum in GraphQL\Type\Definition\PhpEnumType resolving return values from a Query or Mutation Serializes \UnitEnum and \BackedEnum in GraphQL\Type\Definition\PhpEnumType when resolving return values from a Query or Mutation Sep 4, 2024
@spawnia
Copy link
Collaborator

spawnia commented Sep 5, 2024

Have you seen #1604?

@Dahl99 Dahl99 changed the title Serializes \UnitEnum and \BackedEnum in GraphQL\Type\Definition\PhpEnumType when resolving return values from a Query or Mutation Serializes \UnitEnum and \BackedEnum in GraphQL\Type\Definition\EnumType when resolving return values from a Query or Mutation Sep 5, 2024
@Dahl99
Copy link
Contributor Author

Dahl99 commented Sep 5, 2024

Have you seen #1604?

Yes, but returning a native PHP enum from a resolver with the schema first approach still fails. I've updated the tests to use SDL for creating the schema and they both fail without the changes to GraphQL\Type\Definition\EnumType.

@Dahl99
Copy link
Contributor Author

Dahl99 commented Sep 10, 2024

@spawnia mind having another look at this pull request?

@spawnia spawnia merged commit 9baaf42 into webonyx:master Sep 10, 2024
15 checks passed
@spawnia
Copy link
Collaborator

spawnia commented Sep 10, 2024

Released with https://github.com/webonyx/graphql-php/releases/tag/v15.14.0. Please consider sponsoring.

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

Successfully merging this pull request may close these issues.

2 participants