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

Backed enum serialization #1617

Closed
theofidry opened this issue Oct 17, 2024 · 1 comment · Fixed by #1618
Closed

Backed enum serialization #1617

theofidry opened this issue Oct 17, 2024 · 1 comment · Fixed by #1618
Labels

Comments

@theofidry
Copy link
Contributor

From #1608, BackedEnum are resolved as follows:

if (is_a($value, \BackedEnum::class)) {
  return $value->value;
}

I find this serialization a bit strange. Indeed if you consider the following object:

class UrlType {
  const WEBSITE = 'website';
  const OTHER = 'other';
}

with the following GraphQL configuration (using OverblogGraphQLBundle):

UrlType:
    type: enum
    config:
        values:
            WEBSITE: { value: website }
            OTHER: { value: other }

And the following class:

class Url {
  function __construct(
    public string $type,
  ) {}
}

Then with the object new Url(type: UrlType::WEBSITE), the returned JSON will be something along the lines of:

{
  "url": {
    "type": "WEBSITE"
  }
}

Note how it is the GraphQL UrlType name that is used to return the value. If you submit type: WEBSITE in GraphQL, then the value serialized to PHP will be 'website'.

Now the issue: make UrlType into a backed string enum instead:

enum UrlType: string {
  case WEBSITE = 'website';
  case OTHER = 'other';
}

Then the JSON response will now be:

{
  "url": {
    "type": "website"
  }
}

I do not know what is correct from the GraphQL spec perspective, but surely having such different behaviour when transforming a pseudo-enum into a backed enum seems incorrect?

@spawnia
Copy link
Collaborator

spawnia commented Oct 17, 2024

Good catch, fixed with https://github.com/webonyx/graphql-php/releases/tag/v15.14.3. Please consider sponsoring.

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

Successfully merging a pull request may close this issue.

2 participants