From c4d46ad17a1164956408426654d903c7abfe8ba1 Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Thu, 15 Jun 2023 15:00:03 -0300 Subject: [PATCH 1/2] GraphQL: get the right profile data when querying with `asPreview` --- inc/graphql.php | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/inc/graphql.php b/inc/graphql.php index cdc301a9..8b5a7a76 100644 --- a/inc/graphql.php +++ b/inc/graphql.php @@ -82,26 +82,41 @@ function register_byline_types() { ], 'type' => 'String', 'description' => __( 'Byline text.', 'byline-manager' ), - 'resolve' => function( WPGraphQL\Model\Post $post, array $args ) { - switch ( $args['format'] ) { + 'resolve' => function( WPGraphQL\Model\Post $profile, array $args ) { + $post_id = $profile->ID; + + if ( $profile->isPreview ) { + $post_id = $profile->parentDatabaseId; + } + + $format = $args['format'] ?? 'text'; + + switch ( $format ) { case 'links': - return get_the_byline_links( $post->ID ); + return get_the_byline_links( $post_id ); case 'post_links': - return get_the_byline_posts_links( $post->ID ); - } + return get_the_byline_posts_links( $post_id ); - // Default handling of 'text' formats. - return get_the_byline( $post->ID ); + case 'text': + default: + return get_the_byline( $post_id ); + } }, ], 'profiles' => [ 'type' => [ 'list_of' => 'ProfileTypes' ], 'description' => __( 'Byline profiles.', 'byline-manager' ), - 'resolve' => function ( WPGraphQL\Model\Post $post ) { - $byline_data = get_post_meta( $post->ID, 'byline', true ); + 'resolve' => function ( WPGraphQL\Model\Post $profile ) { + $post_id = $profile->ID; + + if ( $profile->isPreview ) { + $post_id = $profile->parentDatabaseId; + } + + $byline_data = get_post_meta( $post_id, 'byline', true ); - if ( ! $byline_data ) { + if ( empty( $byline_data ) ) { return null; } @@ -150,10 +165,10 @@ function register_byline_post_connection() { 'toType' => 'Post', 'fromFieldName' => 'profilePosts', 'connectionTypeName' => 'PostsFromProfileConnection', - 'resolve' => function ( $source, $args, $context, $info ) { - $profile = Models\Profile::get_by_post( $source->data ); + 'resolve' => function ( WPGraphQL\Model\Post $profile, $args, $context, $info ) { + $profile = Models\Profile::get_by_post( $profile->data ); $byline_id = $profile->term_id; - $resolver = new WPGraphQL\Data\Connection\PostObjectConnectionResolver( $source, $args, $context, $info ); + $resolver = new WPGraphQL\Data\Connection\PostObjectConnectionResolver( $profile, $args, $context, $info ); $resolver->set_query_arg( 'tax_query', From a0d423aad272b13fccb98aecb9fbf70acd29cf10 Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Thu, 15 Jun 2023 15:06:08 -0300 Subject: [PATCH 2/2] Making Github Action happy: fixig PHPCS issues --- inc/graphql.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/graphql.php b/inc/graphql.php index 8b5a7a76..ffb2c2ad 100644 --- a/inc/graphql.php +++ b/inc/graphql.php @@ -85,8 +85,8 @@ function register_byline_types() { 'resolve' => function( WPGraphQL\Model\Post $profile, array $args ) { $post_id = $profile->ID; - if ( $profile->isPreview ) { - $post_id = $profile->parentDatabaseId; + if ( $profile->isPreview ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + $post_id = $profile->parentDatabaseId; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase } $format = $args['format'] ?? 'text'; @@ -110,8 +110,8 @@ function register_byline_types() { 'resolve' => function ( WPGraphQL\Model\Post $profile ) { $post_id = $profile->ID; - if ( $profile->isPreview ) { - $post_id = $profile->parentDatabaseId; + if ( $profile->isPreview ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + $post_id = $profile->parentDatabaseId; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase } $byline_data = get_post_meta( $post_id, 'byline', true );