• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.12
/src/GraphQl/Serializer/SerializerContextBuilder.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\GraphQl\Serializer;
15

16
use ApiPlatform\Metadata\GraphQl\Mutation;
17
use ApiPlatform\Metadata\GraphQl\Operation;
18
use ApiPlatform\Metadata\GraphQl\Subscription;
19
use GraphQL\Type\Definition\ResolveInfo;
20
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
21
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
22
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
23
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
24

25
/**
26
 * Builds the context used by the Symfony Serializer.
27
 *
28
 * @author Alan Poulain <contact@alanpoulain.eu>
29
 */
30
final class SerializerContextBuilder implements SerializerContextBuilderInterface
31
{
32
    public function __construct(private readonly ?NameConverterInterface $nameConverter)
33
    {
34
    }
647✔
35

36
    public function create(?string $resourceClass, Operation $operation, array $resolverContext, bool $normalization): array
37
    {
38
        $context = ['resource_class' => $resourceClass, 'operation_name' => $operation->getName(), 'graphql_operation_name' => $operation->getName()];
136✔
39

40
        if (isset($resolverContext['fields'])) {
136✔
UNCOV
41
            $context['no_resolver_data'] = true;
1✔
42
        }
43

44
        $context['operation'] = $operation;
136✔
45
        if ($operation->getInput()) {
136✔
UNCOV
46
            $context['input'] = $operation->getInput();
5✔
47
        }
48
        if ($operation->getOutput()) {
136✔
UNCOV
49
            $context['output'] = $operation->getOutput();
4✔
50
        }
51
        $context = $normalization ? array_merge($operation->getNormalizationContext() ?? [], $context) : array_merge($operation->getDenormalizationContext() ?? [], $context);
136✔
52

53
        if ($normalization) {
136✔
54
            $context['attributes'] = $this->fieldsToAttributes($resourceClass, $operation, $resolverContext, $context);
136✔
55
        }
56

57
        // to keep the cache computation smaller, we have "operation_name" and "iri" anyways
58
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'root_operation';
136✔
59
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'operation';
136✔
60
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'object';
136✔
61
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'data';
136✔
62
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'property_metadata';
136✔
63
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'circular_reference_limit_counters';
136✔
64
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'debug_trace_id';
136✔
65

66
        return $context;
136✔
67
    }
68

69
    /**
70
     * 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).
71
     */
72
    private function fieldsToAttributes(?string $resourceClass, Operation $operation, array $resolverContext, array $context): array
73
    {
74
        if (isset($resolverContext['fields'])) {
136✔
UNCOV
75
            $fields = $resolverContext['fields'];
1✔
76
        } else {
77
            /** @var ResolveInfo $info */
78
            $info = $resolverContext['info'];
135✔
79
            $fields = $info->getFieldSelection(\PHP_INT_MAX);
135✔
80
        }
81

82
        $attributes = $this->replaceIdKeys($fields['edges']['node'] ?? $fields['collection'] ?? $fields, $resourceClass, $context);
136✔
83

84
        if ($operation instanceof Subscription || $operation instanceof Mutation) {
136✔
85
            $wrapFieldName = lcfirst($operation->getShortName());
35✔
86

87
            return $attributes[$wrapFieldName] ?? [];
35✔
88
        }
89

90
        return $attributes;
107✔
91
    }
92

93
    private function replaceIdKeys(array $fields, ?string $resourceClass, array $context): array
94
    {
95
        $denormalizedFields = [];
136✔
96

97
        foreach ($fields as $key => $value) {
136✔
98
            if ('_id' === $key) {
136✔
UNCOV
99
                $denormalizedFields['id'] = $fields['_id'];
4✔
100

UNCOV
101
                continue;
4✔
102
            }
103

104
            $denormalizedFields[$this->denormalizePropertyName((string) $key, $resourceClass, $context)] = \is_array($value) ? $this->replaceIdKeys($value, $resourceClass, $context) : $value;
135✔
105
        }
106

107
        return $denormalizedFields;
136✔
108
    }
109

110
    private function denormalizePropertyName(string $property, ?string $resourceClass, array $context): string
111
    {
112
        if (null === $this->nameConverter) {
135✔
113
            return $property;
×
114
        }
115
        if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
135✔
116
            return $this->nameConverter->denormalize($property, $resourceClass, null, $context);
135✔
117
        }
118

119
        return $this->nameConverter->denormalize($property);
×
120
    }
121
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc