Skip to content

Commit 121a691

Browse files
authored
Merge pull request #3035 from jasonbahl/feat/3034-provide-better-error-when-type-does-not-exist
feat: provide better error when field references a type that does not exist
2 parents 1a7f4b0 + 35e49fc commit 121a691

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/Registry/TypeRegistry.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace WPGraphQL\Registry;
44

5+
use GraphQL\Error\Error;
56
use GraphQL\Type\Definition\Type;
67
use WPGraphQL\Data\DataSource;
78
use WPGraphQL\Mutation\CommentCreate;
@@ -943,8 +944,21 @@ protected function prepare_field( string $field_name, array $field_config, strin
943944
return null;
944945
}
945946

946-
$field_config['type'] = function () use ( $field_config ) {
947-
return $this->get_type( $field_config['type'] );
947+
$field_config['type'] = function () use ( $field_config, $type_name ) {
948+
$type = $this->get_type( $field_config['type'] );
949+
if ( ! $type ) {
950+
$message = sprintf(
951+
/* translators: %1$s is the Field name, %2$s is the type name the field belongs to. %3$s is the non-existent type name being referenced. */
952+
__( 'The field \'%1$s\' on Type \'%2$s\' is configured to return \'%3$s\' which is a non-existent Type in the Schema. Make sure to define a valid type for all fields. This might occur if there was a typo with \'%3$s\', or it needs to be registered to the Schema.', 'wp-graphql' ),
953+
$field_config['name'],
954+
$type_name,
955+
$field_config['type']
956+
);
957+
// We throw an error here instead of graphql_debug message, as an error would already be thrown if a type didn't exist at this point,
958+
// but now it will have a more helpful error message.
959+
throw new Error( esc_html( $message ) );
960+
}
961+
return $type;
948962
};
949963
}
950964

tests/wpunit/AccessFunctionsTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,4 +2338,45 @@ public function testDeRegisterMutationWithUnderscores() {
23382338
// query should error because the mutation was deregistered
23392339
self::assertQueryError( $actual, [] );
23402340
}
2341+
2342+
public function testRegisterFieldWithNonExistingTypeReturnsErrorWhenFieldIsReferenced() {
2343+
2344+
register_graphql_field( 'User', 'fakeField', [
2345+
'type' => 'NonExistingType'
2346+
]);
2347+
2348+
// This should query without error because the field doesn't impact types queried here
2349+
$query_one = '{posts{nodes{id}}}';
2350+
2351+
// Should not return error because the field with the invalid type is not being queried for
2352+
$query_two = '{users{nodes{id}}}';
2353+
2354+
// This should return an error because the fakeField is being queried for and it references a non-existent type
2355+
$query_three = '{users{nodes{id, fakeField}}}';
2356+
2357+
$actual = $this->graphql([
2358+
'query' => $query_one
2359+
]);
2360+
2361+
self::assertQuerySuccessful( $actual, [
2362+
$this->expectedField( 'posts', self::NOT_NULL ),
2363+
] );
2364+
2365+
$actual_two = $this->graphql([
2366+
'query' => $query_two
2367+
]);
2368+
2369+
self::assertQuerySuccessful( $actual_two, [
2370+
$this->expectedField( 'users', self::NOT_NULL ),
2371+
] );
2372+
2373+
// The third query should throw a GraphQL\Error calling out the fact that the field is referencing a non-existent type
2374+
$this->expectException( GraphQL\Error\Error::class );
2375+
$this->expectExceptionMessageMatches( "/non-existent/" );
2376+
2377+
$actual_three = $this->graphql([
2378+
'query' => $query_three
2379+
]);
2380+
2381+
}
23412382
}

0 commit comments

Comments
 (0)