Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4e7b7b8

Browse files
authored
feat: add aggregations to schema (#12)
1 parentb1b2865 commit4e7b7b8

File tree

2 files changed

+216
-14
lines changed

2 files changed

+216
-14
lines changed

‎src/filter-query.php‎

Lines changed: 135 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,118 @@ public function __construct() {
2727
*/
2828
publicfunctiondefine_public_hooks() {
2929
add_action('graphql_register_types', [$this,'extend_wp_graphql_fields' ] );
30+
add_action('graphql_register_types', [$this,'extend_wp_graphql_aggregation_fields' ] );
31+
}
32+
33+
/**
34+
* Define the objects for aggregates.
35+
*
36+
* @return void
37+
*/
38+
publicfunctionextend_wp_graphql_aggregation_fields() {
39+
register_graphql_object_type(
40+
'BucketItem',
41+
[
42+
'description' =>'aggregate',
43+
'fields' => [
44+
'key' => [
45+
'type' =>'String',
46+
],
47+
'count' => [
48+
'type' =>'Integer',
49+
],
50+
],
51+
]
52+
);
53+
54+
$post_types =$this->get_supported_post_types();
55+
56+
// iterate through all the supported models.
57+
foreach ($post_typesas$post_type ) {
58+
// pick out the aggregate fields this model.
59+
$fields = ['categories','tags' ];
60+
61+
// if none continue.
62+
if (count($fields ) <1 ) {
63+
continue;
64+
}
65+
66+
// next we are generating the aggregates block for each model.
67+
$aggregate_graphql = [];
68+
foreach ($fieldsas$field ) {
69+
$aggregate_graphql[$field ] = ['type' =>array('list_of' =>'BucketItem' ) ];
70+
}
71+
72+
// store object name in a variable to DRY up code.
73+
$aggregate_for_type_name ='AggregatesFor' .ucfirst($post_type );
74+
75+
// finally, register the type.
76+
register_graphql_object_type(
77+
$aggregate_for_type_name,
78+
[
79+
'description' =>'aggregate',
80+
'fields' =>$aggregate_graphql,
81+
]
82+
);
83+
84+
// here we are registering the root `aggregates` field onto each model
85+
// that has aggregate fields defined.
86+
register_graphql_field(
87+
'RootQueryTo' .ucfirst($post_type ) .'Connection',
88+
'aggregations',
89+
[
90+
'type' =>$aggregate_for_type_name,
91+
'resolve' =>function($root,$args,$context,$info ) {
92+
return [
93+
'categories' => [
94+
[
95+
'key' =>'soccer',
96+
'count' =>25,
97+
],
98+
[
99+
'key' =>'rugby',
100+
'count' =>6,
101+
],
102+
],
103+
'tags' => [
104+
[
105+
'key' =>'adidas',
106+
'count' =>2,
107+
],
108+
[
109+
'key' =>'nike',
110+
'count' =>6,
111+
],
112+
],
113+
'seasons' => [
114+
[
115+
'key' =>'spring',
116+
'count' =>2,
117+
],
118+
[
119+
'key' =>'summer',
120+
'count' =>6,
121+
],
122+
[
123+
'key' =>'autumn',
124+
'count' =>2,
125+
],
126+
[
127+
'key' =>'winter',
128+
'count' =>30,
129+
],
130+
],
131+
'labels' => [
132+
[
133+
'key' =>'richard',
134+
'count' =>2,
135+
],
136+
],
137+
];
138+
},
139+
]
140+
);
141+
}
30142
}
31143

32144
/**
@@ -136,20 +248,7 @@ public function extend_wp_graphql_fields() {
136248
);
137249

138250
// Add { filter: TagOrCategory.TagOrCategoryFields.FilterFieldsInteger } input object in Posts.where args connector, until we figure how to add to root Posts object with args.
139-
$default_supported_wp_types =array('Post','Page' );
140-
$cpt_type_names =get_post_types(
141-
array(
142-
'public' =>true,
143-
'_builtin' =>false,
144-
),
145-
'names'
146-
);
147-
148-
foreach ($cpt_type_namesas$name ) {
149-
$cpt_type_names[$name ] =ucwords($name );
150-
}
151-
152-
$taxonomy_filter_supported_types =array_merge($default_supported_wp_types,$cpt_type_names );
251+
$taxonomy_filter_supported_types =$this->get_supported_post_types();
153252

154253
foreach ($taxonomy_filter_supported_typesas &$type ) {
155254
$graphql_single_name =$type;
@@ -164,4 +263,26 @@ public function extend_wp_graphql_fields() {
164263
}
165264
unset($type );
166265
}
266+
267+
/**
268+
* Get the supported post types.
269+
*
270+
* @return array
271+
*/
272+
privatefunctionget_supported_post_types():array {
273+
$built_ins = ['Post','Page' ];
274+
$cpt_type_names =get_post_types(
275+
[
276+
'public' =>true,
277+
'_builtin' =>false,
278+
],
279+
'names'
280+
);
281+
282+
foreach ($cpt_type_namesas$name ) {
283+
$cpt_type_names[$name ] =ucwords($name );
284+
}
285+
286+
returnarray_merge($built_ins,$cpt_type_names );
287+
}
167288
}

‎tests/src/test-filter-query.php‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,85 @@ public function data_provider(): array {
218218
),
219219
);
220220
}
221+
222+
publicfunctiondata_for_schema_exists_for_aggregations():array {
223+
return [
224+
'posts_have_aggregations' => [
225+
'query {
226+
posts {
227+
nodes {
228+
title
229+
}
230+
aggregations {
231+
tags {
232+
key
233+
count
234+
}
235+
}
236+
}
237+
}',
238+
'data',
239+
],
240+
'pages_have_aggregations' => [
241+
'query {
242+
pages {
243+
nodes {
244+
title
245+
}
246+
aggregations {
247+
tags {
248+
key
249+
count
250+
}
251+
}
252+
}
253+
}',
254+
'data',
255+
],
256+
'zombies_have_aggregations' => [
257+
'query {
258+
zombies {
259+
nodes {
260+
title
261+
}
262+
aggregations {
263+
tags {
264+
key
265+
count
266+
}
267+
}
268+
}
269+
}',
270+
'data',
271+
],
272+
'non_existing_type_should_not_have_aggregations' => [
273+
'query {
274+
doesNotExist {
275+
nodes {
276+
title
277+
}
278+
aggregations {
279+
tags {
280+
key
281+
count
282+
}
283+
}
284+
}
285+
}',
286+
'errors',
287+
],
288+
];
289+
}
290+
291+
/**
292+
*
293+
* @dataProvider data_for_schema_exists_for_aggregations
294+
* @return void
295+
* @throws Exception
296+
*/
297+
publicfunctiontest_schema_exists_for_aggregations($query,$expected ) {
298+
$result =do_graphql_request($query );
299+
$this->assertArrayHasKey($expected,$result,json_encode($result ) );
300+
$this->assertNotEmpty($result );
301+
}
221302
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp