Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,10 @@ export interface PageInfo {
endCursor?: string | null;
}

export interface FindManyArgs<TSelect, TWhere, TOrderBy> {
export interface FindManyArgs<TSelect, TWhere, TCondition, TOrderBy> {
select?: TSelect;
where?: TWhere;
condition?: TCondition;
orderBy?: TOrderBy[];
first?: number;
last?: number;
Expand All @@ -287,9 +288,10 @@ export interface FindManyArgs<TSelect, TWhere, TOrderBy> {
offset?: number;
}

export interface FindFirstArgs<TSelect, TWhere> {
export interface FindFirstArgs<TSelect, TWhere, TCondition> {
select?: TSelect;
where?: TWhere;
condition?: TCondition;
}

export interface CreateArgs<TSelect, TData> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ exports[`model-generator generates model with all CRUD methods 1`] = `
import { OrmClient } from "../client";
import { QueryBuilder, buildFindManyDocument, buildFindFirstDocument, buildFindOneDocument, buildCreateDocument, buildUpdateByPkDocument, buildDeleteByPkDocument } from "../query-builder";
import type { ConnectionResult, FindManyArgs, FindFirstArgs, CreateArgs, UpdateArgs, DeleteArgs, InferSelectResult, StrictSelect } from "../select-types";
import type { User, UserWithRelations, UserSelect, UserFilter, UsersOrderBy, CreateUserInput, UpdateUserInput, UserPatch } from "../input-types";
import type { User, UserWithRelations, UserSelect, UserFilter, UserCondition, UsersOrderBy, CreateUserInput, UpdateUserInput, UserPatch } from "../input-types";
import { connectionFieldsMap } from "../input-types";
export class UserModel {
constructor(private client: OrmClient) {}
findMany<S extends UserSelect>(args: FindManyArgs<S, UserFilter, UsersOrderBy> & {
findMany<S extends UserSelect>(args: FindManyArgs<S, UserFilter, UserCondition, UsersOrderBy> & {
select: S;
} & StrictSelect<S, UserSelect>): QueryBuilder<{
users: ConnectionResult<InferSelectResult<UserWithRelations, S>>;
Expand All @@ -23,13 +23,14 @@ export class UserModel {
variables
} = buildFindManyDocument("User", "users", args.select, {
where: args?.where,
condition: args?.condition,
orderBy: args?.orderBy as string[] | undefined,
first: args?.first,
last: args?.last,
after: args?.after,
before: args?.before,
offset: args?.offset
}, "UserFilter", "UsersOrderBy", connectionFieldsMap);
}, "UserFilter", "UsersOrderBy", connectionFieldsMap, "UserCondition");
return new QueryBuilder({
client: this.client,
operation: "query",
Expand All @@ -39,7 +40,7 @@ export class UserModel {
variables
});
}
findFirst<S extends UserSelect>(args: FindFirstArgs<S, UserFilter> & {
findFirst<S extends UserSelect>(args: FindFirstArgs<S, UserFilter, UserCondition> & {
select: S;
} & StrictSelect<S, UserSelect>): QueryBuilder<{
users: {
Expand All @@ -50,8 +51,9 @@ export class UserModel {
document,
variables
} = buildFindFirstDocument("User", "users", args.select, {
where: args?.where
}, "UserFilter", connectionFieldsMap);
where: args?.where,
condition: args?.condition
}, "UserFilter", connectionFieldsMap, "UserCondition");
return new QueryBuilder({
client: this.client,
operation: "query",
Expand Down Expand Up @@ -156,11 +158,11 @@ exports[`model-generator generates model without update/delete when not availabl
import { OrmClient } from "../client";
import { QueryBuilder, buildFindManyDocument, buildFindFirstDocument, buildFindOneDocument, buildCreateDocument, buildUpdateByPkDocument, buildDeleteByPkDocument } from "../query-builder";
import type { ConnectionResult, FindManyArgs, FindFirstArgs, CreateArgs, UpdateArgs, DeleteArgs, InferSelectResult, StrictSelect } from "../select-types";
import type { AuditLog, AuditLogWithRelations, AuditLogSelect, AuditLogFilter, AuditLogsOrderBy, CreateAuditLogInput, UpdateAuditLogInput, AuditLogPatch } from "../input-types";
import type { AuditLog, AuditLogWithRelations, AuditLogSelect, AuditLogFilter, AuditLogCondition, AuditLogsOrderBy, CreateAuditLogInput, UpdateAuditLogInput, AuditLogPatch } from "../input-types";
import { connectionFieldsMap } from "../input-types";
export class AuditLogModel {
constructor(private client: OrmClient) {}
findMany<S extends AuditLogSelect>(args: FindManyArgs<S, AuditLogFilter, AuditLogsOrderBy> & {
findMany<S extends AuditLogSelect>(args: FindManyArgs<S, AuditLogFilter, AuditLogCondition, AuditLogsOrderBy> & {
select: S;
} & StrictSelect<S, AuditLogSelect>): QueryBuilder<{
auditLogs: ConnectionResult<InferSelectResult<AuditLogWithRelations, S>>;
Expand All @@ -170,13 +172,14 @@ export class AuditLogModel {
variables
} = buildFindManyDocument("AuditLog", "auditLogs", args.select, {
where: args?.where,
condition: args?.condition,
orderBy: args?.orderBy as string[] | undefined,
first: args?.first,
last: args?.last,
after: args?.after,
before: args?.before,
offset: args?.offset
}, "AuditLogFilter", "AuditLogsOrderBy", connectionFieldsMap);
}, "AuditLogFilter", "AuditLogsOrderBy", connectionFieldsMap, "AuditLogCondition");
return new QueryBuilder({
client: this.client,
operation: "query",
Expand All @@ -186,7 +189,7 @@ export class AuditLogModel {
variables
});
}
findFirst<S extends AuditLogSelect>(args: FindFirstArgs<S, AuditLogFilter> & {
findFirst<S extends AuditLogSelect>(args: FindFirstArgs<S, AuditLogFilter, AuditLogCondition> & {
select: S;
} & StrictSelect<S, AuditLogSelect>): QueryBuilder<{
auditLogs: {
Expand All @@ -197,8 +200,9 @@ export class AuditLogModel {
document,
variables
} = buildFindFirstDocument("AuditLog", "auditLogs", args.select, {
where: args?.where
}, "AuditLogFilter", connectionFieldsMap);
where: args?.where,
condition: args?.condition
}, "AuditLogFilter", connectionFieldsMap, "AuditLogCondition");
return new QueryBuilder({
client: this.client,
operation: "query",
Expand Down Expand Up @@ -259,11 +263,11 @@ exports[`model-generator handles custom query/mutation names 1`] = `
import { OrmClient } from "../client";
import { QueryBuilder, buildFindManyDocument, buildFindFirstDocument, buildFindOneDocument, buildCreateDocument, buildUpdateByPkDocument, buildDeleteByPkDocument } from "../query-builder";
import type { ConnectionResult, FindManyArgs, FindFirstArgs, CreateArgs, UpdateArgs, DeleteArgs, InferSelectResult, StrictSelect } from "../select-types";
import type { Organization, OrganizationWithRelations, OrganizationSelect, OrganizationFilter, OrganizationsOrderBy, CreateOrganizationInput, UpdateOrganizationInput, OrganizationPatch } from "../input-types";
import type { Organization, OrganizationWithRelations, OrganizationSelect, OrganizationFilter, OrganizationCondition, OrganizationsOrderBy, CreateOrganizationInput, UpdateOrganizationInput, OrganizationPatch } from "../input-types";
import { connectionFieldsMap } from "../input-types";
export class OrganizationModel {
constructor(private client: OrmClient) {}
findMany<S extends OrganizationSelect>(args: FindManyArgs<S, OrganizationFilter, OrganizationsOrderBy> & {
findMany<S extends OrganizationSelect>(args: FindManyArgs<S, OrganizationFilter, OrganizationCondition, OrganizationsOrderBy> & {
select: S;
} & StrictSelect<S, OrganizationSelect>): QueryBuilder<{
allOrganizations: ConnectionResult<InferSelectResult<OrganizationWithRelations, S>>;
Expand All @@ -273,13 +277,14 @@ export class OrganizationModel {
variables
} = buildFindManyDocument("Organization", "allOrganizations", args.select, {
where: args?.where,
condition: args?.condition,
orderBy: args?.orderBy as string[] | undefined,
first: args?.first,
last: args?.last,
after: args?.after,
before: args?.before,
offset: args?.offset
}, "OrganizationFilter", "OrganizationsOrderBy", connectionFieldsMap);
}, "OrganizationFilter", "OrganizationsOrderBy", connectionFieldsMap, "OrganizationCondition");
return new QueryBuilder({
client: this.client,
operation: "query",
Expand All @@ -289,7 +294,7 @@ export class OrganizationModel {
variables
});
}
findFirst<S extends OrganizationSelect>(args: FindFirstArgs<S, OrganizationFilter> & {
findFirst<S extends OrganizationSelect>(args: FindFirstArgs<S, OrganizationFilter, OrganizationCondition> & {
select: S;
} & StrictSelect<S, OrganizationSelect>): QueryBuilder<{
allOrganizations: {
Expand All @@ -300,8 +305,9 @@ export class OrganizationModel {
document,
variables
} = buildFindFirstDocument("Organization", "allOrganizations", args.select, {
where: args?.where
}, "OrganizationFilter", connectionFieldsMap);
where: args?.where,
condition: args?.condition
}, "OrganizationFilter", connectionFieldsMap, "OrganizationCondition");
return new QueryBuilder({
client: this.client,
operation: "query",
Expand Down
38 changes: 38 additions & 0 deletions graphql/codegen/src/__tests__/codegen/model-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,42 @@ describe('model-generator', () => {
expect(result.content).toContain('UpdateProductInput');
expect(result.content).toContain('ProductPatch');
});

it('imports and wires Condition type for findMany and findFirst', () => {
const table = createTable({
name: 'Contact',
fields: [
{ name: 'id', type: fieldTypes.uuid },
{ name: 'name', type: fieldTypes.string },
],
query: {
all: 'contacts',
one: 'contact',
create: 'createContact',
update: 'updateContact',
delete: 'deleteContact',
},
});

const result = generateModelFile(table, false);

// Condition type should be imported
expect(result.content).toContain('ContactCondition');

// findMany should include condition in its args type
expect(result.content).toContain(
'FindManyArgs<S, ContactFilter, ContactCondition, ContactsOrderBy>',
);

// findFirst should include condition in its args type
expect(result.content).toContain(
'FindFirstArgs<S, ContactFilter, ContactCondition>',
);

// condition should be forwarded in the body args object
expect(result.content).toContain('condition: args?.condition');

// conditionTypeName should be passed as a string literal to the document builder
expect(result.content).toContain('"ContactCondition"');
});
});
65 changes: 61 additions & 4 deletions graphql/codegen/src/__tests__/codegen/query-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ function buildConnectionSelections(nodeSelections: FieldNode[]): FieldNode[] {
}

function addVariable(
spec: { varName: string; argName?: string; typeName: string; value: unknown },
spec: { varName: string; argName?: string; typeName?: string; value: unknown },
definitions: VariableDefinitionNode[],
args: ArgumentNode[],
variables: Record<string, unknown>,
): void {
if (spec.value === undefined) return;
if (spec.value === undefined || !spec.typeName) return;
definitions.push(
t.variableDefinition({
variable: t.variable({ name: spec.varName }),
Expand Down Expand Up @@ -123,14 +123,15 @@ function buildSelections(
return fields;
}

function buildFindManyDocument<TSelect, TWhere>(
function buildFindManyDocument<TSelect, TWhere, TCondition>(
operationName: string,
queryField: string,
select: TSelect,
args: { where?: TWhere; first?: number; orderBy?: string[] },
args: { where?: TWhere; condition?: TCondition; first?: number; orderBy?: string[] },
filterTypeName: string,
orderByTypeName: string,
connectionFieldsMap?: Record<string, Record<string, string>>,
conditionTypeName?: string,
): { document: string; variables: Record<string, unknown> } {
const selections = select
? buildSelections(
Expand All @@ -143,6 +144,16 @@ function buildFindManyDocument<TSelect, TWhere>(
const queryArgs: ArgumentNode[] = [];
const variables: Record<string, unknown> = {};

addVariable(
{
varName: 'condition',
typeName: conditionTypeName,
value: args.condition,
},
variableDefinitions,
queryArgs,
variables,
);
addVariable(
{
varName: 'where',
Expand Down Expand Up @@ -557,6 +568,52 @@ describe('query-builder', () => {
orderBy: ['NAME_ASC'],
});
});

it('includes condition variable when conditionTypeName is provided', () => {
const { document, variables } = buildFindManyDocument(
'Contacts',
'contacts',
{ id: true, name: true },
{
condition: { embeddingNearby: { vector: [0.1, 0.2], metric: 'COSINE' } },
where: { name: { equalTo: 'test' } },
first: 5,
},
'ContactFilter',
'ContactsOrderBy',
undefined,
'ContactCondition',
);

// condition variable should appear in the query
expect(document).toContain('$condition: ContactCondition');
expect(document).toContain('condition: $condition');
// filter should still work alongside condition
expect(document).toContain('$where: ContactFilter');
expect(document).toContain('filter: $where');
// variables should include both
expect(variables.condition).toEqual({
embeddingNearby: { vector: [0.1, 0.2], metric: 'COSINE' },
});
expect(variables.where).toEqual({ name: { equalTo: 'test' } });
});

it('omits condition variable when not provided', () => {
const { document } = buildFindManyDocument(
'Users',
'users',
{ id: true },
{ first: 10 },
'UserFilter',
'UsersOrderBy',
undefined,
'UserCondition',
);

// condition should NOT appear since no value was provided
expect(document).not.toContain('$condition');
expect(document).not.toContain('condition:');
});
});

describe('buildMutationDocument', () => {
Expand Down
24 changes: 24 additions & 0 deletions graphql/codegen/src/core/codegen/orm/model-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export function generateModelFile(
const selectTypeName = `${typeName}Select`;
const relationTypeName = `${typeName}WithRelations`;
const whereTypeName = getFilterTypeName(table);
const conditionTypeName = `${typeName}Condition`;
const orderByTypeName = getOrderByTypeName(table);
const createInputTypeName = `Create${typeName}Input`;
const updateInputTypeName = `Update${typeName}Input`;
Expand Down Expand Up @@ -228,6 +229,7 @@ export function generateModelFile(
relationTypeName,
selectTypeName,
whereTypeName,
conditionTypeName,
orderByTypeName,
createInputTypeName,
updateInputTypeName,
Expand Down Expand Up @@ -270,6 +272,7 @@ export function generateModelFile(
t.tsTypeParameterInstantiation([
sel,
t.tsTypeReference(t.identifier(whereTypeName)),
t.tsTypeReference(t.identifier(conditionTypeName)),
t.tsTypeReference(t.identifier(orderByTypeName)),
]),
);
Expand Down Expand Up @@ -327,6 +330,15 @@ export function generateModelFile(
true,
),
),
t.objectProperty(
t.identifier('condition'),
t.optionalMemberExpression(
t.identifier('args'),
t.identifier('condition'),
false,
true,
),
),
t.objectProperty(
t.identifier('orderBy'),
t.tsAsExpression(
Expand Down Expand Up @@ -391,6 +403,7 @@ export function generateModelFile(
t.stringLiteral(whereTypeName),
t.stringLiteral(orderByTypeName),
t.identifier('connectionFieldsMap'),
t.stringLiteral(conditionTypeName),
];
classBody.push(
createClassMethod(
Expand All @@ -417,6 +430,7 @@ export function generateModelFile(
t.tsTypeParameterInstantiation([
sel,
t.tsTypeReference(t.identifier(whereTypeName)),
t.tsTypeReference(t.identifier(conditionTypeName)),
]),
);
const retType = (sel: t.TSType) =>
Expand Down Expand Up @@ -477,9 +491,19 @@ export function generateModelFile(
true,
),
),
t.objectProperty(
t.identifier('condition'),
t.optionalMemberExpression(
t.identifier('args'),
t.identifier('condition'),
false,
true,
),
),
]),
t.stringLiteral(whereTypeName),
t.identifier('connectionFieldsMap'),
t.stringLiteral(conditionTypeName),
];
classBody.push(
createClassMethod(
Expand Down
Loading
Loading