@@ -27,6 +27,118 @@ public function __construct() {
2727 */
2828public function define_public_hooks () {
2929add_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+ public function extend_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
154253foreach ($ taxonomy_filter_supported_typesas &$ type ) {
155254$ graphql_single_name =$ type ;
@@ -164,4 +263,26 @@ public function extend_wp_graphql_fields() {
164263}
165264unset($ type );
166265}
266+
267+ /**
268+ * Get the supported post types.
269+ *
270+ * @return array
271+ */
272+ private function get_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+ return array_merge ($ built_ins ,$ cpt_type_names );
287+ }
167288}