@@ -47,18 +47,29 @@ public static function delegate(
47
47
Schema $ schema ,
48
48
DelegatorInterface $ delegator ,
49
49
ErrorsReporterInterface $ errorsReporter = null ,
50
- ): void {
50
+ ): Schema {
51
51
$ execution = new Execution ($ delegator , $ errorsReporter );
52
+ $ schemaConfig = $ schema ->getConfig ();
52
53
53
54
foreach (['query ' , 'mutation ' , 'subscription ' ] as $ operation ) {
54
- $ operationType = $ schema -> getOperationType ( $ operation) ;
55
+ $ operationConfig = $ schemaConfig ->{ $ operation} ;
55
56
56
- if (null === $ operationType ) {
57
- continue ;
57
+ if ($ operationConfig instanceof ObjectType ) {
58
+ $ execution -> prepareType ( $ operationConfig ) ;
58
59
}
59
60
60
- $ execution ->prepareType ($ operationType );
61
+ if (is_callable ($ operationConfig )) {
62
+ $ schemaConfig ->{$ operation } = function () use ($ operationConfig , $ execution ) {
63
+ $ type = $ operationConfig ();
64
+
65
+ $ execution ->prepareType ($ type );
66
+
67
+ return $ type ;
68
+ };
69
+ }
61
70
}
71
+
72
+ return $ schema ;
62
73
}
63
74
64
75
private function prepareType (Type $ type ): void
@@ -81,7 +92,7 @@ private function prepareType(Type $type): void
81
92
}
82
93
83
94
if ($ type instanceof AbstractType) {
84
- $ resolveType = fn (array $ value , mixed $ context , ResolveInfo $ info ) => $ this ->resolveAbstractType (
95
+ $ resolveType = fn (array $ value , mixed $ context , ResolveInfo $ info ) => $ this ->resolveAbstractType (
85
96
$ type ,
86
97
$ value ,
87
98
$ context ,
@@ -94,8 +105,12 @@ private function prepareType(Type $type): void
94
105
$ this ->preparedTypes [$ type ] = true ;
95
106
}
96
107
97
- private function resolveAbstractType (AbstractType $ abstractType , array $ value , mixed $ context , ResolveInfo $ info ): Type
98
- {
108
+ private function resolveAbstractType (
109
+ AbstractType $ abstractType ,
110
+ array $ value ,
111
+ mixed $ context ,
112
+ ResolveInfo $ info
113
+ ): Type {
99
114
/// __typename field should be existed in $value
100
115
/// because we have added it to delegated query
101
116
$ typename = $ value [Introspection::TYPE_NAME_FIELD_NAME ];
@@ -140,12 +155,16 @@ function (ExecutionResult $result) use ($info): mixed {
140
155
* @param array<string, mixed> $variables
141
156
* @return Promise
142
157
*/
143
- private function delegateToExecute (Schema $ schema , OperationDefinitionNode $ operation , array $ fragments , array $ variables ): Promise
144
- {
158
+ private function delegateToExecute (
159
+ Schema $ schema ,
160
+ OperationDefinitionNode $ operation ,
161
+ array $ fragments ,
162
+ array $ variables
163
+ ): Promise {
145
164
try {
146
165
/// We need to clone all fragments and operation to make sure it can not be mutated by delegator.
147
166
$ delegateOperation = $ operation ->cloneDeep ();
148
- $ delegateFragments = array_map (fn (FragmentDefinitionNode $ fragment ) => $ fragment ->cloneDeep (), $ fragments );
167
+ $ delegateFragments = array_map (fn (FragmentDefinitionNode $ fragment ) => $ fragment ->cloneDeep (), $ fragments );
149
168
150
169
/// Add typename for detecting object type of interface or union
151
170
SelectionSet::addTypename ($ delegateOperation ->getSelectionSet ());
0 commit comments