Skip to content

Commit 8a51e40

Browse files
authored
Merge pull request #2452 from jasonbahl/bug/#2448-fix-comment-to-comment-author-connection-resolver
fix: #2448 - Fixes Comment.author connection resolver to properly load both User and CommentAuthor types
2 parents cd7819d + 6fa3130 commit 8a51e40

3 files changed

Lines changed: 19 additions & 11 deletions

File tree

src/Model/Comment.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use GraphQLRelay\Relay;
7+
use WP_Comment;
78

89
/**
910
* Class Comment - Models data for Comments
@@ -35,18 +36,18 @@ class Comment extends Model {
3536
/**
3637
* Stores the incoming WP_Comment object to be modeled
3738
*
38-
* @var \WP_Comment $data
39+
* @var WP_Comment $data
3940
*/
4041
protected $data;
4142

4243
/**
4344
* Comment constructor.
4445
*
45-
* @param \WP_Comment $comment The incoming WP_Comment to be modeled
46+
* @param WP_Comment $comment The incoming WP_Comment to be modeled
4647
*
4748
* @throws Exception
4849
*/
49-
public function __construct( \WP_Comment $comment ) {
50+
public function __construct( WP_Comment $comment ) {
5051

5152
$allowed_restricted_fields = [
5253
'id',

src/Type/ObjectType/Comment.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@ public static function register_type() {
3333

3434
$node = null;
3535

36-
// if the request is authenticated
37-
// and the comment is from a user, try and load
38-
// the user node
39-
if ( ! empty( $comment->userId ) && is_user_logged_in() ) {
36+
// try and load the user node
37+
if ( ! empty( $comment->userId ) ) {
4038
$node = $context->get_loader( 'user' )->load( absint( $comment->userId ) );
4139
}
4240

4341
// If no node is loaded, fallback to the
4442
// public comment author data
45-
if ( ! $node ) {
43+
if ( ! $node || ( true === $node->isPrivate ) ) {
4644
$node = ! empty( $comment->commentId ) ? $context->get_loader( 'comment_author' )->load( $comment->commentId ) : null;
4745
}
4846

tests/wpunit/CommentObjectQueriesTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,11 @@ public function dataProviderSwitchUser() {
663663
public function testQueryingAvatarOnUserAuthorsIsValidForPublicAndAuthenticatedRequests() {
664664

665665
// create a comment with a guest author as the author
666-
$comment_id = $this->createCommentObject();
666+
$comment_by_comment_author_id = $this->createCommentObject([
667+
'user_id' => 0
668+
]);
669+
670+
$comment_by_user_id = $this->createCommentObject();
667671

668672
$query = '
669673
query GetCommentAuthorWithAvatar($id:ID!){
@@ -682,7 +686,7 @@ public function testQueryingAvatarOnUserAuthorsIsValidForPublicAndAuthenticatedR
682686
}
683687
';
684688

685-
$global_id = \GraphQLRelay\Relay::toGlobalId( 'comment', $comment_id );
689+
$global_id = \GraphQLRelay\Relay::toGlobalId( 'comment', $comment_by_comment_author_id );
686690

687691
$actual = graphql( [
688692
'query' => $query,
@@ -698,21 +702,26 @@ public function testQueryingAvatarOnUserAuthorsIsValidForPublicAndAuthenticatedR
698702
$this->assertSame( 'CommentAuthor', $typename );
699703
$this->assertNotEmpty( $actual['data']['comment']['author']['node']['avatar']['url'] );
700704

701-
wp_set_current_user( $this->admin );
705+
$global_id = \GraphQLRelay\Relay::toGlobalId( 'comment', $comment_by_user_id );
702706

703707
$actual = graphql( [
704708
'query' => $query,
705709
'variables' => [
706710
'id' => $global_id,
707711
],
708712
] );
713+
709714
$this->assertArrayNotHasKey( 'errors', $actual );
710715
codecept_debug( $actual );
711716

712717
$typename = $actual['data']['comment']['author']['node']['__typename'];
713718

714719
$this->assertSame( 'User', $typename );
715720
$this->assertNotEmpty( $actual['data']['comment']['author']['node']['avatar']['url'] );
721+
722+
wp_delete_comment( $comment_by_user_id );
723+
wp_delete_comment( $comment_by_comment_author_id );
724+
716725
}
717726

718727

0 commit comments

Comments
 (0)