Skip to content

Commit

Permalink
Merge pull request #157 from jasonbahl/feature/#116-ancestor-support
Browse files Browse the repository at this point in the history
#116 ancestor support
  • Loading branch information
jasonbahl committed Jun 23, 2017
2 parents aa447cb + a4a3077 commit 9c5d469
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 697 deletions.
25 changes: 24 additions & 1 deletion src/Type/PostObject/PostObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ private static function fields( $post_type_object ) {
return absint( $post->ID );
},
],
'ancestors' => [
'type' => Types::list_of( Types::post_object_union() ),
'description' => esc_html__( 'Ancestors of the object', 'wp-graphql' ),
'args' => [
'types' => [
'type' => Types::list_of( Types::post_type_enum() ),
'description' => __( 'The types of ancestors to check for. Defaults to the same type as the current object', 'wp-graphql' ),
],
],
'resolve' => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
$ancestors = [];
$types = ! empty( $args['types'] ) ? $args['types'] : [ $post->post_type ];
$ancestor_ids = get_ancestors( $post->ID, $post->post_type );
if ( ! empty( $ancestor_ids ) ) {
foreach ( $ancestor_ids as $ancestor_id ) {
$ancestor_obj = get_post( $ancestor_id );
if ( in_array( $ancestor_obj->post_type, $types, true ) ) {
$ancestors[] = $ancestor_obj;
}
}
}
return ! empty( $ancestors ) ? $ancestors : null;
},
],
'author' => [
'type' => Types::user(),
'description' => __( "The author field will return a queryable User type matching the post's author.", 'wp-graphql' ),
Expand Down Expand Up @@ -377,7 +401,6 @@ private static function fields( $post_type_object ) {
* @since 0.0.5
*/
return self::prepare_fields( $fields, $single_name );

};
endif;
return ! empty( self::$fields[ $single_name ] ) ? self::$fields[ $single_name ] : null;
Expand Down
21 changes: 17 additions & 4 deletions src/Type/TermObject/TermObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public function __construct( $taxonomy ) {
*
* This defines the fields for TermObjectType
*
* @param $taxonomy_object
*
* @param \WP_Taxonomy $taxonomy_object
* @return \GraphQL\Type\Definition\FieldDefinition|mixed|null
* @since 0.0.5
*/
Expand Down Expand Up @@ -130,8 +129,22 @@ private static function fields( $taxonomy_object ) {
return ! empty( $term->term_id ) ? absint( $term->term_id ) : null;
},
],
'count' => [
'type' => Types::int(),
'ancestors' => [
'type' => Types::list_of( Types::term_object( $taxonomy_object->name ) ),
'description' => esc_html__( 'The ancestors of the object', 'wp-graphql' ),
'resolve' => function( \WP_Term $term, $args, AppContext $context, ResolveInfo $info ) {
$ancestors = [];
$ancestor_ids = get_ancestors( $term->term_id, $term->taxonomy );
if ( ! empty( $ancestor_ids ) ) {
foreach ( $ancestor_ids as $ancestor_id ) {
$ancestors[] = get_term( $ancestor_id );
}
}
return ! empty( $ancestors ) ? $ancestors : null;
},
],
'count' => [
'type' => Types::int(),
'description' => __( 'The number of objects connected to the object', 'wp-graphql' ),
'resolve' => function( \WP_Term $term, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $term->count ) ? absint( $term->count ) : null;
Expand Down
Loading

0 comments on commit 9c5d469

Please sign in to comment.