@@ -62,7 +62,12 @@ final class WPGraphQL {
6262 /**
6363 * @var bool
6464 */
65- protected static $ is_graphql_request ;
65+ protected static $ is_graphql_request = false ;
66+
67+ /**
68+ * @var bool
69+ */
70+ protected static $ is_introspection_query = false ;
6671
6772 /**
6873 * The instance of the WPGraphQL object
@@ -140,12 +145,32 @@ public static function set_is_graphql_request( $is_graphql_request = false ) {
140145 }
141146
142147 /**
143- * @return bool
148+ * Whether the request is a graphql request or not
144149 */
145- public static function is_graphql_request () {
150+ public static function is_graphql_request (): bool {
146151 return self ::$ is_graphql_request ;
147152 }
148153
154+ /**
155+ * Set whether the request is an introspection query or not
156+ *
157+ * @param bool $is_introspection_query
158+ *
159+ * @since todo
160+ */
161+ public static function set_is_introspection_query ( bool $ is_introspection_query = false ): void {
162+ self ::$ is_introspection_query = $ is_introspection_query ;
163+ }
164+
165+ /**
166+ * Whether the request is an introspection query or not (query for __type or __schema)
167+ *
168+ * @since todo
169+ */
170+ public static function is_introspection_query (): bool {
171+ return self ::$ is_introspection_query ;
172+ }
173+
149174 /**
150175 * Sets up actions to run at certain spots throughout WordPress and the WPGraphQL execution
151176 * cycle
@@ -203,6 +228,7 @@ static function () {
203228
204229 // Throw an exception
205230 add_action ( 'do_graphql_request ' , [ $ this , 'min_php_version_check ' ] );
231+ add_action ( 'do_graphql_request ' , [ $ this , 'introspection_check ' ], 10 , 4 );
206232
207233 // Initialize Admin functionality
208234 add_action ( 'after_setup_theme ' , [ $ this , 'init_admin ' ] );
@@ -219,6 +245,41 @@ static function () {
219245 );
220246 }
221247
248+ /**
249+ * @param ?string $query The GraphQL query
250+ * @param ?string $operation The name of the operation
251+ * @param ?array<mixed> $variables Variables to be passed to your GraphQL
252+ * request
253+ * @param \GraphQL\Server\OperationParams $params The Operation Params. This includes any
254+ * extra params,
255+ *
256+ * @throws \GraphQL\Error\SyntaxError
257+ * @throws \Exception
258+ */
259+ public function introspection_check ( ?string $ query , ?string $ operation , ?array $ variables , \GraphQL \Server \OperationParams $ params ): void {
260+
261+ if ( empty ( $ query ) ) {
262+ return ;
263+ }
264+
265+ $ ast = \GraphQL \Language \Parser::parse ( $ query );
266+ $ is_introspection = false ;
267+
268+ \GraphQL \Language \Visitor::visit (
269+ $ ast ,
270+ [
271+ 'Field ' => static function ( \GraphQL \Language \AST \FieldNode $ node ) use ( &$ is_introspection ) {
272+ if ( '__schema ' === $ node ->name ->value || '__type ' === $ node ->name ->value ) {
273+ $ is_introspection = true ;
274+ return \GraphQL \Language \Visitor::stop ();
275+ }
276+ },
277+ ]
278+ );
279+
280+ self ::set_is_introspection_query ( $ is_introspection );
281+ }
282+
222283 /**
223284 * Check if the minimum PHP version requirement is met before execution begins.
224285 *
@@ -233,7 +294,7 @@ public function min_php_version_check() {
233294 throw new \Exception (
234295 esc_html (
235296 sprintf (
236- // translators: %1$s is the current PHP version, %2$s is the minimum required PHP version.
297+ // translators: %1$s is the current PHP version, %2$s is the minimum required PHP version.
237298 __ ( 'The server \'s current PHP version %1$s is lower than the WPGraphQL minimum required version: %2$s ' , 'wp-graphql ' ),
238299 PHP_VERSION ,
239300 GRAPHQL_MIN_PHP_VERSION
0 commit comments