Open onethumb opened 6 months ago
Adding an ApiPlatform\Graphql\Serializer\ItemNormalizer::denormalize()
method like this appears to resolve the issue:
public function denormalize(mixed $data, string $class, string $format = null, array $context = []): mixed
{
if (isset($data['_id'])) {
$data['id'] = $data['_id'];
unset($data['_id']);
}
return parent::denormalize($data, $class, $format, $context);
}
...but I don't know if that's the preferred way to solve this issue in this project. If it is, I'm happy to submit a PR. If not, happy to rework to better match this project's expectations. 👍
I'm really not up to date with the graphql specification your denormalizer is a good solution but idk if we should put this into api platform
Why wouldn't we include it? api-platform/core
already includes this pattern (using _id
for the non-ID
scalar representation of id
), it's just broken in this case. AFAICT, this is a bug, not a feature.
In older versions of API Platform (pre 3.*), a field of 'id' was automatically added to graphql mutations not named 'create'.
This was removed via https://github.com/api-platform/core/pull/5359.
You could try adding it back via 'extraArgs' and see if that addresses your error?
You could try adding it back via 'extraArgs' and see if that addresses your error?
That's nice, also you should be able to define that by default for every resource using the defaults
configuration option.
API Platform version(s) affected: v3.2.10
Description
When using a simple
Entity
flagged asAPIResource
with a constructor (rather than just public properties),GraphQL
correctly documents the input requirements with_id
as the input identifier, but doesn't correctly normalize it back toid
whenMutations
are sent, such ascreate
.This is with a fresh clone of the api-platform template repository, and I also tried a bootstrapped api-platform/core installation, both on
v3.2.10
.The error is:
I stepped through the normalizers a bit to see if it was something obvious, but it wasn't (at least to me). I can see
_id
getting normalized toid
in some of the contexts and operations, but not all of them, so my hunch is it's just missing one somewhere.The other supported API languages I've tried,
OpenAPI
,JSON-LD
, andHAL
all seem to work fine with this approach.How to reproduce
The
Entity
:The
GraphQL
documentation forcreateGreetingInput
inGraphiQL
:The
GraphQL
Mutation
:The JSON error:
Possible Solution
I'd call these workarounds, rather than solutions, but they both "work fine" (but don't match the patterns I'm trying to retrofit
api-platform
onto, so don't work for us).First, using public properties without a constructor properly maps
_id
toid
:As does marking up the
GraphQL
Mutation
forcreate
directly to useID!
(it does not work if I useInt!
for the type):with
Both workarounds are messy for our projects.
Additional Context
Nothing else comes to mind, but of course, I'm happy to answer questions and try nearly anything. :)