4
4
5
5
namespace TheCodingMachine \GraphQLite ;
6
6
7
+ use GraphQL \Type \Definition \ResolveInfo ;
8
+
7
9
use function array_key_exists ;
8
10
use function md5 ;
9
11
use function serialize ;
@@ -20,14 +22,27 @@ class PrefetchBuffer
20
22
private array $ results = [];
21
23
22
24
/** @param array<array-key, mixed> $arguments The input arguments passed from GraphQL to the field. */
23
- public function register (object $ object , array $ arguments ): void
24
- {
25
- $ this ->objects [$ this ->computeHash ($ arguments )][] = $ object ;
25
+ public function register (
26
+ object $ object ,
27
+ array $ arguments ,
28
+ ResolveInfo |null $ info = null ,
29
+ ): void {
30
+ $ this ->objects [$ this ->computeHash ($ arguments , $ info )][] = $ object ;
26
31
}
27
32
28
33
/** @param array<array-key, mixed> $arguments The input arguments passed from GraphQL to the field. */
29
- private function computeHash (array $ arguments ): string
30
- {
34
+ private function computeHash (
35
+ array $ arguments ,
36
+ ResolveInfo |null $ info ,
37
+ ): string {
38
+ if (
39
+ $ info instanceof ResolveInfo
40
+ && isset ($ info ->operation )
41
+ && $ info ->operation ->loc ?->source?->body !== null
42
+ ) {
43
+ return md5 (serialize ($ arguments ) . $ info ->operation ->loc ->source ->body );
44
+ }
45
+
31
46
return md5 (serialize ($ arguments ));
32
47
}
33
48
@@ -36,32 +51,43 @@ private function computeHash(array $arguments): string
36
51
*
37
52
* @return array<int, object>
38
53
*/
39
- public function getObjectsByArguments (array $ arguments ): array
40
- {
41
- return $ this ->objects [$ this ->computeHash ($ arguments )] ?? [];
54
+ public function getObjectsByArguments (
55
+ array $ arguments ,
56
+ ResolveInfo |null $ info = null ,
57
+ ): array {
58
+ return $ this ->objects [$ this ->computeHash ($ arguments , $ info )] ?? [];
42
59
}
43
60
44
61
/** @param array<array-key, mixed> $arguments The input arguments passed from GraphQL to the field. */
45
- public function purge (array $ arguments ): void
46
- {
47
- unset($ this ->objects [$ this ->computeHash ($ arguments )]);
62
+ public function purge (
63
+ array $ arguments ,
64
+ ResolveInfo |null $ info = null ,
65
+ ): void {
66
+ unset($ this ->objects [$ this ->computeHash ($ arguments , $ info )]);
48
67
}
49
68
50
69
/** @param array<array-key, mixed> $arguments The input arguments passed from GraphQL to the field. */
51
- public function storeResult (mixed $ result , array $ arguments ): void
52
- {
53
- $ this ->results [$ this ->computeHash ($ arguments )] = $ result ;
70
+ public function storeResult (
71
+ mixed $ result ,
72
+ array $ arguments ,
73
+ ResolveInfo |null $ info = null ,
74
+ ): void {
75
+ $ this ->results [$ this ->computeHash ($ arguments , $ info )] = $ result ;
54
76
}
55
77
56
78
/** @param array<array-key, mixed> $arguments The input arguments passed from GraphQL to the field. */
57
- public function hasResult (array $ arguments ): bool
58
- {
59
- return array_key_exists ($ this ->computeHash ($ arguments ), $ this ->results );
79
+ public function hasResult (
80
+ array $ arguments ,
81
+ ResolveInfo |null $ info = null ,
82
+ ): bool {
83
+ return array_key_exists ($ this ->computeHash ($ arguments , $ info ), $ this ->results );
60
84
}
61
85
62
86
/** @param array<array-key, mixed> $arguments The input arguments passed from GraphQL to the field. */
63
- public function getResult (array $ arguments ): mixed
64
- {
65
- return $ this ->results [$ this ->computeHash ($ arguments )];
87
+ public function getResult (
88
+ array $ arguments ,
89
+ ResolveInfo |null $ info = null ,
90
+ ): mixed {
91
+ return $ this ->results [$ this ->computeHash ($ arguments , $ info )];
66
92
}
67
93
}
0 commit comments