import SearchkitRequest from '../core/SearchkitRequest' ; import { getAggregationsFromFacets , getFacetsFromResponse } from '../core/FacetsFns' ; import QueryManager from '../core/QueryManager' ; import { BaseFacet } from '../facets' ; export interface FacetResolverParameters { identifier : string ; query : string ; size : number ; } export default async ( parent , parameters , ctx ) => { const queryManager = parent . searchkit . queryManager ; const skRequest = parent . searchkit . skRequest ; const skFacets = parent . searchkit . facets ; const facet = skFacets && skFacets . find ( ( facet ) => facet . getIdentifier ( ) === parameters . identifier ) ; if ( ! facet ) return null ; try { const aggs = getAggregationsFromFacets ( queryManager , { [ facet . getIdentifier ( ) ] : { query : parameters . query , size : parameters . size } } , [ facet ] ) ; const response = await skRequest . search ( aggs ) ; const facets = getFacetsFromResponse ( [ facet ] , response , queryManager ) ; return facets [ 0 ] ; } catch ( e ) { throw e ; } } ;