(builder)
| 15 | }; |
| 16 | |
| 17 | export const PgDistinctPlugin: Plugin = (builder) => { |
| 18 | // Creates enums for each entity based on their fields |
| 19 | builder.hook( |
| 20 | 'init', |
| 21 | (args, build) => { |
| 22 | const { |
| 23 | graphql: {GraphQLEnumType}, |
| 24 | newWithHooks, |
| 25 | pgIntrospectionResultsByKind, |
| 26 | } = build; |
| 27 | |
| 28 | pgIntrospectionResultsByKind.class.forEach((cls: PgClass) => { |
| 29 | if (!cls.isSelectable || build.pgOmit(cls, 'order')) return; |
| 30 | if (!cls.namespace) return; |
| 31 | |
| 32 | const enumTypeName = getEnumName(cls.name); |
| 33 | |
| 34 | const entityEnumValues: Record<string, {value: number}> = {}; |
| 35 | cls?.attributes?.forEach((attr, index) => { |
| 36 | if (attr.name.indexOf('_') !== 0) { |
| 37 | entityEnumValues[attr.name.toUpperCase()] = {value: index}; |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | newWithHooks( |
| 42 | GraphQLEnumType, |
| 43 | { |
| 44 | name: enumTypeName, |
| 45 | values: entityEnumValues, |
| 46 | }, |
| 47 | { |
| 48 | __origin: `Adding connection "distinct" enum type for ${cls.name}.`, |
| 49 | pgIntrospection: cls, |
| 50 | }, |
| 51 | true |
| 52 | ); |
| 53 | }); |
| 54 | |
| 55 | return args; |
| 56 | }, |
| 57 | ['AddDistinctEnumsPlugin'] |
| 58 | ); |
| 59 | |
| 60 | // Extends schema and modifies the query |
| 61 | builder.hook( |
| 62 | 'GraphQLObjectType:fields:field:args', |
| 63 | (args, build, {addArgDataGenerator, scope: {pgFieldIntrospection}}) => { |
| 64 | const { |
| 65 | extend, |
| 66 | graphql: {GraphQLList}, |
| 67 | pgSql: sql, |
| 68 | } = build as Build & {extend: Extend; pgSql: typeof PgSql}; |
| 69 | |
| 70 | const enumTypeName = getEnumName(pgFieldIntrospection?.name); |
| 71 | const enumType = build.getTypeByName(enumTypeName) as GraphQLEnumType; |
| 72 | |
| 73 | if (!enumType) { |
| 74 | return args; |
nothing calls this directly
no test coverage detected