• 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

24.49
/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() {
762✔
143

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

147
                /**
148
                 * This sets up the NodeResolver to allow nodes to be resolved by URI
149
                 */
150
                $this->node_resolver = new NodeResolver( $this );
762✔
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 );
762✔
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 {
762✔
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 );
762✔
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' ) ) {
762✔
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
         * @param string $key The name of the loader to get
217
         *
218
         * @return \WPGraphQL\Data\Loader\AbstractDataLoader
219
         *
220
         * @deprecated Use get_loader instead.
221
         */
222
        public function getLoader( $key ) {
×
223
                _deprecated_function( __METHOD__, '0.8.4', self::class . '::get_loader()' );
×
224
                return $this->get_loader( $key );
×
225
        }
226

227
        /**
228
         * Retrieves loader assigned to $key
229
         *
230
         * @template T of key-of<self::DEFAULT_LOADERS>
231
         *
232
         * @param T|string $key The name of the loader to get.
233
         *
234
         * @return \WPGraphQL\Data\Loader\AbstractDataLoader
235
         * @throws \GraphQL\Error\UserError If the loader is not found.
236
         *
237
         * @phpstan-return ( $key is T ? new<self::DEFAULT_LOADERS[T]> : \WPGraphQL\Data\Loader\AbstractDataLoader )
238
         */
239
        public function get_loader( $key ) {
555✔
240
                // @todo: Remove the isset() when `graphql_data_loaders` is removed.
241
                if ( ! array_key_exists( $key, $this->loader_classes ) && ! isset( $this->loaders[ $key ] ) ) {
555✔
242
                        // translators: %s is the key of the loader that was not found.
243
                        throw new UserError( esc_html( sprintf( __( 'No loader assigned to the key %s', 'wp-graphql' ), $key ) ) );
×
244
                }
245

246
                // If the loader is not instantiated, instantiate it.
247
                if ( ! isset( $this->loaders[ $key ] ) ) {
555✔
248
                        try {
249
                                $this->loaders[ $key ] = new $this->loader_classes[ $key ]( $this );
555✔
250
                        } catch ( \Throwable $e ) {
×
251
                                // translators: %s is the key of the loader that failed to instantiate.
252
                                throw new UserError( esc_html( sprintf( __( 'Failed to instantiate %1$s: %2$s', 'wp-graphql' ), $this->loader_classes[ $key ], $e->getMessage() ) ) );
×
253
                        }
254
                }
255

256
                return $this->loaders[ $key ];
555✔
257
        }
258

259
        /**
260
         * Magic getter used to warn about accessing the loaders property directly.
261
         *
262
         * @todo Remove this when we change the property visibility.
263
         *
264
         * @param string $key The name of the property to get.
265
         * @return mixed
266
         */
267
        public function __get( $key ) {
×
268
                // Use default handling if the key is not a loader.
269
                if ( 'loaders' !== $key ) {
×
270
                        return $this->$key;
×
271
                }
272

273
                // Warn about accessing the loaders property directly.
274
                _doing_it_wrong(
×
275
                        __METHOD__,
×
276
                        esc_html__( 'Accessing the AppContext::$loaders property from outside the AppContext class is deprecated and will throw an error in a future version. Use AppContext::get_loader() instead.', 'wp-graphql' ), //phpcs:ignore PHPCS.Functions.VersionParameter.InvalidVersion -- @todo Fix this smell.
×
277
                        '2.3.2' // phpcs:ignore PHPCS.Functions.VersionParameter.OldVersionPlaceholder -- @todo Fix this smell.
×
278
                );
×
279

280
                // Return the actual loaders array.
281
                return $this->loaders;
×
282
        }
283

284
        /**
285
         * Returns the $args for the connection the field is a part of
286
         *
287
         * @deprecated use get_connection_args() instead
288
         * @return mixed[]|mixed
289
         */
290
        public function getConnectionArgs() {
×
291
                _deprecated_function( __METHOD__, '0.8.4', self::class . '::get_connection_args()' );
×
292
                return $this->get_connection_args();
×
293
        }
294

295
        /**
296
         * Returns the $args for the connection the field is a part of
297
         *
298
         * @todo These properties and methods are unused. We should consider deprecating/removing them.
299
         *
300
         * @return mixed[]|mixed
301
         */
302
        public function get_connection_args() {
×
303
                return isset( $this->currentConnection ) && isset( $this->connectionArgs[ $this->currentConnection ] ) ? $this->connectionArgs[ $this->currentConnection ] : [];
×
304
        }
305

306
        /**
307
         * Returns the current connection
308
         *
309
         * @todo These properties and methods are unused. We should consider deprecating/removing them.
310
         *
311
         * @return mixed|string|null
312
         */
313
        public function get_current_connection() {
×
314
                return isset( $this->currentConnection ) ? $this->currentConnection : null;
×
315
        }
316

317
        /**
318
         * @return mixed|string|null
319
         * @deprecated use get_current_connection instead.
320
         */
321
        public function getCurrentConnection() {
×
322
                return $this->get_current_connection();
×
323
        }
324
}
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