Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message for non-existent post #501

Merged
merged 2 commits into from Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Data/DataSource.php
Expand Up @@ -175,7 +175,7 @@ public static function resolve_post_object( $id, $post_type ) {

$post_object = \WP_Post::get_instance( $id );
if ( empty( $post_object ) ) {
throw new UserError( sprintf( __( 'No %1$s was found with the ID: %2$s', 'wp-graphql' ), $id, $post_type ) );
throw new UserError( sprintf( __( 'No %1$s was found with the ID: %2$s', 'wp-graphql' ), $post_type, $id ) );
}

/**
Expand Down
55 changes: 55 additions & 0 deletions tests/wpunit/PostObjectQueriesTest.php
Expand Up @@ -240,6 +240,61 @@ public function testPostQuery() {
$this->assertEquals( $expected, $actual );
}

/**
* testPostQueryWherePostDoesNotExist
*
* Tests a query for non existant post.
*
* @since 0.0.34
*/
public function testPostQueryWherePostDoesNotExist() {
/**
* Create the global ID based on the plugin_type and the created $id
*/
$global_id = \GraphQLRelay\Relay::toGlobalId( 'post', 'doesNotExist' );

/**
* Create the query string to pass to the $query
*/
$query = "
query {
post(id: \"{$global_id}\") {
slug
}
}";

/**
* Run the GraphQL query
*/
$actual = do_graphql_request( $query );

/**
* Establish the expectation for the output of the query
*/
$expected = [
'data' => [
'post' => null,
],
'errors' => [
[
'message' => 'No post was found with the ID: doesNotExist',
'locations' => [
[
'line' => 3,
'column' => 4,
],
],
'path' => [
'post',
],
'category' => 'user',
],
],
];

$this->assertEquals( $expected, $actual );
}

/**
* testPostQueryWithComments
*
Expand Down