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

feat: BackedEnum resources #6309

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

GwendolenLynch
Copy link
Contributor

@GwendolenLynch GwendolenLynch commented Apr 13, 2024

Q A
Branch? main
Tickets Closes #6298, closes #6317
License MIT
Doc PR api-platform/docs#...

Intended for 3.4, so now it is just for review.

Depends on #6288 as I want to refactor BackedEnumPlainResourceTest test methods into those classes.

  • Add a provider for BackedEnums
  • Identifier is value by default, and implementing function getId(), etc, works too
  • Resource operations will default to only Get & GetCollection unless otherwise configured
#[ApiResource]
enum Status: int
{
    case DRAFT = 0;
    case PUBLISHED = 1;
}

GET /statuses

[
  {
    "name": "DRAFT",
    "value": 0
  },
  {
    "name": "PUBLISHED",
    "value": 1
  }
]

GET /statuses/1

{
  "name": "PUBLISHED",
  "value": 1
}

Copy link
Member

@soyuka soyuka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would love to see json-ld supported, I need to add my commit to this at some point :)

src/State/Provider/BackedEnumProvider.php Outdated Show resolved Hide resolved
src/State/Provider/BackedEnumProvider.php Outdated Show resolved Hide resolved
tests/Functional/BackedEnumPlainResourceTest.php Outdated Show resolved Hide resolved
@GwendolenLynch GwendolenLynch force-pushed the feat/enum-resources branch 7 times, most recently from 8c96867 to 8acce05 Compare April 14, 2024 12:55
@@ -59,6 +59,9 @@ public function createLinksFromIdentifiers(Metadata $operation): array

$link = (new Link())->withFromClass($resourceClass)->withIdentifiers($identifiers);
$parameterName = $identifiers[0];
if ('value' === $parameterName && enum_exists($resourceClass)) {
$parameterName = 'id';
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this match what you had in mind @soyuka?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uri variable should be as followed:

uriVariables: ['id' => new Link(parameterName: 'id', identifiers: ['value'])]

in the metadata directly so that we don't need any condition here right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, but I'm confused again 😇 … I'm probably missing something obvious, but the Link is constructed directly above, and the parameterName is set at return: $link->withParameterName($parameterName) … So is your suggestion how that return array should be shaped? If so what about non-enums?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which raises another thing about the createLinksFromIdentifiers method … If the $identifier array is empty there is an early return (lines 56-58), and then there is the if (1 < \count($identifiers)) { (lines 66-69) test that looks like dead code, correct?

Scrap this one, I was reading the comparison backwards in my head … confusion reigns today 😟

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to check this into the details, will try to find some time!

@dunglas
Copy link
Member

dunglas commented Apr 15, 2024

I would use URIs in GraphQL too, as we do for existing IDs and relations. WDYT?

@GwendolenLynch
Copy link
Contributor Author

@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?

@gnito-org
Copy link

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.

@GwendolenLynch
Copy link
Contributor Author

GwendolenLynch commented Apr 18, 2024

Thank you @gnito-org, I can reproduce it here.

@GwendolenLynch GwendolenLynch force-pushed the feat/enum-resources branch 2 times, most recently from e99f735 to 3df9f0b Compare April 19, 2024 10:38
@GwendolenLynch GwendolenLynch force-pushed the feat/enum-resources branch 3 times, most recently from 37672db to b088d10 Compare April 24, 2024 07:16
@GwendolenLynch GwendolenLynch force-pushed the feat/enum-resources branch 3 times, most recently from e227f3c to a069f68 Compare May 2, 2024 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants