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

wp-graphql / wp-graphql / 13316763745

13 Feb 2025 08:45PM UTC coverage: 82.712% (-0.3%) from 83.023%
13316763745

push

github

web-flow
Merge pull request #3307 from wp-graphql/release/v2.0.0

release: v2.0.0

195 of 270 new or added lines in 20 files covered. (72.22%)

180 existing lines in 42 files now uncovered.

13836 of 16728 relevant lines covered (82.71%)

299.8 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
// @phpcs:ignore
32
#[\AllowDynamicProperties]
33
class AppContext {
34

35
        /**
36
         * Stores the class to use for the connection query.
37
         *
38
         * @var \WP_Query|null
39
         */
40
        public $connection_query_class = null;
41

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

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

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

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

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

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

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

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

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

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

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

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

137
                /**
138
                 * This sets up the NodeResolver to allow nodes to be resolved by URI
139
                 *
140
                 * @param \WPGraphQL\AppContext $app_context The AppContext instance
141
                 */
142
                $this->node_resolver = new NodeResolver( $this );
756✔
143

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

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

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

183
                return $this->loaders[ $key ];
555✔
184
        }
185

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

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

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

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