Skip to content

Commit

Permalink
[drizzle] Fix table types for queries on relations to aliased tables
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Sep 10, 2024
1 parent afa9c34 commit bc73a7b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-dragons-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pothos/plugin-drizzle": patch
---

Fix table types for queries on relations to aliased tables
9 changes: 9 additions & 0 deletions packages/plugin-drizzle/src/global-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
TypeParam,
} from '@pothos/core';
import type {
AnyTable,
BuildQueryResult,
Column,
DBQueryConfig,
Expand Down Expand Up @@ -42,6 +43,10 @@ import type { GraphQLInputObjectType } from 'graphql';
import type { PothosDrizzlePlugin } from '.';
import type { DrizzleNodeRef } from './node-ref';

type MapToDbName<T> = {
[K in keyof T as T[K] extends { dbName: infer K2 extends string } ? K2 : K]: T[K];
};

declare global {
export namespace PothosSchemaTypes {
export interface Plugins<Types extends SchemaTypes> {
Expand All @@ -55,11 +60,15 @@ declare global {
export interface UserSchemaTypes {
DrizzleSchema: Record<string, unknown>;
DrizzleRelationSchema: Record<string, TableRelationalConfig>;
DrizzleRelationSchemaByDbName: Record<string, TableRelationalConfig>;
}

export interface ExtendDefaultTypes<PartialTypes extends Partial<UserSchemaTypes>> {
DrizzleSchema: PartialTypes['DrizzleSchema'] & {};
DrizzleRelationSchema: ExtractTablesWithRelations<PartialTypes['DrizzleSchema'] & {}>;
DrizzleRelationSchemaByDbName: MapToDbName<
ExtractTablesWithRelations<PartialTypes['DrizzleSchema'] & {}>
>;
}

export interface SchemaBuilder<Types extends SchemaTypes> {
Expand Down
10 changes: 5 additions & 5 deletions packages/plugin-drizzle/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export type RefForRelation<Types extends SchemaTypes, Rel extends Relation> = Re

export type TypesForRelation<Types extends SchemaTypes, Rel extends Relation> = BuildQueryResult<
Types['DrizzleRelationSchema'],
Types['DrizzleRelationSchema'][Rel['referencedTable']['_']['name']],
Types['DrizzleRelationSchemaByDbName'][Rel['referencedTableName']],
true
>;

Expand All @@ -410,7 +410,7 @@ export type QueryForField<
'one',
false,
Types['DrizzleRelationSchema'],
Types['DrizzleRelationSchema'][Rel['referencedTable']['_']['name']]
Types['DrizzleRelationSchemaByDbName'][Rel['referencedTableName']]
>,
'columns' | 'extra' | 'with'
>
Expand All @@ -419,7 +419,7 @@ export type QueryForField<
'many',
false,
Types['DrizzleRelationSchema'],
Types['DrizzleRelationSchema'][Rel['referencedTable']['_']['name']]
Types['DrizzleRelationSchemaByDbName'][Rel['referencedTableName']]
>,
'columns' | 'extra' | 'with'
>
Expand Down Expand Up @@ -537,7 +537,7 @@ export type RelatedConnectionOptions<
Nullable extends boolean,
Args extends InputFieldMap,
NodeTable extends
TableRelationalConfig = Types['DrizzleRelationSchema'][Table['relations'][Field]['referencedTable']['_']['name']],
TableRelationalConfig = Types['DrizzleRelationSchemaByDbName'][Table['relations'][Field]['referencedTableName']],
> = Omit<
PothosSchemaTypes.ObjectFieldOptions<
Types,
Expand Down Expand Up @@ -580,7 +580,7 @@ export type RelatedConnectionOptions<
>;
query?: QueryForRelatedConnection<Types, NodeTable, ConnectionArgs>;
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
type?: DrizzleRef<any, Table['relations'][Field]['referencedTable']['_']['name']>;
type?: DrizzleRef<any, Table['relations'][Field]['referencedTableName']>;

defaultSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number);
maxSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number);
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-drizzle/tests/example/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import AddGraphQL from '@pothos/plugin-add-graphql';
import ScopeAuthPlugin from '@pothos/plugin-relay';
import RelayPlugin from '@pothos/plugin-scope-auth';
import WithInputPlugin from '@pothos/plugin-with-input';
import { createTableRelationsHelpers, extractTablesRelationalConfig } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/libsql';
import DrizzlePlugin from '../../src';
import { type DrizzleSchema, client, db, schema } from './db';
Expand Down

0 comments on commit bc73a7b

Please sign in to comment.