12
12
use TheCodingMachine \GraphQLite \PrefetchBuffer ;
13
13
use TheCodingMachine \GraphQLite \QueryField ;
14
14
15
- use function array_key_exists ;
16
15
use function assert ;
17
16
18
17
/**
@@ -28,7 +27,6 @@ public function __construct(
28
27
private readonly string $ fieldName ,
29
28
private readonly mixed $ resolver ,
30
29
public readonly array $ parameters ,
31
- public readonly bool $ returnRequested = false ,
32
30
)
33
31
{
34
32
}
@@ -59,7 +57,10 @@ public function resolve(object|null $source, array $args, mixed $context, Resolv
59
57
$ this ->computePrefetch ($ args , $ context , $ info , $ prefetchBuffer );
60
58
}
61
59
62
- return $ prefetchBuffer ->getResult ($ source );
60
+ $ result = $ prefetchBuffer ->getResult ($ source );
61
+ // clear internal storage
62
+ $ prefetchBuffer ->purgeResult ($ source );
63
+ return $ result ;
63
64
});
64
65
}
65
66
@@ -71,22 +72,9 @@ private function computePrefetch(array $args, mixed $context, ResolveInfo $info,
71
72
$ toPassPrefetchArgs = QueryField::paramsToArguments ($ this ->fieldName , $ this ->parameters , null , $ args , $ context , $ info , $ this ->resolver );
72
73
73
74
$ resolvedValues = ($ this ->resolver )($ sources , ...$ toPassPrefetchArgs );
74
- if ($ this ->returnRequested ) {
75
- foreach ($ resolvedValues as $ key => $ resolvedValue ) {
76
- if (! array_key_exists ($ key , $ sources )) {
77
- throw new GraphQLRuntimeException (
78
- 'Called by Prefetch function should accept ' .
79
- 'Array<key> and return Array<value>, but the function did ' .
80
- 'not return an Array of the same length as the Array of keys. ' ,
81
- );
82
- }
83
- $ prefetchBuffer ->storeResult ($ sources [$ key ], $ resolvedValue );
84
- }
85
- } else {
86
- foreach ($ sources as $ source ) {
87
- // map results to each source to support old prefetch behavior
88
- $ prefetchBuffer ->storeResult ($ source , $ resolvedValues );
89
- }
75
+ foreach ($ sources as $ source ) {
76
+ // map results to each source to support old prefetch behavior
77
+ $ prefetchBuffer ->storeResult ($ source , $ resolvedValues );
90
78
}
91
79
}
92
80
0 commit comments