Skip to content

Commit 2141440

Browse files
soyukateohhanhui
authored andcommitted
JsonLd: resolve @id when there is an output class
1 parent aad5e4b commit 2141440

File tree

5 files changed

+14
-1
lines changed

5 files changed

+14
-1
lines changed

features/main/input_output.feature

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ Feature: DTO input and output
4242
"@vocab": "http://example.com/docs.jsonld#",
4343
"hydra": "http://www.w3.org/ns/hydra/core#",
4444
"foo": {
45+
"@id": "/dummy_dto_customs/1/foo",
4546
"@type": "@id"
4647
},
4748
"bar": {
49+
"@id": "/dummy_dto_customs/1/bar",
4850
"@type": "@id"
4951
}
5052
},
5153
"@type": "CustomOutputDto",
54+
"@id": "/dummy_dto_customs/1",
5255
"foo": "test",
5356
"bar": 1
5457
}
@@ -69,10 +72,12 @@ Feature: DTO input and output
6972
"@type": "hydra:Collection",
7073
"hydra:member": [
7174
{
75+
"@id": "/dummy_dto_customs/1",
7276
"foo": "test",
7377
"bar": 1
7478
},
7579
{
80+
"@id": "/dummy_dto_customs/2",
7681
"foo": "test",
7782
"bar": 2
7883
}

src/GraphQl/Resolver/ResourceFieldResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function __invoke($source, $args, $context, ResolveInfo $info)
5050
}
5151

5252
if ('_id' === $info->fieldName && isset($source['id'])) {
53+
// if we set _id in the normalizer we take it
5354
$property = $source['_id'] ?? $source['id'];
5455
} elseif (\is_array($source) && isset($source[$info->fieldName])) {
5556
$property = $source[$info->fieldName];

src/GraphQl/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function normalize($object, $format = null, array $context = [])
6464
throw new UnexpectedValueException('Expected data to be an array');
6565
}
6666

67+
// we're handling the case where we have an Output class, we need the IRI from the origin resource
6768
if (($context['origin_resource'] ?? false) && isset($data['id'])) {
6869
$data['_id'] = $data['id'];
6970
$data['id'] = $this->iriConverter->getIriFromItem($context['origin_resource']);

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ public function normalize($object, $format = null, array $context = [])
7171

7272
$context['api_normalize'] = true;
7373
$context['resource_class'] = $this->getObjectClass($transformed);
74+
$context['origin_resource'] = $object;
7475

7576
return $this->serializer->normalize($transformed, $format, $context);
7677
}
7778

7879
if ($this->handleNonResource && $context['api_normalize'] ?? false) {
80+
if (($context['origin_resource'] ?? false)) {
81+
$context['output']['iri'] = $this->iriConverter->getIriFromItem($context['origin_resource']);
82+
}
83+
7984
$data = $this->createJsonLdContext($this->contextBuilder, $object, $context);
8085
$rawData = parent::normalize($object, $format, $context);
8186
if (!\is_array($rawData)) {

src/JsonLd/Serializer/JsonLdContextTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ private function addJsonLdContext(ContextBuilderInterface $contextBuilder, strin
4949

5050
private function createJsonLdContext(AnonymousContextBuilderInterface $contextBuilder, $object, array &$context, array $data = []): array
5151
{
52+
// We're in a collection, just add the IRI if available
5253
if (isset($context['jsonld_has_context'])) {
53-
return $data;
54+
return isset($context['output']['iri']) ? ['@id' => $context['output']['iri']] : $data;
5455
}
5556

5657
$context['jsonld_has_context'] = true;

0 commit comments

Comments
 (0)