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

wp-graphql / wp-graphql / 17562196906

08 Sep 2025 07:44PM UTC coverage: 84.575% (+0.4%) from 84.17%
17562196906

push

github

web-flow
Merge pull request #3389 from wp-graphql/develop

release: next version 📦

238 of 308 new or added lines in 13 files covered. (77.27%)

6 existing lines in 6 files now uncovered.

15884 of 18781 relevant lines covered (84.57%)

261.69 hits per line

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

29.27
/src/AppContext.php
1
<?php
2

3
namespace WPGraphQL;
4

5
use GraphQL\Error\UserError;
6
use WPGraphQL\Data\Loader\CommentAuthorLoader;
7
use WPGraphQL\Data\Loader\CommentLoader;
8
use WPGraphQL\Data\Loader\EnqueuedScriptLoader;
9
use WPGraphQL\Data\Loader\EnqueuedStylesheetLoader;
10
use WPGraphQL\Data\Loader\PluginLoader;
11
use WPGraphQL\Data\Loader\PostObjectLoader;
12
use WPGraphQL\Data\Loader\PostTypeLoader;
13
use WPGraphQL\Data\Loader\TaxonomyLoader;
14
use WPGraphQL\Data\Loader\TermObjectLoader;
15
use WPGraphQL\Data\Loader\ThemeLoader;
16
use WPGraphQL\Data\Loader\UserLoader;
17
use WPGraphQL\Data\Loader\UserRoleLoader;
18
use WPGraphQL\Data\NodeResolver;
19

20
/**
21
 * Class AppContext
22
 * Creates an object that contains all of the context for the GraphQL query
23
 * This class gets instantiated and populated in the main WPGraphQL class.
24
 *
25
 * The context is passed to each resolver during execution.
26
 *
27
 * Resolvers have the ability to read and write to context to pass info to nested resolvers.
28
 *
29
 * @package WPGraphQL
30
 */
