13
13
14
14
namespace ApiPlatform \GraphQl \Serializer ;
15
15
16
- use ApiPlatform \Exception \OperationNotFoundException ;
17
16
use ApiPlatform \Metadata \GraphQl \Operation ;
18
- use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
19
17
use GraphQL \Type \Definition \ResolveInfo ;
20
18
use Symfony \Component \Serializer \NameConverter \AdvancedNameConverterInterface ;
21
19
use Symfony \Component \Serializer \NameConverter \NameConverterInterface ;
27
25
*/
28
26
final class SerializerContextBuilder implements SerializerContextBuilderInterface
29
27
{
30
- private $ resourceMetadataCollectionFactory ;
31
28
private $ nameConverter ;
32
29
33
- public function __construct (ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory , ?NameConverterInterface $ nameConverter )
30
+ public function __construct (?NameConverterInterface $ nameConverter )
34
31
{
35
- $ this ->resourceMetadataCollectionFactory = $ resourceMetadataCollectionFactory ;
36
32
$ this ->nameConverter = $ nameConverter ;
37
33
}
38
34
39
- public function create (?string $ resourceClass , string $ operationName , array $ resolverContext , bool $ normalization ): array
35
+ public function create (?string $ resourceClass , Operation $ operation , array $ resolverContext , bool $ normalization ): array
40
36
{
41
- $ context = ['resource_class ' => $ resourceClass , 'operation_name ' => $ operationName , 'graphql_operation_name ' => $ operationName ];
42
- $ operation = null ;
43
-
44
- if ($ resourceClass ) {
45
- $ resourceMetadata = $ this ->resourceMetadataCollectionFactory ->create ($ resourceClass );
46
- try {
47
- $ operation = $ resourceMetadata ->getOperation ($ operationName );
48
- } catch (OperationNotFoundException $ e ) {
49
- // It's possible that the serialization context may not be tight to an existing operation
50
- try {
51
- $ context ['operation_name ' ] = $ resourceMetadata ->getOperation ()->getName ();
52
- } catch (OperationNotFoundException $ e ) {
53
- }
54
- }
55
- }
37
+ $ context = ['resource_class ' => $ resourceClass , 'operation_name ' => $ operation ->getName (), 'graphql_operation_name ' => $ operation ->getName ()];
56
38
57
39
if (isset ($ resolverContext ['fields ' ])) {
58
40
$ context ['no_resolver_data ' ] = true ;
59
41
}
60
42
61
- if ($ operation ) {
43
+ $ context ['operation ' ] = $ operation ;
44
+ if ($ operation ->getInput ()) {
62
45
$ context ['input ' ] = $ operation ->getInput ();
46
+ }
47
+ if ($ operation ->getOutput ()) {
63
48
$ context ['output ' ] = $ operation ->getOutput ();
64
- $ context = $ normalization ? array_merge ($ operation ->getNormalizationContext () ?? [], $ context ) : array_merge ($ operation ->getDenormalizationContext () ?? [], $ context );
65
49
}
50
+ $ context = $ normalization ? array_merge ($ operation ->getNormalizationContext () ?? [], $ context ) : array_merge ($ operation ->getDenormalizationContext () ?? [], $ context );
66
51
67
52
if ($ normalization ) {
68
- $ context ['attributes ' ] = $ this ->fieldsToAttributes ($ resourceClass , $ operation instanceof Operation ? $ operation : null , $ resolverContext , $ context );
53
+ $ context ['attributes ' ] = $ this ->fieldsToAttributes ($ resourceClass , $ operation , $ resolverContext , $ context );
69
54
}
70
55
71
56
return $ context ;
@@ -74,7 +59,7 @@ public function create(?string $resourceClass, string $operationName, array $res
74
59
/**
75
60
* 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).
76
61
*/
77
- private function fieldsToAttributes (?string $ resourceClass , ? Operation $ operation , array $ resolverContext , array $ context ): array
62
+ private function fieldsToAttributes (?string $ resourceClass , Operation $ operation , array $ resolverContext , array $ context ): array
78
63
{
79
64
if (isset ($ resolverContext ['fields ' ])) {
80
65
$ fields = $ resolverContext ['fields ' ];
@@ -87,10 +72,6 @@ private function fieldsToAttributes(?string $resourceClass, ?Operation $operatio
87
72
$ attributes = $ this ->replaceIdKeys ($ fields ['edges ' ]['node ' ] ?? $ fields ['collection ' ] ?? $ fields , $ resourceClass , $ context );
88
73
89
74
if ($ resolverContext ['is_mutation ' ] || $ resolverContext ['is_subscription ' ]) {
90
- if (!$ operation ) {
91
- throw new \LogicException ('An operation should always exist for a mutation or a subscription. ' );
92
- }
93
-
94
75
$ wrapFieldName = lcfirst ($ operation ->getShortName ());
95
76
96
77
return $ attributes [$ wrapFieldName ] ?? [];
0 commit comments