Skip to content

Commit d734a3e

Browse files
authored
Merge pull request #178 from wp-graphql/fix/177-taxonomy-field-not-handling-objects-properly
fix: Taxonomy field returns incorrect data if set to store objects instead of IDs
2 parents cd6404a + 5626533 commit d734a3e

File tree

3 files changed

+119
-24
lines changed

3 files changed

+119
-24
lines changed

composer.lock

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FieldType/Taxonomy.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,35 @@ public static function register_field_type(): void {
3333
$values = $value;
3434
}
3535

36-
$ids = array_map(
37-
static function ( $id ) {
38-
return absint( $id );
39-
},
40-
$values
36+
$ids = array_filter(
37+
array_map(
38+
static function ( $value ) {
39+
$id = $value;
40+
if ( is_array( $value ) && isset( $value['term_id'] ) ) {
41+
$id = $value['term_id'];
42+
}
43+
if ( isset( $value->term_id ) ) {
44+
$id = $value->term_id;
45+
}
46+
47+
$id = absint( $id );
48+
return ! empty( $id ) ? $id : null;
49+
},
50+
$values
51+
)
4152
);
4253

4354
if ( empty( $ids ) ) {
4455
return null;
4556
}
4657

4758
// set the default ordering if not overridden by args input on the field
48-
$args['where']['include'] = $ids;
59+
$args['where']['include'] = array_values( $ids );
4960
$args['where']['orderby'] = $args['where']['orderby'] ?? 'include';
5061
$args['where']['order'] = $args['where']['order'] ?? 'ASC';
51-
52-
return ( new TermObjectConnectionResolver( $root, $args, $context, $info ) )->get_connection();
62+
$args['first'] = count( $ids );
63+
$resolver = new TermObjectConnectionResolver( $root, $args, $context, $info, 'any' );
64+
return $resolver->get_connection();
5365
},
5466
];
5567

tests/wpunit/FieldTypes/TaxonomyFieldTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,5 +393,88 @@ interfaces {
393393
wp_delete_term( $category_2_id, 'category' );
394394
}
395395

396+
public function testTaxonomyFieldReturnsExpectedNodeWhenObjectIsSavedInsteadOfId() {
397+
398+
register_taxonomy( 'test_taxonomy', 'post', [
399+
'show_in_graphql' => true,
400+
'graphql_single_name' => 'TestCustomTaxonomy',
401+
'graphql_plural_name' => 'TestCustomTaxonomies',
402+
] );
403+
404+
$term_object = self::factory()->term->create_and_get( [
405+
'taxonomy' => 'test_taxonomy',
406+
'name' => 'Test Term',
407+
] );
408+
409+
$field_key = $this->register_acf_field([
410+
'type' => 'taxonomy',
411+
'name' => 'taxonomy_field_test',
412+
'show_in_graphql' => true,
413+
'graphql_field_name' => 'testTaxonomyField',
414+
'required' => 1,
415+
'taxonomy' => 'category',
416+
'add_term' => 0,
417+
'save_terms' => 0,
418+
'load_terms' => 0,
419+
'return_format' => 'object',
420+
'field_type' => 'select',
421+
'multiple' => 0,
422+
'bidirectonal' => 0,
423+
'bidirectional_target' => [],
424+
], [
425+
'name' => 'Post Taxonomy Test',
426+
'graphql_field_name' => 'TaxonomyFieldTest',
427+
'location' => [
428+
[
429+
[
430+
'param' => 'post_type',
431+
'operator' => '==',
432+
'value' => 'post',
433+
]
434+
]
435+
],
436+
'graphql_types' => [ 'Post' ],
437+
]);
438+
439+
update_field( $field_key, $term_object->term_id, $this->published_post->ID );
440+
441+
$query = '
442+
query GetPostAndTaxonomyField( $id: ID! ){
443+
post(id:$id idType:DATABASE_ID) {
444+
id
445+
title
446+
taxonomyFieldTest {
447+
testTaxonomyField {
448+
nodes {
449+
__typename
450+
databaseId
451+
}
452+
}
453+
}
454+
}
455+
}';
456+
457+
$variables = [
458+
'id' => $this->published_post->ID,
459+
];
460+
461+
$actual = $this->graphql([
462+
'query' => $query,
463+
'variables' => $variables,
464+
]);
465+
466+
self::assertQuerySuccessful( $actual, [
467+
$this->expectedNode( 'post.taxonomyFieldTest.testTaxonomyField.nodes', [
468+
'__typename' => 'TestCustomTaxonomy',
469+
'databaseId' => $term_object->term_id,
470+
], 0 ),
471+
]);
472+
473+
// cleanup
474+
wp_delete_term( $term_object->term_id, 'test_taxonomy' );
475+
unregister_taxonomy( 'test_taxonomy' );
476+
477+
}
478+
396479

397480
}

0 commit comments

Comments
 (0)