import type { Plugin } from "graphile-build" ; import type { PgClass , SQL } from "graphile-build-pg" ; import { AggregateGroupBySpec } from "./interfaces" ; const AddGroupByAggregateEnumValuesForColumnsPlugin = ( builder ) => { builder . hook ( "GraphQLEnumType:values" , ( values , build , context ) => { const { extend , pgColumnFilter , inflection , pgOmit : omit , describePgEntity , sqlCommentByAddingTags , pgSql : sql , } = build ; const pgAggregateGroupBySpecs = build . pgAggregateGroupBySpecs ; const { scope : { isPgAggregateGroupEnum , pgIntrospection } , } = context ; if ( ! isPgAggregateGroupEnum || ! pgIntrospection || pgIntrospection . kind !== "class" ) { return values ; } const table = pgIntrospection ; return extend ( values , table . attributes . reduce ( ( memo , attr ) => { if ( ! pgColumnFilter ( attr , build , context ) ) return memo ; if ( omit ( attr , "order" ) ) return memo ; const unique = attr . isUnique ; if ( unique ) return memo ; const fieldName = inflection . aggregateGroupByColumnEnum ( attr ) ; memo = extend ( memo , { [ fieldName ] : { value : { spec : ( tableAlias ) => sql . fragment `${ tableAlias }.${ sql . identifier ( attr . name ) }` , } , } , } , `Adding groupBy enum value for ${ describePgEntity ( attr ) }. You can rename this field with a 'Smart Comment':\n\n  ${ sqlCommentByAddingTags ( attr , { name : "newNameHere" , } ) }` ) ; pgAggregateGroupBySpecs . forEach ( ( spec ) => { if ( ( ! spec . shouldApplyToEntity || spec . shouldApplyToEntity ( attr ) ) && spec . isSuitableType ( attr . type ) ) { const fieldName = inflection . aggregateGroupByColumnDerivativeEnum ( attr , spec ) ; memo = extend ( memo , { [ fieldName ] : { value : { spec : ( tableAlias ) => spec . sqlWrap ( sql . fragment `${ tableAlias }.${ sql . identifier ( attr . name ) }` ) , } , } , } , `Adding groupBy enum value for '${ spec . id }' derivative of ${ describePgEntity ( attr ) }. You can rename this field with a 'Smart Comment':\n\n  ${ sqlCommentByAddingTags ( attr , { name : "newNameHere" , } ) }` ) ; } } ) ; return memo ; } , { } ) , `Adding group by values for columns from table '${ table . name }'` ) ; } , [ "AddGroupByAggregateEnumValuesForColumnsPlugin" ] ) ; } ; export default AddGroupByAggregateEnumValuesForColumnsPlugin ;