@@ -32,9 +32,7 @@ import {
3232 GraphQLNonNull ,
3333 GraphQLObjectType ,
3434 GraphQLString ,
35- Kind ,
3635} from 'graphql' ;
37- import { parseResolveInfo } from 'graphql-parse-resolve-info' ;
3836
3937import { capitalize } from '@/util/case-ops' ;
4038import { remapFromGraphQLCore } from '@/util/data-mappers' ;
@@ -46,7 +44,6 @@ import {
4644} from '@/util/type-converter' ;
4745
4846import type { Column , Table } from 'drizzle-orm' ;
49- import type { FieldNode , GraphQLResolveInfo } from 'graphql' ;
5047import type { ResolveTree } from 'graphql-parse-resolve-info' ;
5148import type {
5249 FilterColumnOperators ,
@@ -70,35 +67,6 @@ const rqbCrashTypes = [
7067 'SQLiteBlobBuffer' ,
7168] ;
7269
73- export const extractSelectedColumnsFromNode = ( info : FieldNode , table : Table ) : Record < string , true > => {
74- const tableColumns = getTableColumns ( table ) ;
75-
76- if ( ! info . selectionSet ) {
77- const columnKeys = Object . entries ( tableColumns ) ;
78- const columnName = columnKeys . find ( ( e ) => rqbCrashTypes . find ( ( haram ) => e [ 1 ] . columnType !== haram ) ) ?. [ 0 ]
79- ?? columnKeys [ 0 ] ! [ 0 ] ;
80-
81- return Object . fromEntries ( [ [ columnName , true ] ] ) ;
82- }
83-
84- const selectedColumns : SelectedColumnsRaw = [ ] ;
85- for ( const columnSelection of info . selectionSet . selections ) {
86- if ( columnSelection . kind !== Kind . FIELD || ! tableColumns [ columnSelection . name . value ] ) continue ;
87-
88- selectedColumns . push ( [ columnSelection . name . value , true ] ) ;
89- }
90-
91- if ( ! selectedColumns . length ) {
92- const columnKeys = Object . entries ( tableColumns ) ;
93- const columnName = columnKeys . find ( ( e ) => rqbCrashTypes . find ( ( haram ) => e [ 1 ] . columnType !== haram ) ) ?. [ 0 ]
94- ?? columnKeys [ 0 ] ! [ 0 ] ;
95-
96- selectedColumns . push ( [ columnName , true ] ) ;
97- }
98-
99- return Object . fromEntries ( selectedColumns ) ;
100- } ;
101-
10270export const extractSelectedColumnsFromTree = (
10371 tree : Record < string , ResolveTree > ,
10472 table : Table ,
@@ -125,32 +93,34 @@ export const extractSelectedColumnsFromTree = (
12593 return Object . fromEntries ( selectedColumns ) ;
12694} ;
12795
128- export const extractSelectedColumnsSQLFormat = < TTable extends Table > (
129- info : GraphQLResolveInfo ,
130- queryName : string ,
131- table : TTable ,
132- ) : Record < string , Column > => {
133- const tableSelection = info . operation . selectionSet . selections . find (
134- ( e ) => e . kind === Kind . FIELD && e . name . value === queryName ,
135- ) as FieldNode | undefined ;
96+ /**
97+ * Can't automatically determine column type on type level
98+ * Since drizzle table types extend eachother
99+ */
100+ export const extractSelectedColumnsFromTreeSQLFormat = < TColType extends Column = Column > (
101+ tree : Record < string , ResolveTree > ,
102+ table : Table ,
103+ ) : Record < string , TColType > => {
104+ const tableColumns = getTableColumns ( table ) ;
136105
106+ const treeEntries = Object . entries ( tree ) ;
137107 const selectedColumns : SelectedSQLColumns = [ ] ;
138108
139- if ( ! tableSelection || ! tableSelection . selectionSet ) throw new GraphQLError ( 'Received empty column selection!' ) ;
140-
141- for ( const columnSelection of tableSelection . selectionSet . selections ) {
142- if ( columnSelection . kind !== Kind . FIELD || columnSelection . name . value === '__typename' ) continue ;
109+ for ( const [ fieldName , fieldData ] of treeEntries ) {
110+ if ( ! tableColumns [ fieldData . name ] ) continue ;
143111
144- selectedColumns . push ( [ columnSelection . name . value , table [ columnSelection . name . value as keyof Table ] as Column ] ) ;
112+ selectedColumns . push ( [ fieldData . name , tableColumns [ fieldData . name ] ! ] ) ;
145113 }
146114
147115 if ( ! selectedColumns . length ) {
148- const columnKeys = Object . entries ( getTableColumns ( table ) ) ;
116+ const columnKeys = Object . entries ( tableColumns ) ;
117+ const columnName = columnKeys . find ( ( e ) => rqbCrashTypes . find ( ( haram ) => e [ 1 ] . columnType !== haram ) ) ?. [ 0 ]
118+ ?? columnKeys [ 0 ] ! [ 0 ] ;
149119
150- selectedColumns . push ( [ columnKeys [ 0 ] ! [ 0 ] , columnKeys [ 0 ] ! [ 1 ] ] ) ;
120+ selectedColumns . push ( [ columnName , tableColumns [ columnName ] ! ] ) ;
151121 }
152122
153- return Object . fromEntries ( selectedColumns ) as any ;
123+ return Object . fromEntries ( selectedColumns ) as Record < string , TColType > ;
154124} ;
155125
156126export const innerOrder = new GraphQLInputObjectType ( {
@@ -222,7 +192,7 @@ const generateColumnFilterValues = (column: Column, tableName: string, columnNam
222192} ;
223193
224194const orderMap = new WeakMap < Object , Record < string , ConvertedInputColumn > > ( ) ;
225- const generateTableOrderCached = ( table : Table , tableName : string ) => {
195+ const generateTableOrderCached = ( table : Table ) => {
226196 if ( orderMap . has ( table ) ) return orderMap . get ( table ) ! ;
227197
228198 const columns = getTableColumns ( table ) ;
@@ -281,7 +251,7 @@ const orderTypeMap = new WeakMap<Object, GraphQLInputObjectType>();
281251const generateTableOrderTypeCached = ( table : Table , tableName : string ) => {
282252 if ( orderTypeMap . has ( table ) ) return orderTypeMap . get ( table ) ! ;
283253
284- const orderColumns = generateTableOrderCached ( table , tableName ) ;
254+ const orderColumns = generateTableOrderCached ( table ) ;
285255 const order = new GraphQLInputObjectType ( {
286256 name : `${ capitalize ( tableName ) } OrderBy` ,
287257 fields : orderColumns ,
@@ -716,13 +686,10 @@ export const extractRelationsParams = (
716686 relationMap : Record < string , Record < string , TableNamedRelations > > ,
717687 tables : Record < string , Table > ,
718688 tableName : string ,
719- info : GraphQLResolveInfo ,
689+ info : ResolveTree | undefined ,
720690 typeName : string ,
721691) : Record < string , Partial < ProcessedTableSelectArgs > > | undefined => {
722- const parsedInfo = parseResolveInfo ( info , {
723- deep : true ,
724- } ) as ResolveTree | undefined ;
725- if ( ! parsedInfo ) return undefined ;
692+ if ( ! info ) return undefined ;
726693
727- return extractRelationsParamsInner ( relationMap , tables , tableName , typeName , parsedInfo , true ) ;
694+ return extractRelationsParamsInner ( relationMap , tables , tableName , typeName , info , true ) ;
728695} ;
0 commit comments