31
#[\AllowDynamicProperties]
32
class AppContext {
33
        /**
34
         * The default loaders for the AppContext.
35
         */
36
        private const DEFAULT_LOADERS = [
37
                'comment_author'      => CommentAuthorLoader::class,
38
                'comment'             => CommentLoader::class,
39
                'enqueued_script'     => EnqueuedScriptLoader::class,
40
                'enqueued_stylesheet' => EnqueuedStylesheetLoader::class,
41
                'plugin'              => PluginLoader::class,
42
                'nav_menu_item'       => PostObjectLoader::class,
43
                'post'                => PostObjectLoader::class,
44
                'post_type'           => PostTypeLoader::class,
45
                'taxonomy'            => TaxonomyLoader::class,
46
                'term'                => TermObjectLoader::class,
47
                'theme'               => ThemeLoader::class,
48
                'user'                => UserLoader::class,
49
                'user_role'           => UserRoleLoader::class,
50
        ];
51

52
        /**
53
         * Stores the class to use for the connection query.
54
         *
55
         * @var \WP_Query|null
56
         */
57
        public $connection_query_class = null;
58

59
        /**
60
         * Stores the url string for the current site
61
         *
62
         * @var string $root_url
63
         */
64
        public $root_url;
65

66
        /**
67
         * Stores the WP_User object of the current user
68
         *
69
         * @var \WP_User $viewer
70
         */
71
        public $viewer;
72

73
        /**
74
         * @var \WPGraphQL\Registry\TypeRegistry
75
         */
76
        public $type_registry;
77

78
        /**
79
         * Stores everything from the $_REQUEST global
80
         *
81
         * @var mixed $request
82
         */
83
        public $request;
84

85
        /**
86
         * Stores additional $config properties
87
         *
88
         * @var mixed $config
89
         */
90
        public $config;
91

92
        /**
93
         * Passes context about the current connection being resolved
94
         *
95
         * @todo These properties and methods are unused. We should consider deprecating/removing them.
96
         *
97
         * @var mixed|string|null
98
         */
99
        public $currentConnection = null;
100

101
        /**
102
         * Passes context about the current connection
103
         *
104
         * @todo These properties and methods are unused. We should consider deprecating/removing them.
105
         *
106
         * @var array<string,mixed>
107
         */
108
        public $connectionArgs = [];
109

110
        /**
111
         * Stores the loaders for the class
112
         *
113
         * @var array<string,\WPGraphQL\Data\Loader\AbstractDataLoader>
114
         *
115
         * phpcs:disable SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation, -- For phpstan type hinting
116
         *
117
         * @template T of key-of<self::DEFAULT_LOADERS>
118
         *
119
         * @phpstan-var array<T, new<self::DEFAULT_LOADERS[T]>>|array<string,\WPGraphQL\Data\Loader\AbstractDataLoader>
120
         *
121
         * phpcs:enable
122
         */
123
        public $loaders = [];
124

125
        /**
126
         * Instance of the NodeResolver class to resolve nodes by URI
127
         *
128
         * @var \WPGraphQL\Data\NodeResolver
129
         */
130
        public $node_resolver;
131

132
        /**
133
         * The loader classes, before they are instantiated.
134
         *
135
         * @var array<string,class-string<\WPGraphQL\Data\Loader\AbstractDataLoader>>
136
         */
137
        private $loader_classes = self::DEFAULT_LOADERS;
138

139
        /**
140
         * AppContext constructor.
141
         */
142
        public function __construct() {
781✔
143

144
                // Prime the loader classes (and their instances) for the AppContext.
145
                $this->prepare_data_loaders();
781✔
146

147
                /**
148
                 * This sets up the NodeResolver to allow nodes to be resolved by URI
149
                 */
150
                $this->node_resolver = new NodeResolver( $this );
781✔
151

152
                /**
153
                 * This filters the config for the AppContext.
154
                 *
155
                 * This can be used to store additional context config, which is available to resolvers
156
                 * throughout the resolution of a GraphQL request.
157
                 *
158
                 * @param mixed[] $config The config array of the AppContext object
159
                 */
160
                $this->config = apply_filters( 'graphql_app_context_config', $this->config );
781✔
161
        }
162

163
        /**
164
         * Prepares the data loaders for the AppContext.
165
         *
166
         * This method instantiates the loader classes and prepares them for use in the AppContext.
167
         * It also applies filters to allow customization of the loader classes.
168
         *
169
         * @uses graphql_data_loader_classes filter.
170
         * @uses graphql_data_loaders filter (deprecated).
171
         */
172
        private function prepare_data_loaders(): void {
781✔
173
                /**
174
                 * Filter to change the data loader classes.
175
                 *
176
                 * This allows for additional loaders to be added to the AppContext or replaced as needed.
177
                 *
178
                 * @param array<string,class-string<\WPGraphQL\Data\Loader\AbstractDataLoader>> $loader_classes The loader classes accessible in the AppContext
179
                 * @param \WPGraphQL\AppContext                                                $context        The AppContext
180
                 */
181
                $this->loader_classes = apply_filters( 'graphql_data_loader_classes', $this->loader_classes, $this );
781✔
182

183
                /**
184
                 * Prime the loaders if needed
185
                 *
186
                 * @todo Remove this when the loaders are instantiated on demand.
187
                 */
188
                if ( has_filter( 'graphql_data_loaders' ) ) {
781✔
189
                        $loaders = array_map(
×
190
                                function ( $loader_class ) {
×
191
                                        return new $loader_class( $this );
×
192
                                },
×
193
                                $this->loader_classes
×
194
                        );
×
195

196
                        /**
197
                         * @deprecated next-version in favor of graphql_data_loader_classes.
198
                         * @todo Remove in a future version.
199
                         *
200
                         * @param array<string,\WPGraphQL\Data\Loader\AbstractDataLoader> $loaders The loaders accessible in the AppContext
201
                         * @param \WPGraphQL\AppContext                                   $context The AppContext
202
                         */
203
                        $this->loaders = apply_filters_deprecated(
×
204
                                'graphql_data_loaders',
×
205
                                [ $loaders, $this ],
×
206
                                '2.3.2',
×
207
                                'graphql_data_loader_classes',
×
208
                                esc_html__( 'The graphql_data_loaders filter is deprecated and will be removed in a future version. Instead, use the graphql_data_loader_classes filter to add/change data loader classes before they are instantiated.', 'wp-graphql' ),
×
209
                        );
×
210
                }
211
        }
212

213
        /**
214
         * Retrieves loader assigned to $key
215
         *
216
         * @template T of key-of<self::DEFAULT_LOADERS>
217
         *
218
         * @param T|string $key The name of the loader to get.
219
         *
220
         * @return \WPGraphQL\Data\Loader\AbstractDataLoader
221
         * @throws \GraphQL\Error\UserError If the loader is not found.
222
         *
223
         * @phpstan-return ( $key is T ? new<self::DEFAULT_LOADERS[T]> : \WPGraphQL\Data\Loader\AbstractDataLoader )
224
         */
225
        public function get_loader( $key ) {
574✔
226
                // @todo: Remove the isset() when `graphql_data_loaders` is removed.
227
                if ( ! array_key_exists( $key, $this->loader_classes ) && ! isset( $this->loaders[ $key ] ) ) {
574✔
228
                        // translators: %s is the key of the loader that was not found.
229
                        throw new UserError( esc_html( sprintf( __( 'No loader assigned to the key %s', 'wp-graphql' ), $key ) ) );
×
230
                }
231

232
                // If the loader is not instantiated, instantiate it.
233
                if ( ! isset( $this->loaders[ $key ] ) ) {
574✔
234
                        try {
235
                                $this->loaders[ $key ] = new $this->loader_classes[ $key ]( $this );
574✔
236
                        } catch ( \Throwable $e ) {
×
237
                                // translators: %s is the key of the loader that failed to instantiate.
238
                                throw new UserError( esc_html( sprintf( __( 'Failed to instantiate %1$s: %2$s', 'wp-graphql' ), $this->loader_classes[ $key ], $e->getMessage() ) ) );
×
239
                        }
240
                }
241

242
                return $this->loaders[ $key ];
574✔
243
        }
244

245
        /**
246
         * Magic getter used to warn about accessing the loaders property directly.
247
         *
248
         * @todo Remove this when we change the property visibility.
249
         *
250
         * @param string $key The name of the property to get.
251
         * @return mixed
252
         */
253
        public function __get( $key ) {
×
254
                // Use default handling if the key is not a loader.
255
                if ( 'loaders' !== $key ) {
×
256
                        return $this->$key;
×
257
                }
258

259
                /** @todo Remove in v3.0.0 */
260
                _doing_it_wrong(
×
261
                        __METHOD__,
×
NEW
262
                        esc_html__( 'Accessing the AppContext::$loaders property from outside the AppContext class is deprecated and will throw an error in the next major version of WPGraphQL. Use AppContext::get_loader() instead.', 'wp-graphql' ),
×
NEW
263
                        '2.3.2'
×
UNCOV
264
                );
×
265

266
                // Return the actual loaders array.
267
                return $this->loaders;
×
268
        }
269

270
        /**
271
         * Returns the $args for the connection the field is a part of
272
         *
273
         * @todo These properties and methods are unused. We should consider deprecating/removing them.
274
         *
275
         * @return mixed[]|mixed
276
         */
277
        public function get_connection_args() {
×
278
                return isset( $this->currentConnection ) && isset( $this->connectionArgs[ $this->currentConnection ] ) ? $this->connectionArgs[ $this->currentConnection ] : [];
×
279
        }
280

281
        /**
282
         * Returns the current connection
283
         *
284
         * @todo These properties and methods are unused. We should consider deprecating/removing them.
285
         *
286
         * @return mixed|string|null
287
         */
288
        public function get_current_connection() {
×
289
                return isset( $this->currentConnection ) ? $this->currentConnection : null;
×
290
        }
291

292
        /**
293
         * @todo remove in v3.0.0
294
         * @deprecated use get_connection_args() instead
295
         * @codeCoverageIgnore
296
         *
297
         * @return mixed[]|mixed
298
         */
299
        public function getConnectionArgs() {
300
                _doing_it_wrong(
301
                        __METHOD__,
302
                        sprintf(
303
                                // translators: %s is the method name.
304
                                esc_html__( 'This function will be removed in the next major version of WPGraphQL. Use %s instead.', 'wp-graphql' ),
305
                                esc_html( self::class . '::get_connection_args()' )
306
                        ),
307
                        '0.8.4',
308
                );
309
                return $this->get_connection_args();
310
        }
311

312
        /**
313
         * @todo Remove in v3.0.0
314
         * @deprecated Use get_loader instead.
315
         * @codeCoverageIgnore
316
         *
317
         * @param string $key The name of the loader to get
318
         *
319
         * @return \WPGraphQL\Data\Loader\AbstractDataLoader
320
         */
321
        public function getLoader( $key ) {
322
                _doing_it_wrong(
323
                        __METHOD__,
324
                        sprintf(
325
                                // translators: %s is the method name.
326
                                esc_html__( 'This function will be removed in the next major version of WPGraphQL. Use %s instead.', 'wp-graphql' ),
327
                                esc_html( self::class . '::get_loader()' )
328
                        ),
329
                        '0.8.4',
330
                );
331
                return $this->get_loader( $key );
332
        }
333

334
        /**
335
         * @todo Remove in v3.0.0
336
         * @deprecated use get_current_connection instead.
337
         * @codeCoverageIgnore
338
         *
339
         * @return mixed|string|null
340
         */
341
        public function getCurrentConnection() {
342
                _doing_it_wrong(
343
                        __METHOD__,
344
                        sprintf(
345
                                // translators: %s is the method name.
346
                                esc_html__( 'This function will be removed in the next major version of WPGraphQL. Use %s instead.', 'wp-graphql' ),
347
                                esc_html( self::class . '::get_current_connection()' )
348
                        ),
349
                        '0.8.4',
350
                );
351
                return $this->get_current_connection();
352
        }
353
}
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