Skip to content

Commit

Permalink
Merge pull request #501 from Quartz/fix/post-error
Browse files Browse the repository at this point in the history
Fix error message for non-existent post
  • Loading branch information
jasonbahl committed Aug 25, 2018
2 parents 4d8f7bf + c3a597f commit f0400a7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
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

0 comments on commit f0400a7

Please sign in to comment.