import { EnumTypeDefinitionNode } from 'graphql' ; import typeDefs from './type_defs/schema.graphql' ; import { RequestContext , ResolverDynamicConfig , SurveyConfig } from './types' ; import { computeTermAggregationAllYearsWithCache , computeTermAggregationSingleYearWithCache } from './compute' ; import { Filters } from './filters' ; import { loadOrGetEntities } from './entities' ; import { TermAggregationOptions , AggregationFunction } from './compute/generic' ; /**
 * Return either e.g. other_tools.browsers.choices or other_tools.browsers.others_normalized
 */ export const getOtherKey = ( id ) => id . includes ( '_others' ) ? `${ id . replace ( '_others' , '' ) }.others.normalized` : `${ id }.choices` ; export const getGraphQLEnumValues = ( name ) : string [ ] => { const enumDef = typeDefs . definitions . find ( def => { return def . kind === 'EnumTypeDefinition' && def . name . value === name ; } ) as EnumTypeDefinitionNode ; if ( enumDef === undefined ) { throw new Error ( `No enum found matching name: ${ name }` ) ; } return enumDef . values ! . map ( v => v . name . value ) ; } ; param id param options export const getStaticResolvers = ( id , options = { } , aggregationFunction ? ) => ( { all_years : async ( { survey , filters } , args , { db } ) => computeTermAggregationAllYearsWithCache ( db , survey , id , { ... options , filters } , aggregationFunction ) , year : async ( { survey , filters } , { year } , { db } ) => computeTermAggregationSingleYearWithCache ( db , survey , id , { ... options , filters , year } , aggregationFunction ) } ) ; param getId param options export const getDynamicResolvers = ( getId , options = { } , aggregationFunction ? ) => ( { all_years : async ( { survey , id , filters } , args , { db } ) => computeTermAggregationAllYearsWithCache ( db , survey , getId ( id ) , { ... options , filters } , aggregationFunction ) , year : async ( { survey , id , filters } , { year } , { db } ) => computeTermAggregationSingleYearWithCache ( db , survey , getId ( id ) , { ... options , filters , year } , aggregationFunction ) } ) ; const demographicsFields = [ 'age' , 'country' , 'locale' , 'source' , 'gender' , 'race_ethnicity' , 'yearly_salary' , 'company_size' , 'years_of_experience' , 'job_title' , 'industry_sector' , 'industry_sector_others' , 'knowledge_score' , 'higher_education_degree' , 'disability_status' , 'disability_status_other' ] ; param survey export const getDemographicsResolvers = ( survey ) => { const resolvers = { } ; demographicsFields . forEach ( field => { resolvers [ field ] = ( { filters } ) => ( { survey , filters } ) ; } ) ; return resolvers ; } ;