Created
June 28, 2019 15:06
-
-
Save jasonbahl/4e47e6a002878c65777ae922d1ed8d1f to your computer and use it in GitHub Desktop.
Register attachments connection for posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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