Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Created June 28, 2019 15:06
Show Gist options
  • Save jasonbahl/4e47e6a002878c65777ae922d1ed8d1f to your computer and use it in GitHub Desktop.
Save jasonbahl/4e47e6a002878c65777ae922d1ed8d1f to your computer and use it in GitHub Desktop.
Register attachments connection for posts
add_action( 'graphql_register_types', function() {
register_graphql_connection([
'fromType' => 'Post',
'toType' => 'MediaItem',
'fromFieldName' => 'attachments',
'resolveNode' => function( $id, $args, $context ) {
return \WPGraphQL\Data\DataSource::resolve_post_object( $id, $context );
},
'resolve' => function( $post, $args, $context, $info ) {
$resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $post, $args, $context, $info, 'attachment' );
$resolver->setQueryArg( 'post__parent', $post->ID );
return $resolver->get_connection();
}
]);
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment