Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#116 ancestor support #157

Merged
merged 2 commits into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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