• 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

62.86
/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
        /**
35
         * Stores the class to use for the connection query.
36
         *
37
         * @var \WP_Query|null
38
         */
39
        public $connection_query_class = null;
40

41
        /**
42
         * Stores the url string for the current site
43
         *
44
         * @var string $root_url
45
         */
46
        public $root_url;
47

48
        /**
49
         * Stores the WP_User object of the current user
50
         *
51
         * @var \WP_User $viewer
52
         */
53
        public $viewer;
54

55
        /**
56
         * @var \WPGraphQL\Registry\TypeRegistry
57
         */
58
        public $type_registry;
59

60
        /**
61
         * Stores everything from the $_REQUEST global
62
         *
63
         * @var mixed $request
64
         */
65
        public $request;
66

67
        /**
68
         * Stores additional $config properties
69
         *
70
         * @var mixed $config
71
         */
72
        public $config;
73

74
        /**
75
         * Passes context about the current connection being resolved
76
         *
77
         * @var mixed|string|null
78
         */
79
        public $currentConnection = null;
80

81
        /**
82
         * Passes context about the current connection
83
         *
84
         * @var array<string,mixed>
85
         */
86
        public $connectionArgs = [];
87

88
        /**
89
         * Stores the loaders for the class
90
         *
91
         * @var array<string,\WPGraphQL\Data\Loader\AbstractDataLoader>
92
         */
93
        public $loaders = [];
94

95
        /**
96
         * Instance of the NodeResolver class to resolve nodes by URI
97
         *
98
         * @var \WPGraphQL\Data\NodeResolver
99
         */
100
        public $node_resolver;
101

102
        /**
103
         * AppContext constructor.
104
         */
105
        public function __construct() {
756✔
106

107
                /**
108
                 * Create a list of loaders to be available in AppContext
109
                 */
110
                $loaders = [
756✔
111
                        'comment_author'      => new CommentAuthorLoader( $this ),
756✔
112
                        'comment'             => new CommentLoader( $this ),
756✔
113
                        'enqueued_script'     => new EnqueuedScriptLoader( $this ),
756✔
114
                        'enqueued_stylesheet' => new EnqueuedStylesheetLoader( $this ),
756✔
115
                        'plugin'              => new PluginLoader( $this ),
756✔
116
                        'nav_menu_item'       => new PostObjectLoader( $this ),
756✔
117
                        'post'                => new PostObjectLoader( $this ),
756✔
118
                        'post_type'           => new PostTypeLoader( $this ),
756✔
119
                        'taxonomy'            => new TaxonomyLoader( $this ),
756✔
120
                        'term'                => new TermObjectLoader( $this ),
756✔
121
                        'theme'               => new ThemeLoader( $this ),
756✔
122
                        'user'                => new UserLoader( $this ),
756✔
123
                        'user_role'           => new UserRoleLoader( $this ),
756✔
124
                ];
756✔
125

126
                /**
127
                 * This filters the data loaders, allowing for additional loaders to be
128
                 * added to the AppContext or for existing loaders to be replaced if
129
                 * needed.
130
                 *
131
                 * @param array<string,\WPGraphQL\Data\Loader\AbstractDataLoader> $loaders The loaders accessible in the AppContext
132
                 * @param \WPGraphQL\AppContext                                   $context The AppContext
133
                 */
134
                $this->loaders = apply_filters( 'graphql_data_loaders', $loaders, $this );
756✔
135

136
                /**
137
                 * This sets up the NodeResolver to allow nodes to be resolved by URI
138
                 */
139
                $this->node_resolver = new NodeResolver( $this );
756✔
140

141
                /**
142
                 * This filters the config for the AppContext.
143
                 *
144
                 * This can be used to store additional context config, which is available to resolvers
145
                 * throughout the resolution of a GraphQL request.
146
                 *
147
                 * @param mixed[] $config The config array of the AppContext object
148
                 */
149
                $this->config = apply_filters( 'graphql_app_context_config', $this->config );
756✔
150
        }
151

152
        /**
153
         * Retrieves loader assigned to $key
154
         *
155
         * @param string $key The name of the loader to get
156
         *
157
         * @return \WPGraphQL\Data\Loader\AbstractDataLoader
158
         *
159
         * @deprecated Use get_loader instead.
160
         */
161
        public function getLoader( $key ) {
×
162
                _deprecated_function( __METHOD__, '0.8.4', self::class . '::get_loader()' );
×
163
                return $this->get_loader( $key );
×
164
        }
165

166
        /**
167
         * Retrieves loader assigned to $key
168
         *
169
         * @param string $key The name of the loader to get
170
         *
171
         * @return \WPGraphQL\Data\Loader\AbstractDataLoader
172
         * @throws \GraphQL\Error\UserError If the loader is not found.
173
         */
174
        public function get_loader( $key ) {
555✔
175
                if ( ! array_key_exists( $key, $this->loaders ) ) {
555✔
176
                        // translators: %s is the key of the loader that was not found.
177
                        throw new UserError( esc_html( sprintf( __( 'No loader assigned to the key %s', 'wp-graphql' ), $key ) ) );
×
178
                }
179

180
                return $this->loaders[ $key ];
555✔
181
        }
182

183
        /**
184
         * Returns the $args for the connection the field is a part of
185
         *
186
         * @deprecated use get_connection_args() instead
187
         * @return mixed[]|mixed
188
         */
189
        public function getConnectionArgs() {
×
190
                _deprecated_function( __METHOD__, '0.8.4', self::class . '::get_connection_args()' );
×
191
                return $this->get_connection_args();
×
192
        }
193

194
        /**
195
         * Returns the $args for the connection the field is a part of
196
         *
197
         * @return mixed[]|mixed
198
         */
199
        public function get_connection_args() {
×
200
                return isset( $this->currentConnection ) && isset( $this->connectionArgs[ $this->currentConnection ] ) ? $this->connectionArgs[ $this->currentConnection ] : [];
×
201
        }
202

203
        /**
204
         * Returns the current connection
205
         *
206
         * @return mixed|string|null
207
         */
208
        public function get_current_connection() {
×
209
                return isset( $this->currentConnection ) ? $this->currentConnection : null;
×
210
        }
211

212
        /**
213
         * @return mixed|string|null
214
         * @deprecated use get_current_connection instead.
215
         */
216
        public function getCurrentConnection() {
×
217
                return $this->get_current_connection();
×
218
        }
219
}
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