WordPress사이트에서 WPGraphQl을 이용해서 등록된 모든 User를 출력하는 Query를 아래와 같이 작성했다.
query getUsers {
users {
nodes {
id
name
accesstoken
}
}
}
하지만 관리자만 출력되고 테스트를 위해 생성한 다른 User는 출력되지 않았다. 아래에 그 이유가 나와있다.
https://www.wpgraphql.com/docs/security#access-control-rights
wpgraphql의 공식문서중에서 Access Control Rights부분을 보면
Access Control Rights
WordPress core has many access control rights established, and WPGraphQL follows them. Anything that is publicly exposed by WordPress is publicly exposed by WPGraphQL, and any data that requires a user to be authenticated to WordPress to see, WPGraphQL also requires requests to be properly authenticated for users to see.
For example, in WordPress core, users that have not published posts are only visible within the WordPress dashboard. There is no public URL for unpublished authors. WPGraphQL respects this, and queries for users will not include users without published posts. Properly authenticated requests from a user that has the capability to list users will be able to see unpublished authors, much like they would be able to within the WordPress dashboard.
두번째 문단에서 WordPress 코어에서는 게시물을 게시하지 않은 사용자는 WordPress 대시보드에만 표시되며, 이 unpublished authors는 공개 URL이 없고 WPGraphQL은 이에 따라 게시물이 없는 사용자가 쿼리의 결과에 포함되지 않는 다고 한다.
https://www.wpgraphql.com/recipes/make-all-users-public
https://www.wpgraphql.com/2020/12/11/allowing-wpgraphql-to-show-unpublished-authors-in-user-queries
위 내용을 참고하여 아래와 같이 스니펫을 추가해 주었다.
Adjust the underlying WP_User_Query
add_filter( 'graphql_connection_query_args', function( $query_args, $connection_resolver ) {
if ( $connection_resolver instanceof \WPGraphQL\Data\Connection\UserConnectionResolver ) {
unset( $query_args['has_published_posts'] );
}
return $query_args;
}, 10, 2 );
WPGraphQL은 User들과 연결할때 'has_published_posts' => true
를 사용해 공개된 Post가 있는 사용자만 쿼리할 수 있도록 설정되어있는데 이를 Unset하는 스니펫이다.
Filter the User Model to make all Users public
add_filter( 'graphql_object_visibility', function( $visibility, $model_name, $data, $owner, $current_user ) {
if ( 'UserObject' === $model_name ) {
$visibility = 'public';
}
return $visibility;
}, 10, 5 );
WPGraphQL은 데이터에 액세스할 때 사용자에게 어떤 객체 또는 해당 객체의 필드가 허용되어야 하는지를 결정하는 모델 레이어를 제공하는데, 이를 이용해서 'UserObject' === $model_name
일때 $visibility = 'public'
으로 설정하는 스니펫이다.
이제 해당 스니펫들을 WordPress사이트에서 활성화 시킨 후 다시 쿼리해 보면,

글을 올리지 않은 유저(UnpublishedAuthor)를 포함한 모든 User가 불러와지는 것을 확인할 수 있다.