Skip to content

Commit c558249

Browse files
committed
Fixed fields not being selected properly from fragments
1 parent 7cb641e commit c558249

File tree

13 files changed

+6119
-2583
lines changed

13 files changed

+6119
-2583
lines changed

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ server
1010
runMysql.ts
1111
runMysql-old.ts
1212
runPg.ts
13-
Tests/.temp
14-
Tests/Migrations
13+
tests/.temp
14+
tests/Migrations
1515
.DS_Store
1616
drizzle.test-heavy.ts
17-
changes.md
17+
changes.md
18+
tests/isolated.test.ts

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "drizzle-graphql",
33
"type": "module",
44
"author": "Drizzle Team",
5-
"version": "0.8.4",
5+
"version": "0.8.5",
66
"description": "Automatically generate GraphQL schema or customizable schema config fields from Drizzle ORM schema",
77
"scripts": {
88
"build": "pnpm tsx scripts/build.ts",
@@ -44,15 +44,15 @@
4444
"cpy": "^11.0.1",
4545
"dockerode": "^4.0.2",
4646
"dprint": "^0.45.1",
47-
"drizzle-kit": "^0.22.8",
48-
"drizzle-orm": "0.31.2",
47+
"drizzle-kit": "^0.24.0",
48+
"drizzle-orm": "0.33.0",
4949
"get-port": "^7.0.0",
5050
"glob": "^10.3.10",
5151
"graphql": "^16.3.0",
5252
"graphql-yoga": "^5.1.1",
5353
"mysql2": "^3.9.2",
5454
"node-pg": "^1.0.1",
55-
"pg": "^8.11.5",
55+
"pg": "^8.12.0",
5656
"postgres": "^3.4.3",
5757
"recast": "^0.23.6",
5858
"resolve-tspaths": "^0.8.18",

pnpm-lock.yaml

Lines changed: 31 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/util/builders/common.ts

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3937
import { capitalize } from '@/util/case-ops';
4038
import { remapFromGraphQLCore } from '@/util/data-mappers';
@@ -46,7 +44,6 @@ import {
4644
} from '@/util/type-converter';
4745

4846
import type { Column, Table } from 'drizzle-orm';
49-
import type { FieldNode, GraphQLResolveInfo } from 'graphql';
5047
import type { ResolveTree } from 'graphql-parse-resolve-info';
5148
import 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-
10270
export 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

156126
export const innerOrder = new GraphQLInputObjectType({
@@ -222,7 +192,7 @@ const generateColumnFilterValues = (column: Column, tableName: string, columnNam
222192
};
223193

224194
const 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>();
281251
const 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

Comments
 (0)