Skip to content

Commit

Permalink
Reused input types for reduced memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sukairo-02 committed May 28, 2024
1 parent 6c727d5 commit 4055a35
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "drizzle-graphql",
"type": "module",
"author": "Drizzle Team",
"version": "0.7.2",
"version": "0.7.3",
"description": "Automatically generate GraphQL schema or customizable schema config fields from Drizzle ORM schema",
"scripts": {
"build": "pnpm tsx scripts/build.ts",
Expand Down
62 changes: 42 additions & 20 deletions src/util/builders/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,37 +277,35 @@ const generateTableSelectTypeFieldsCached = (table: Table, tableName: string): R
return remapped;
};

const generateSelectFields = <TWithOrder extends boolean>(
tables: Record<string, Table>,
tableName: string,
relationMap: Record<string, Record<string, TableNamedRelations>>,
typeName: string,
withOrder: TWithOrder,
usedTables: Set<string> = new Set(),
): SelectData<TWithOrder> => {
const relations = relationMap[tableName];
const relationEntries: [string, TableNamedRelations][] = relations ? Object.entries(relations) : [];

const table = tables[tableName]!;
const orderTypeMap = new WeakMap<Object, GraphQLInputObjectType>();
const generateTableOrderTypeCached = (table: Table, tableName: string) => {
if (orderTypeMap.has(table)) return orderTypeMap.get(table)!;

const orderColumns = generateTableOrderCached(table, tableName);
const order = withOrder
? new GraphQLInputObjectType({
name: `${typeName}OrderBy`,
fields: orderColumns,
})
: undefined;
const order = new GraphQLInputObjectType({
name: `${tableName}OrderBy`,
fields: orderColumns,
});

orderTypeMap.set(table, order);

return order;
};

const filterTypeMap = new WeakMap<Object, GraphQLInputObjectType>();
const generateTableFilterTypeCached = (table: Table, tableName: string) => {
if (filterTypeMap.has(table)) return filterTypeMap.get(table)!;

const filterColumns = generateTableFilterValuesCached(table, tableName);
const filters: GraphQLInputObjectType = new GraphQLInputObjectType({
name: `${typeName}Filters`,
name: `${tableName}Filters`,
fields: {
...filterColumns,
OR: {
type: new GraphQLList(
new GraphQLNonNull(
new GraphQLInputObjectType({
name: `${typeName}FiltersOr`,
name: `${tableName}FiltersOr`,
fields: filterColumns,
}),
),
Expand All @@ -316,6 +314,30 @@ const generateSelectFields = <TWithOrder extends boolean>(
},
});

filterTypeMap.set(table, filters);

return filters;
};

const generateSelectFields = <TWithOrder extends boolean>(
tables: Record<string, Table>,
tableName: string,
relationMap: Record<string, Record<string, TableNamedRelations>>,
typeName: string,
withOrder: TWithOrder,
usedTables: Set<string> = new Set(),
): SelectData<TWithOrder> => {
const relations = relationMap[tableName];
const relationEntries: [string, TableNamedRelations][] = relations ? Object.entries(relations) : [];

const table = tables[tableName]!;

const order = withOrder
? generateTableOrderTypeCached(table, tableName)
: undefined;

const filters = generateTableFilterTypeCached(table, tableName);

const tableFields = generateTableSelectTypeFieldsCached(table, tableName);

if (usedTables.has(tableName) || !relationEntries.length) {
Expand Down

0 comments on commit 4055a35

Please sign in to comment.