44
55namespace TheCodingMachine \GraphQLite ;
66
7+ use GraphQL \Type \Definition \ResolveInfo ;
8+
79use function array_key_exists ;
810use function md5 ;
911use function serialize ;
@@ -20,14 +22,27 @@ class PrefetchBuffer
2022 private array $ results = [];
2123
2224 /** @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 ;
2631 }
2732
2833 /** @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+
3146 return md5 (serialize ($ arguments ));
3247 }
3348
@@ -36,32 +51,43 @@ private function computeHash(array $arguments): string
3651 *
3752 * @return array<int, object>
3853 */
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 )] ?? [];
4259 }
4360
4461 /** @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 )]);
4867 }
4968
5069 /** @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 ;
5476 }
5577
5678 /** @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 );
6084 }
6185
6286 /** @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 )];
6692 }
6793}
0 commit comments