Skip to content

Commit c37b437

Browse files
committed
- add test
1 parent e28d834 commit c37b437

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

src/Data/Connection/AbstractConnectionResolver.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,12 +836,10 @@ public function set_query_arg( $key, $value ) {
836836
if ( ! is_array( $value ) ) {
837837
$value = [ $value ];
838838
}
839-
840839
$this->query_args[ $key ] = array_merge( $this->query_args[ $key ], $value );
841840
} else {
842841
$this->query_args[ $key ] = $value;
843842
}
844-
845843
return $this;
846844
}
847845

tests/wpunit/PostObjectConnectionQueriesTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,4 +1868,80 @@ public function testIsValidModelFilter(): void {
18681868
// Reset the filter
18691869
remove_filter( 'graphql_connection_is_valid_model', '__return_false' );
18701870
}
1871+
1872+
public function testConnectionArgsAsArrayAreMergedProperly() {
1873+
1874+
$categoryId = $this->factory()->category->create( [ 'name' => 'Test Category' ] );
1875+
$postId = $this->factory()->post->create( [ 'post_title' => 'Test Post', 'post_category' => [ $categoryId ] ] );
1876+
1877+
add_action( 'graphql_map_input_fields_to_wp_query', function($query_args, $input_args) {
1878+
if ( isset( $input_args['postsWithoutTags'] ) ) {
1879+
1880+
$query_args['tax_query'] = [
1881+
[
1882+
'taxonomy' => 'post_tag',
1883+
'field' => 'slug',
1884+
'terms' => [ 'test' ],
1885+
],
1886+
];
1887+
}
1888+
return $query_args;
1889+
}, 10 , 2 );
1890+
1891+
$query_args = [];
1892+
1893+
register_graphql_connection([
1894+
'fromType' => 'Category',
1895+
'toType' => 'Post',
1896+
'fromFieldName' => 'testMergedArgs',
1897+
'connectionArgs' => [
1898+
'postsWithoutTags' => [
1899+
'type' => 'Boolean',
1900+
],
1901+
],
1902+
'resolve' => function( \WPGraphQL\Model\Term $term, $args, $context, $info ) use ( &$query_args ) {
1903+
$resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $term, $args, $context, $info, 'post' );
1904+
$resolver->set_query_arg(
1905+
'tax_query',
1906+
[
1907+
[
1908+
'taxonomy' => $term->taxonomyName,
1909+
'terms' => [ $term->term_id ],
1910+
'field' => 'term_id',
1911+
'include_children' => false,
1912+
],
1913+
]
1914+
);
1915+
$query_args = $resolver->get_query_args();
1916+
return $resolver->get_connection();
1917+
},
1918+
]);
1919+
1920+
$query = '
1921+
query TestMergedArgs( $id: ID! ) {
1922+
category(id: $id idType: DATABASE_ID) {
1923+
testMergedArgs( where: { postsWithoutTags: true } ) {
1924+
nodes {
1925+
id
1926+
databaseId
1927+
}
1928+
}
1929+
}
1930+
}
1931+
';
1932+
1933+
$actual = $this->graphql( [
1934+
'query' => $query,
1935+
'variables' => [
1936+
'id' => $categoryId,
1937+
],
1938+
] );
1939+
1940+
$this->assertNotEmpty( $query_args['tax_query'] );
1941+
$this->assertCount( 2, $query_args['tax_query'] );
1942+
1943+
wp_delete_post( $postId, true );
1944+
wp_delete_term( $categoryId, 'category' );
1945+
1946+
}
18711947
}

0 commit comments

Comments
 (0)