feat: BackedEnum resources#6309
Conversation
soyuka
left a comment
There was a problem hiding this comment.
would love to see json-ld supported, I need to add my commit to this at some point :)
8c96867 to
8acce05
Compare
|
I would use URIs in GraphQL too, as we do for existing IDs and relations. WDYT? |
|
@dunglas it must be my week for getting confused. 🌞 I'm very happy to make required changes, but could you be so kind as to give a bit more context, please? |
|
Please look at how I configure a backed enum as an API resource so that each instance has an IRI just like any other API resource. https://github.com/gnito-org/problem-replicator-api-platform-enum-example This is important for machine discovery of APIs and for consistency. |
|
Thank you @gnito-org, I can reproduce it here. |
37672db to
b088d10
Compare
e227f3c to
a069f68
Compare
|
@GwendolenLynch I merged #6288 could you rebase I this should fix some of the failing tests right? |
a069f68 to
a232425
Compare
|
I'll continue working on this tomorrow, hopefully merging it! |
84612ab to
cc755bf
Compare
cc755bf to
f5e7ad2
Compare
| <service id="api_platform.state_provider.backed_enum" class="ApiPlatform\State\Provider\BackedEnumProvider"> | ||
| <tag name="api_platform.state_provider" key="ApiPlatform\State\Provider\BackedEnumProvidevr" /> | ||
| <tag name="api_platform.state_provider" key="api_platform.state_provider.backed_enum" /> | ||
| </service> |
There was a problem hiding this comment.
For the record @GwendolenLynch instead of decorating the provider chain, I choose to declare this as an operation provider instead. This will be called inside the ReadProvider:
core/src/State/Provider/ReadProvider.php
Lines 75 to 79 in ef4812a
It's easier for userland to decorate our enum provider (or any item provider) instead of hooking into the main decoration chain.
| use ApiPlatform\Metadata\Operation; | ||
| use Symfony\Component\Serializer\Attribute\Groups; | ||
|
|
||
| trait BackedEnumStringTrait |
There was a problem hiding this comment.
For further readers, this was duplicated from the BackedEnumTrait as graphql "guesses" the type from an union:
core/src/GraphQl/Type/FieldsBuilder.php
Lines 232 to 239 in ef4812a
I'm not a huge fan, ideally we should provide a fix at some point there, even though when representing resources one should avoid scalar union types in my opinion.
|
Thanks @GwendolenLynch! |
* fix(metadata): Only add GET operations for enums when ApiResource doesn't specify operations * feat(state): backed enum provider * fix(metadata): enum resource identifier default to value * fix(metadata): get method metadata for BackedEnums * test: resource with enum properties schema * what I would like * test: backed enums --------- Co-authored-by: soyuka <soyuka@users.noreply.github.com>
* fix(metadata): Only add GET operations for enums when ApiResource doesn't specify operations * feat(state): backed enum provider * fix(metadata): enum resource identifier default to value * fix(metadata): get method metadata for BackedEnums * test: resource with enum properties schema * what I would like * test: backed enums --------- Co-authored-by: soyuka <soyuka@users.noreply.github.com>
| } | ||
|
|
||
| $newGraphQlOperations = []; | ||
| foreach ($resourceMetadata->getGraphQlOperations() as $operationName => $operation) { |
There was a problem hiding this comment.
This does not work if there are no GraphQlOperations
|
Hi, @soyuka could you please send me a link to the docs please? (we tried to use it, but no provider registered message is comming up) |
Intended for 3.4, so now it is just for review.
Depends on #6288 as I want to refactor
BackedEnumPlainResourceTesttest methods into those classes.BackedEnumsvalueby default, and implementingfunction getId(), etc, works tooGet&GetCollectionunless otherwise configuredGET /statuses[ { "name": "DRAFT", "value": 0 }, { "name": "PUBLISHED", "value": 1 } ]GET /statuses/1{ "name": "PUBLISHED", "value": 1 }