How to resolve WP Post Object by custom mutation ? #2702
-
add_action( 'graphql_register_types', function() {
register_graphql_mutation( 'viewPopularPost', [
'inputFields' => [
'postId' => [
'type' => 'Int',
'description' => __( 'input field' ),
]
],
'outputFields' => [
'post' => [
'type' => 'Post',
'description' => __( 'output field' ),
'resolve' => function( $payload, $args, $context, $info ) {
graphql_debug($payload['post']);// I can check I got Post here!
return isset( $payload['post'] ) ? $payload['post'] : null;
}
]
],
'mutateAndGetPayload' => function( $input, $context, $info ) {
$post = get_post($input['postId']);
return [
'post' => $post,
];
}
] );
}); I wrote like this, but empty return resolve. i checked by but why did i get a something like empty object, and how to resolve WP Post Object. Please help me 😭 |
Beta Was this translation helpful? Give feedback.
Answered by
justlevine
Jan 29, 2023
Replies: 1 comment 1 reply
-
Hey @jiwon-mun , That's because you need to return a WPGraphQL Changing your return statement in - return isset( $payload['post'] ) ? $payload['post'] : null;
+ return isset( $payload['post'] ) ? new \WPGraphQL\Model\Post( $payload['post'] ) : null; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
jiwon-mun
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @jiwon-mun ,
That's because you need to return a WPGraphQL
Post
model and not theWP_Post
object.Changing your return statement in
$config['outputFields']['post']['resolve']
to the following should fix your issue: