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

wp-graphql / wp-graphql / 14841806285

05 May 2025 04:57PM UTC coverage: 84.234% (-0.05%) from 84.287%
14841806285

push

github

actions-user
chore: update changeset for PR #3374

15900 of 18876 relevant lines covered (84.23%)

257.2 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
abstract class AbstractDataLoader {
17

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

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

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

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

49
        /**
50
         * AbstractDataLoader constructor.
51
         *
52
         * @param \WPGraphQL\AppContext $context
53
         */
54
        public function __construct( AppContext $context ) {
756✔
55
                $this->context = $context;
756✔
56
        }
57

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

73
                $database_id = absint( $database_id ) ? absint( $database_id ) : sanitize_text_field( $database_id );
360✔
74

75
                $this->buffer( [ $database_id ] );
360✔
76

77
                return new Deferred(
360✔
78
                        function () use ( $database_id ) {
360✔
79
                                return $this->load( $database_id );
360✔
80
                        }
360✔
81
                );
360✔
82
        }
83

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

107
                return $this;
520✔
108
        }
109

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

136
                return isset( $result[ $key ] ) ? $this->normalize_entry( $result[ $key ], $key ) : null;
519✔
137
        }
138

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

178
                return $this;
×
179
        }
180

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

197
                return $this;
×
198
        }
199

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

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

223
                return $this;
×
224
        }
225

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

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

263
                return $asArray ? iterator_to_array( $generator ) : $generator;
340✔
264
        }
265

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

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

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

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

324
                // Re-include previously-cached entries to result:
325
                $result += array_intersect_key( $this->cached, $this->buffer );
520✔
326

327
                $this->buffer = [];
520✔
328

329
                return $result;
520✔
330
        }
331

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

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

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

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

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

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

403
                if ( $model instanceof Model && 'private' === $model->get_visibility() ) {
516✔
404
                        return null;
20✔
405
                }
406

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

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

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

447
                if ( $value && ! isset( $this->cached[ $key ] ) ) {
520✔
448
                        $this->cached[ $key ] = $value;
×
449
                }
450

451
                return $value;
520✔
452
        }
453

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

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

493
        /**
494
         * Given array of keys, loads and returns a map consisting of keys from `keys` array and loaded
495
         * values
496
         *
497
         * Note that order of returned values must match exactly the order of keys.
498
         * If some entry is not available for given key - it must include null for the missing key.
499
         *
500
         * For example:
501
         * loadKeys(['a', 'b', 'c']) -> ['a' => 'value1, 'b' => null, 'c' => 'value3']
502
         *
503
         * @param int[]|string[] $keys
504
         *
505
         * @return array<int|string,mixed>
506
         */
507
        abstract protected function loadKeys( array $keys ); // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- @todo deprecate for `::load_keys()`
508
}
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

© 2026 Coveralls, Inc