/
SerializerContextBuilder.php
124 lines (98 loc) · 4.45 KB
/
SerializerContextBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Core\GraphQl\Serializer;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use GraphQL\Type\Definition\ResolveInfo;
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
/**
* Builds the context used by the Symfony Serializer.
*
* @experimental
*
* @author Alan Poulain <contact@alanpoulain.eu>
*/
final class SerializerContextBuilder implements SerializerContextBuilderInterface
{
private $resourceMetadataFactory;
private $nameConverter;
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ?NameConverterInterface $nameConverter)
{
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->nameConverter = $nameConverter;
}
public function create(?string $resourceClass, string $operationName, array $resolverContext, bool $normalization): array
{
$resourceMetadata = $resourceClass ? $this->resourceMetadataFactory->create($resourceClass) : null;
$context = [
'resource_class' => $resourceClass,
'graphql_operation_name' => $operationName,
];
if (isset($resolverContext['fields'])) {
$context['no_resolver_data'] = true;
}
if ($resourceMetadata) {
$context['input'] = $resourceMetadata->getGraphqlAttribute($operationName, 'input', null, true);
$context['output'] = $resourceMetadata->getGraphqlAttribute($operationName, 'output', null, true);
$key = $normalization ? 'normalization_context' : 'denormalization_context';
$context = array_merge($resourceMetadata->getGraphqlAttribute($operationName, $key, [], true), $context);
}
if ($normalization) {
$context['attributes'] = $this->fieldsToAttributes($resourceClass, $resourceMetadata, $resolverContext, $context);
}
return $context;
}
/**
* Retrieves fields, recursively replaces the "_id" key (the raw id) by "id" (the name of the property expected by the Serializer) and flattens edge and node structures (pagination).
*/
private function fieldsToAttributes(?string $resourceClass, ?ResourceMetadata $resourceMetadata, array $resolverContext, array $context): array
{
if (isset($resolverContext['fields'])) {
$fields = $resolverContext['fields'];
} else {
/** @var ResolveInfo $info */
$info = $resolverContext['info'];
$fields = $info->getFieldSelection(PHP_INT_MAX);
}
$attributes = $this->replaceIdKeys($fields['edges']['node'] ?? $fields, $resourceClass, $context);
if ($resolverContext['is_mutation'] || $resolverContext['is_subscription']) {
if (!$resourceMetadata) {
throw new \LogicException('ResourceMetadata should always exist for a mutation or a subscription.');
}
$wrapFieldName = lcfirst($resourceMetadata->getShortName());
return $attributes[$wrapFieldName] ?? [];
}
return $attributes;
}
private function replaceIdKeys(array $fields, ?string $resourceClass, array $context): array
{
$denormalizedFields = [];
foreach ($fields as $key => $value) {
if ('_id' === $key) {
$denormalizedFields['id'] = $fields['_id'];
continue;
}
$denormalizedFields[$this->denormalizePropertyName((string) $key, $resourceClass, $context)] = \is_array($fields[$key]) ? $this->replaceIdKeys($fields[$key], $resourceClass, $context) : $value;
}
return $denormalizedFields;
}
private function denormalizePropertyName(string $property, ?string $resourceClass, array $context): string
{
if (null === $this->nameConverter) {
return $property;
}
if ($this->nameConverter instanceof AdvancedNameConverterInterface) {
return $this->nameConverter->denormalize($property, $resourceClass, null, $context);
}
return $this->nameConverter->denormalize($property);
}
}