Skip to content

Conversation

@jasonbahl
Copy link
Contributor

What does this implement/fix? Explain your changes.

fixes the sort order for querying fields of the taxonomy type.

Does this close any currently open issues?

closes #171

Any other comments?

Given a field of the "taxonomy" type set as a "multi select" and multiple terms stored:

CleanShot 2024-02-14 at 14 42 47

Before

Querying the field would return the nodes ordered by name

CleanShot 2024-02-14 at 14 44 38

After

Querying the field returns the nodes in the same order they're saved

CleanShot 2024-02-14 at 14 43 31

…s are saved - update taxonomy field resolver to se the default order and orderby when resolving with the TermObjectConnectionResolver
@jasonbahl jasonbahl self-assigned this Feb 14, 2024
@coveralls
Copy link

coveralls commented Feb 14, 2024

Pull Request Test Coverage Report for Build 4d75e4965c3416b8369ea2f446f5370f2948f1a1-PR-174

Details

  • 0 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 62.412%

Totals Coverage Status
Change from base Build 29440f7848aab82e427e540a9283d95d9006dc1a: 0.02%
Covered Lines: 2029
Relevant Lines: 3251

💛 - Coveralls
Comment on lines +96 to +192
public function testQueryReturnsTermsInOrderTheyWereSaved() {

$field_key = $this->register_acf_field([
'type' => 'taxonomy',
'name' => 'tax_term_order_test',
'show_in_graphql' => true,
'graphql_field_name' => 'termOrderTest',
'required' => 1,
'taxonomy' => 'category',
'add_term' => 0,
'save_terms' => 0,
'load_terms' => 0,
'return_format' => 'id',
'field_type' => 'multi_select',
'multiple' => 1,
'bidirectonal' => 0,
'bidirectional_target' => [],
], [
'name' => 'Taxonomy Order Test',
'graphql_field_name' => 'TaxonomyOrderTest',
'location' => [
[
[
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
]
]
],
'graphql_types' => [ 'Post' ],
]);

$cat_aaa = self::factory()->category->create([
'name' => 'AAA'
]);

$cat_bbb = self::factory()->category->create([
'name' => 'BBB'
]);

$cat_ccc = self::factory()->category->create([
'name' => 'CCC'
]);

$cats = [$cat_ccc, $cat_aaa, $cat_bbb];

update_field( $field_key, $cats, $this->published_post );

$query = '
query GetPost($id:ID!) {
post(id:$id idType:DATABASE_ID) {
id
databaseId
taxonomyOrderTest {
termOrderTest {
nodes {
__typename
databaseId
}
}
}
}
}
';

$actual = $this->graphql([
'query' => $query,
'variables' => [
'id' => $this->published_post->ID,
]
]);

codecept_debug( [
'$actual' => $actual,
'$cats' => $cats,
]);

self::assertQuerySuccessful( $actual, [
$this->expectedNode( 'post.taxonomyOrderTest.termOrderTest.nodes', [
'__typename' => 'Category',
'databaseId' => $cats[0]
], 0 ),
$this->expectedNode( 'post.taxonomyOrderTest.termOrderTest.nodes', [
'__typename' => 'Category',
'databaseId' => $cats[1]
], 1 ),
$this->expectedNode( 'post.taxonomyOrderTest.termOrderTest.nodes', [
'__typename' => 'Category',
'databaseId' => $cats[2]
], 2 ),
]);

foreach ( $cats as $cat ) {
wp_delete_term( $cat, 'category' );
}

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is sound 👍🏻

@jasonbahl jasonbahl merged commit cd6404a into develop Feb 14, 2024
@jasonbahl jasonbahl changed the title fix: taxonomy field resolves with wrong order fix: taxonomy field resolves sorted in the incorrect order Feb 20, 2024
@jasonbahl jasonbahl mentioned this pull request Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants