11import { ALL_NETWORK_AGGREGATION_TYPES_MAP } from '..' ;
2- import { SUPPORTED_AGGREGATIONS } from '../common' ;
2+ import { SupportedAggregation , SUPPORTED_AGGREGATIONS } from '../common' ;
33import { Aggregations , Bucket , NumericAggregations } from '../types/aggregations' ;
44import { Hits } from '../types/hits' ;
55import { AllAggregations } from '../types/types' ;
@@ -14,36 +14,57 @@ type ResolveAggregationInput = {
1414type AggregationsTuple = [ Aggregations , Aggregations ] ;
1515type NumericAggregationsTuple = [ NumericAggregations , NumericAggregations ] ;
1616
17- const emptyAggregation = ( hits : number ) => ( {
17+ const emptyAggregation = ( hits : number ) : Aggregations => ( {
18+ __typename : 'Aggregations' ,
1819 bucket_count : 1 ,
1920 buckets : [ { key : '___aggregation_not_available___' , doc_count : hits } ] ,
2021} ) ;
2122
2223// mutation - update a single aggregations field in the accumulator
23- const addToAccumulator = ( { existingAggregation, aggregation, type } ) => {
24+ const addToAccumulator = < T extends Aggregations | NumericAggregations > ( {
25+ existingAggregation,
26+ aggregation,
27+ type,
28+ } : {
29+ existingAggregation : T | undefined ;
30+ aggregation : T ;
31+ type : SupportedAggregation ;
32+ } ) => {
2433 // if first aggregation, nothing to resolve with yet
2534 return ! existingAggregation
2635 ? aggregation
27- : resolveToNetworkAggregation ( type , [ aggregation , existingAggregation ] ) ;
36+ : resolveToNetworkAggregation < T > ( type , [ aggregation , existingAggregation ] ) ;
2837} ;
2938
3039/**
3140 * Resolves returned aggregations from network queries into single accumulated aggregation
41+ * ALL_NETWORK_AGGREGATION_TYPES_MAP should be initialised before using this function
3242 *
3343 * @param
3444 */
3545const resolveAggregations = ( { data, accumulator, requestedFields } : ResolveAggregationInput ) => {
3646 requestedFields . forEach ( ( requestedField ) => {
3747 const { aggregations, hits } = data ;
3848
39- const isFieldAvailable = ! ! aggregations [ requestedField ] ;
49+ /*
50+ * requested field will always be in ALL_NETWORK_AGGREGATION_TYPES_MAP
51+ * GQL schema validation will throw an error earlier if a requested field isn't in the schema
52+ */
4053 const type = ALL_NETWORK_AGGREGATION_TYPES_MAP . get ( requestedField ) ;
54+ if ( type === undefined ) {
55+ console . log (
56+ 'Could not find aggregation type.\nPlease ensure ALL_NETWORK_AGGREGATION_TYPES_MAP is initialised.' ,
57+ ) ;
58+ return ;
59+ }
60+
61+ const aggregation = aggregations [ requestedField ] ;
4162 const existingAggregation = accumulator [ requestedField ] ;
4263
43- if ( isFieldAvailable ) {
64+ if ( aggregation !== undefined ) {
4465 accumulator [ requestedField ] = addToAccumulator ( {
4566 existingAggregation,
46- aggregation : aggregations [ requestedField ] ,
67+ aggregation,
4768 type,
4869 } ) ;
4970 } else {
@@ -66,9 +87,9 @@ const resolveAggregations = ({ data, accumulator, requestedFields }: ResolveAggr
6687 * @param type
6788 * @param aggregations
6889 */
69- const resolveToNetworkAggregation = (
90+ const resolveToNetworkAggregation = < T > (
7091 type : string ,
71- aggregations : AggregationsTuple | NumericAggregationsTuple ,
92+ aggregations : [ T , T ] ,
7293) : Aggregations | NumericAggregations => {
7394 if ( type === SUPPORTED_AGGREGATIONS . Aggregations ) {
7495 return resolveAggregation ( aggregations as AggregationsTuple ) ;
@@ -179,7 +200,11 @@ export const resolveAggregation = (aggregations: AggregationsTuple): Aggregation
179200 const resolvedAggregation = aggregations . reduce ( ( resolvedAggregation , agg ) => {
180201 const computedBuckets = resolvedAggregation . buckets ;
181202 agg . buckets . forEach ( ( bucket ) => updateComputedBuckets ( bucket , computedBuckets ) ) ;
182- return { bucket_count : computedBuckets . length , buckets : computedBuckets } ;
203+ return {
204+ bucket_count : computedBuckets . length ,
205+ buckets : computedBuckets ,
206+ __typename : resolvedAggregation . __typename ,
207+ } ;
183208 } ) ;
184209
185210 return resolvedAggregation ;
0 commit comments