Skip to content

Commit

Permalink
Merge pull request #117 from alleyinteractive/feature/graphql-as-preview
Browse files Browse the repository at this point in the history
GraphQL: get the right profile data when querying with `asPreview`
  • Loading branch information
renatonascalves authored Jun 17, 2023
2 parents 79c7e0b + a0d423a commit fc93300
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions inc/graphql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$post_id = $profile->parentDatabaseId; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
}

$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 ) { // 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 );

if ( ! $byline_data ) {
if ( empty( $byline_data ) ) {
return null;
}

Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit fc93300

Please sign in to comment.