• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 15710056976

17 Jun 2025 02:27PM UTC coverage: 84.17% (-0.1%) from 84.287%
15710056976

push

github

actions-user
release: merge develop into master for v2.3.3

15925 of 18920 relevant lines covered (84.17%)

258.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

54.61
/src/Data/Loader/AbstractDataLoader.php
1
<?php
2

3
namespace WPGraphQL\Data\Loader;
4

5
use Exception;
6
use GraphQL\Deferred;
7
use GraphQL\Utils\Utils;
8
use WPGraphQL\AppContext;
9
use WPGraphQL\Model\Model;
10

11
/**
12
 * Class AbstractDataLoader
13
 *
14
 * @package WPGraphQL\Data\Loader
15
 *
16
 * @todo Replace this type with a generic.
17
 * @phpstan-type TModel \WPGraphQL\Model\Model<mixed>
18
 */
19
abstract class AbstractDataLoader {
20

21
        /**
22
         * Whether the loader should cache results or not. In some cases the loader may be used to just
23
         * get content but not bother with caching it.
24
         *
25
         * Default: true
26
         *
27
         * @var bool
28
         */
29
        private $shouldCache = true;
30

31
        /**
32
         * This stores an array of items that have already been loaded
33
         *
34
         * @var array<int|string,mixed>
35
         */
36
        private $cached = [];
37

38
        /**
39
         * This stores an array of IDs that need to be loaded
40
         *
41
         * @var array<int|string,int|string>
42
         */
43
        private $buffer = [];
44

45
        /**
46
         * This stores a reference to the AppContext for the loader to make use of
47
         *
48
         * @var \WPGraphQL\AppContext
49
         */
50
        protected $context;
51

52
        /**
53
         * AbstractDataLoader constructor.
54
         *
55
         * @param \WPGraphQL\AppContext $context
56
         */
57
        public function __construct( AppContext $context ) {
555✔
58
                $this->context = $context;
555✔
59
        }
60

61
        /**
62
         * Given a Database ID, the particular loader will buffer it and resolve it deferred.
63
         *
64
         * @param mixed|int|string $database_id The database ID for a particular loader to load an object
65
         *
66
         * @return \GraphQL\Deferred|null
67
         * @throws \Exception
68
         *
69
         * @phpstan-return ($database_id is int|string ? \GraphQL\Deferred : null)
70
         */
71
        public function load_deferred( $database_id ) {
360✔
72
                if ( empty( $database_id ) ) {
360✔
73
                        return null;
×
74
                }
75

76
                $database_id = absint( $database_id ) ? absint( $database_id ) : sanitize_text_field( $database_id );
360✔
77

78
                $this->buffer( [ $database_id ] );
360✔
79

80
                return new Deferred(
360✔
81
                        function () use ( $database_id ) {
360✔
82
                                return $this->load( $database_id );
360✔
83
                        }
360✔
84
                );
360✔
85
        }
86

87
        /**
88
         * Add keys to buffer to be loaded in single batch later.
89
         *
90
         * @param int[]|string[] $keys The keys of the objects to buffer
91
         *
92
         * @return $this
93
         * @throws \Exception
94
         */
95
        public function buffer( array $keys ) {
520✔
96
                foreach ( $keys as $index => $key ) {
520✔
97
                        $key = $this->key_to_scalar( $key );
520✔
98
                        if ( ! is_scalar( $key ) ) {
520✔
99
                                throw new Exception(
×
100
                                        static::class . '::buffer expects all keys to be scalars, but key ' .
×
101
                                        'at position ' . esc_html( $index ) . ' is ' . esc_html(
×
102
                                                Utils::printSafe( $keys ) . '. ' .
×
103
                                                $this->get_scalar_key_hint( $key )
×
104
                                        )
×
105
                                );
×
106
                        }
107
                        $this->buffer[ $key ] = 1;
520✔
108
                }
109

110
                return $this;
520✔
111
        }
112

113
        /**
114
         * Loads a key and returns value represented by this key.
115
         * Internally this method will load all currently buffered items and cache them locally.
116
         *
117
         * @param int|string|mixed $key
118
         *
119
         * @return ?TModel
120
         * @throws \Exception
121
         */
122
        public function load( $key ) {
519✔
123
                $key = $this->key_to_scalar( $key );
519✔
124
                if ( ! is_scalar( $key ) ) {
519✔
125
                        throw new Exception(
×
126
                                static::class . '::load expects key to be scalar, but got ' . esc_html(
×
127
                                        Utils::printSafe( $key ) .
×
128
                                        $this->get_scalar_key_hint( $key )
×
129
                                )
×
130
                        );
×
131
                }
132
                if ( ! $this->shouldCache ) {
519✔
133
                        $this->buffer = [];
×
134
                }
135
                $keys = [ $key ];
519✔
136
                $this->buffer( $keys );
519✔
137
                $result = $this->load_buffered();
519✔
138

139
                return isset( $result[ $key ] ) ? $this->normalize_entry( $result[ $key ], $key ) : null;
519✔
140
        }
141

142
        /**
143
         * Adds the provided key and value to the cache. If the key already exists, no
144
         * change is made. Returns itself for method chaining.
145
         *
146
         * @param mixed $key
147
         * @param mixed $value
148
         *
149
         * @return $this
150
         * @throws \Exception
151
         */
152
        public function prime( $key, $value ) {
×
153
                $key = $this->key_to_scalar( $key );
×
154
                if ( ! is_scalar( $key ) ) {
×
155
                        throw new Exception(
×
156
                                static::class . '::prime is expecting scalar $key, but got ' . esc_html(
×
157
                                        Utils::printSafe( $key )
×
158
                                        . $this->get_scalar_key_hint( $key )
×
159
                                )
×
160
                        );
×
161
                }
162
                if ( null === $value ) {
×
163
                        throw new Exception(
×
164
                                static::class . '::prime is expecting non-null $value, but got null. Double-check for null or ' .
×
165
                                ' use `clear` if you want to clear the cache'
×
166
                        );
×
167
                }
168
                if ( ! $this->get_cached( $key ) ) {
×
169
                        /**
170
                         * For adding third-party caching support.
171
                         * Use this filter to store the queried value in a cache.
172
                         *
173
                         * @param mixed  $value         Queried object.
174
                         * @param mixed  $key           Object key.
175
                         * @param string $loader_class  Loader classname. Use as a means of identified the loader.
176
                         * @param mixed  $loader        Loader instance.
177
                         */
178
                        $this->set_cached( $key, $value );
×
179
                }
180

181
                return $this;
×
182
        }
183

184
        /**
185
         * Clears the value at `key` from the cache, if it exists. Returns itself for
186
         * method chaining.
187
         *
188
         * @param int[]|string[] $keys
189
         *
190
         * @return $this
191
         */
192
        public function clear( array $keys ) {
×
193
                foreach ( $keys as $key ) {
×
194
                        $key = $this->key_to_scalar( $key );
×
195
                        if ( isset( $this->cached[ $key ] ) ) {
×
196
                                unset( $this->cached[ $key ] );
×
197
                        }
198
                }
199

200
                return $this;
×
201
        }
202

203
        /**
204
         * Clears the entire cache. To be used when some event results in unknown
205
         * invalidations across this particular `DataLoader`. Returns itself for
206
         * method chaining.
207
         *
208
         * @return \WPGraphQL\Data\Loader\AbstractDataLoader
209
         * @deprecated in favor of clear_all
210
         */
211
        public function clearAll() {
×
212
                _deprecated_function( __METHOD__, '0.8.4', static::class . '::clear_all()' );
×
213
                return $this->clear_all();
×
214
        }
215

216
        /**
217
         * Clears the entire cache. To be used when some event results in unknown
218
         * invalidations across this particular `DataLoader`. Returns itself for
219
         * method chaining.
220
         *
221
         * @return \WPGraphQL\Data\Loader\AbstractDataLoader
222
         */
223
        public function clear_all() {
×
224
                $this->cached = [];
×
225

226
                return $this;
×
227
        }
228

229
        /**
230
         * Loads multiple keys. Returns generator where each entry directly corresponds to entry in
231
         * $keys. If second argument $asArray is set to true, returns array instead of generator
232
         *
233
         * @param int[]|string[] $keys
234
         * @param bool           $asArray
235
         *
236
         * @return \Generator|array<int|string,mixed>
237
         * @throws \Exception
238
         *
239
         * @deprecated Use load_many instead
240
         */
241
        public function loadMany( array $keys, $asArray = false ) {
×
242
                _deprecated_function( __METHOD__, '0.8.4', static::class . '::load_many()' );
×
243
                return $this->load_many( $keys, $asArray );
×
244
        }
245

246
        /**
247
         * Loads multiple keys. Returns generator where each entry directly corresponds to entry in
248
         * $keys. If second argument $asArray is set to true, returns array instead of generator
249
         *
250
         * @param int[]|string[] $keys
251
         * @param bool           $asArray
252
         *
253
         * @return \Generator|array<int|string,mixed>
254
         * @throws \Exception
255
         */
256
        public function load_many( array $keys, $asArray = false ) {
340✔
257
                if ( empty( $keys ) ) {
340✔
258
                        return [];
×
259
                }
260
                if ( ! $this->shouldCache ) {
340✔
261
                        $this->buffer = [];
×
262
                }
263
                $this->buffer( $keys );
340✔
264
                $generator = $this->generate_many( $keys, $this->load_buffered() );
340✔
265

266
                return $asArray ? iterator_to_array( $generator ) : $generator;
340✔
267
        }
268

269
        /**
270
         * Given an array of keys, this yields the object from the cached results
271
         *
272
         * @param int[]|string[]          $keys   The keys to generate results for
273
         * @param array<int|string,mixed> $result The results for all keys
274
         *
275
         * @return \Generator
276
         */
277
        private function generate_many( array $keys, array $result ) {
1✔
278
                foreach ( $keys as $key ) {
1✔
279
                        $key = $this->key_to_scalar( $key );
1✔
280
                        yield isset( $result[ $key ] ) ? $this->normalize_entry( $result[ $key ], $key ) : null;
1✔
281
                }
282
        }
283

284
        /**
285
         * This checks to see if any items are in the buffer, and if there are this
286
         * executes the loaders `loadKeys` method to load the items and adds them
287
         * to the cache if necessary
288
         *
289
         * @return array<int|string,mixed>
290
         * @throws \Exception
291
         */
292
        private function load_buffered(): array {
520✔
293
                // Do not load previously-cached entries:
294
                $keysToLoad = [];
520✔
295
                foreach ( $this->buffer as $key => $unused ) {
520✔
296
                        if ( ! $this->get_cached( $key ) ) {
520✔
297
                                $keysToLoad[] = $key;
520✔
298
                        }
299
                }
300

301
                $result = [];
520✔
302
                if ( ! empty( $keysToLoad ) ) {
520✔
303
                        try {
304
                                $loaded = $this->loadKeys( $keysToLoad );
520✔
305
                        } catch ( \Throwable $e ) {
×
306
                                throw new Exception(
×
307
                                        'Method ' . static::class . '::loadKeys is expected to return array, but it threw: ' .
×
308
                                        esc_html( $e->getMessage() ),
×
309
                                        0,
×
310
                                        $e // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
×
311
                                );
×
312
                        }
313

314
                        if ( ! is_array( $loaded ) ) {
520✔
315
                                throw new Exception(
×
316
                                        'Method ' . static::class . '::loadKeys is expected to return an array with keys ' .
×
317
                                        'but got: ' . esc_html( Utils::printSafe( $loaded ) )
×
318
                                );
×
319
                        }
320
                        if ( $this->shouldCache ) {
520✔
321
                                foreach ( $loaded as $key => $value ) {
520✔
322
                                        $this->set_cached( $key, $value );
520✔
323
                                }
324
                        }
325
                }
326

327
                // Re-include previously-cached entries to result:
328
                $result += array_intersect_key( $this->cached, $this->buffer );
520✔
329

330
                $this->buffer = [];
520✔
331

332
                return $result;
520✔
333
        }
334

335
        /**
336
         * This helps to ensure null values aren't being loaded by accident.
337
         *
338
         * @param mixed $key
339
         */
340
        private function get_scalar_key_hint( $key ): string {
×
341
                if ( null === $key ) {
×
342
                        return ' Make sure to add additional checks for null values.';
×
343
                } else {
344
                        return ' Try overriding ' . self::class . '::key_to_scalar if your keys are composite.';
×
345
                }
346
        }
347

348
        /**
349
         * For loaders that need to decode keys, this method can help with that.
350
         * For example, if we wanted to accept a list of RELAY style global IDs and pass them
351
         * to the loader, we could have the loader centrally decode the keys into their
352
         * integer values in the PostObjectLoader by overriding this method.
353
         *
354
         * @param int|string|mixed $key
355
         *
356
         * @return int|string
357
         */
358
        protected function key_to_scalar( $key ) {
520✔
359
                return $key;
520✔
360
        }
361

362
        /**
363
         * @param int|string|mixed $key
364
         *
365
         * @return int|string
366
         * @deprecated Use key_to_scalar instead
367
         */
368
        protected function keyToScalar( $key ) {
×
369
                _deprecated_function( __METHOD__, '0.8.4', static::class . '::key_to_scalar()' );
×
370
                return $this->key_to_scalar( $key );
×
371
        }
372

373
        /**
374
         * @param mixed $entry The entry loaded from the dataloader to be used to generate a Model
375
         * @param mixed $key   The Key used to identify the loaded entry
376
         *
377
         * @return TModel|null
378
         */
379
        protected function normalize_entry( $entry, $key ) {
516✔
380

381
                /**
382
                 * This filter allows the model generated by the DataLoader to be filtered.
383
                 *
384
                 * Returning anything other than null here will bypass the default model generation
385
                 * for an object.
386
                 *
387
                 * One example would be WooCommerce Products returning a custom Model for posts of post_type "product".
388
                 *
389
                 * @param null               $model                The filtered model to return. Default null
390
                 * @param mixed              $entry                The entry loaded from the dataloader to be used to generate a Model
391
                 * @param mixed              $key                  The Key used to identify the loaded entry
392
                 * @param \WPGraphQL\Data\Loader\AbstractDataLoader $abstract_data_loader The AbstractDataLoader instance
393
                 */
394
                $model         = null;
516✔
395
                $pre_get_model = apply_filters( 'graphql_dataloader_pre_get_model', $model, $entry, $key, $this );
516✔
396

397
                /**
398
                 * If a Model has been pre-loaded via filter, return it and skip the
399
                 */
400
                if ( ! empty( $pre_get_model ) ) {
516✔
401
                        $model = $pre_get_model;
×
402
                } else {
403
                        $model = $this->get_model( $entry, $key );
516✔
404
                }
405

406
                if ( $model instanceof Model && 'private' === $model->get_visibility() ) {
516✔
407
                        return null;
20✔
408
                }
409

410
                /**
411
                 * Filter the model before returning.
412
                 *
413
                 * @param mixed              $model  The Model to be returned by the loader
414
                 * @param mixed              $entry  The entry loaded by dataloader that was used to create the Model
415
                 * @param mixed              $key    The Key that was used to load the entry
416
                 * @param \WPGraphQL\Data\Loader\AbstractDataLoader $loader The AbstractDataLoader Instance
417
                 */
418
                return apply_filters( 'graphql_dataloader_get_model', $model, $entry, $key, $this );
511✔
419
        }
420

421
        /**
422
         * Returns a cached data object by key.
423
         *
424
         * @param int|string $key Key.
425
         *
426
         * @return mixed
427
         */
428
        protected function get_cached( $key ) {
520✔
429
                $value = null;
520✔
430
                if ( isset( $this->cached[ $key ] ) ) {
520✔
431
                        $value = $this->cached[ $key ];
355✔
432
                }
433

434
                /**
435
                 * Use this filter to retrieving cached data objects from third-party caching system.
436
                 *
437
                 * @param mixed       $value        Value to be cached.
438
                 * @param int|string  $key          Key identifying object.
439
                 * @param string      $loader_class Loader class name.
440
                 * @param mixed       $loader       Loader instance.
441
                 */
442
                $value = apply_filters(
520✔
443
                        'graphql_dataloader_get_cached',
520✔
444
                        $value,
520✔
445
                        $key,
520✔
446
                        static::class,
520✔
447
                        $this
520✔
448
                );
520✔
449

450
                if ( $value && ! isset( $this->cached[ $key ] ) ) {
520✔
451
                        $this->cached[ $key ] = $value;
×
452
                }
453

454
                return $value;
520✔
455
        }
456

457
        /**
458
         * Caches a data object by key.
459
         *
460
         * @param int|string $key    Key.
461
         * @param mixed      $value  Data object.
462
         *
463
         * @return void
464
         */
465
        protected function set_cached( $key, $value ) {
520✔
466
                /**
467
                 * Use this filter to store entry in a third-party caching system.
468
                 *
469
                 * @param mixed  $value         Value to be cached.
470
                 * @param mixed  $key           Key identifying object.
471
                 * @param string $loader_class  Loader class name.
472
                 * @param mixed  $loader        Loader instance.
473
                 */
474
                $this->cached[ $key ] = apply_filters(
520✔
475
                        'graphql_dataloader_set_cached',
520✔
476
                        $value,
520✔
477
                        $key,
520✔
478
                        static::class,
520✔
479
                        $this
520✔
480
                );
520✔
481
        }
482

483
        /**
484
         * If the loader needs to do any tweaks between getting raw data from the DB and caching,
485
         * this can be overridden by the specific loader and used for transformations, etc.
486
         *
487
         * @param mixed $entry The entry data to be used to generate a Model.
488
         * @param mixed $key   The Key to identify the entry by.
489
         *
490
         * @return ?TModel
491
         */
492
        protected function get_model( $entry, $key ) {
41✔
493
                return $entry;
41✔
494
        }
495

496
        /**
497
         * Given array of keys, loads and returns a map consisting of keys from `keys` array and loaded
498
         * values
499
         *
500
         * Note that order of returned values must match exactly the order of keys.
501
         * If some entry is not available for given key - it must include null for the missing key.
502
         *
503
         * For example:
504
         * loadKeys(['a', 'b', 'c']) -> ['a' => 'value1, 'b' => null, 'c' => 'value3']
505
         *
506
         * @param int[]|string[] $keys
507
         *
508
         * @return array<int|string,mixed>
509
         */
510
        abstract protected function loadKeys( array $keys ); // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- @todo deprecate for `::load_keys()`
511
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc