Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Last active June 4, 2020 13:51
Show Gist options
  • Save jasonbahl/ab36ff6a4a54c904c795643810b23afe to your computer and use it in GitHub Desktop.
Save jasonbahl/ab36ff6a4a54c904c795643810b23afe to your computer and use it in GitHub Desktop.
Showing how to register a connection to attached media in WPGraphQL
add_action( 'graphql_register_types', function() {
register_graphql_connection([
'fromType' => 'ContentNode',
'toType' => 'MediaItem',
'fromFieldName' => 'attachedMedia',
'connectionArgs' => \WPGraphQL\Connection\PostObjects::get_connection_args(),
'resolve' => function( \WPGraphQL\Model\Post $source, $args, $context, $info ) {
$resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $source, $args, $context, $info, 'attachment' );
$resolver->setQueryArg( 'post_parent', $source->ID );
return $resolver->get_connection();
}
]);
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment