Skip to content

Commit 169bf36

Browse files
authored
Merge pull request #450 from smartive/next
fix: No nonnull in subwhere
2 parents ade2fe5 + 05e1af4 commit 169bf36

10 files changed

Lines changed: 389 additions & 136 deletions

File tree

migrations/20230912185644_setup.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ export const up = async (knex: Knex) => {
6767
table.boolean('deleted').notNullable();
6868
table.string('deleteRootType');
6969
table.uuid('deleteRootId');
70-
table.uuid('anotherId').nullable();
71-
table.index('anotherId');
72-
table.foreign('anotherId').references('id').inTable('AnotherObject').onDelete('CASCADE');
73-
table.integer('xyz').notNullable();
74-
table.specificType('time', 'time without time zone').nullable();
70+
table.uuid('anotherId');
71+
table.integer('xyz');
72+
table.specificType('time', 'time without time zone');
7573
});
7674

7775
await knex.schema.createTable('Reaction', (table) => {
@@ -110,7 +108,7 @@ export const up = async (knex: Knex) => {
110108
table.boolean('deleted').notNullable();
111109
table.string('deleteRootType');
112110
table.uuid('deleteRootId');
113-
table.string('content', undefined).nullable();
111+
table.string('content', undefined);
114112
});
115113

116114
await knex.schema.createTable('Review', (table) => {
@@ -122,7 +120,7 @@ export const up = async (knex: Knex) => {
122120

123121
await knex.schema.createTable('ReviewRevision', (table) => {
124122
table.uuid('id').notNullable().primary();
125-
table.decimal('rating', undefined, undefined).nullable();
123+
table.decimal('rating', undefined, undefined);
126124
});
127125

128126
};

src/migrations/generate.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ export class MigrationGenerator {
12881288
events: readonly ('INSERT' | 'UPDATE' | 'DELETE')[];
12891289
forEach: 'ROW' | 'STATEMENT';
12901290
deferrable?: 'INITIALLY DEFERRED' | 'INITIALLY IMMEDIATE';
1291-
function: { name: string; args?: string[] };
1291+
function: { name: string; args?: readonly string[] };
12921292
},
12931293
): string {
12941294
const eventsStr = this.sortTriggerEvents(entry.events).join(' OR ');
@@ -1305,7 +1305,7 @@ export class MigrationGenerator {
13051305
model: EntityModel,
13061306
entry: {
13071307
name: string;
1308-
function: { name: string; args?: string[] };
1308+
function: { name: string; args?: readonly string[] };
13091309
},
13101310
): void {
13111311
const fnName = entry.function.name;
@@ -1334,7 +1334,7 @@ export class MigrationGenerator {
13341334
events: readonly ('INSERT' | 'UPDATE' | 'DELETE')[];
13351335
forEach: 'ROW' | 'STATEMENT';
13361336
deferrable?: 'INITIALLY DEFERRED' | 'INITIALLY IMMEDIATE';
1337-
function: { name: string; args?: string[] };
1337+
function: { name: string; args?: readonly string[] };
13381338
},
13391339
) {
13401340
const eventsStr = this.sortTriggerEvents(entry.events).join(' OR ');

src/models/model-definitions.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type FieldDefinitionBase2 =
4040
} & BaseNumberType)
4141
| { type: 'Upload' }
4242
))
43-
| { kind: 'enum'; type: string; possibleValues?: Value[] }
43+
| { kind: 'enum'; type: string; possibleValues?: readonly Value[] }
4444
| { kind: 'custom'; type: string };
4545

4646
export type ObjectFieldDefinition = FieldDefinitionBase & FieldDefinitionBase2;
@@ -132,14 +132,14 @@ export type ModelDefinition = {
132132
| { kind: 'enum'; values: readonly string[]; deleted?: true }
133133
| { kind: 'raw-enum'; values: readonly string[] }
134134
| { kind: 'union'; types: readonly string[] }
135-
| { kind: 'interface'; fields: EntityFieldDefinition[] }
135+
| { kind: 'interface'; fields: readonly EntityFieldDefinition[] }
136136
| {
137137
kind: 'input';
138-
fields: ObjectFieldDefinition[];
138+
fields: readonly ObjectFieldDefinition[];
139139
}
140140
| {
141141
kind: 'object';
142-
fields: ObjectFieldDefinition[];
142+
fields: readonly ObjectFieldDefinition[];
143143
}
144144
| {
145145
kind: 'entity';
@@ -173,16 +173,16 @@ export type ModelDefinition = {
173173
};
174174
aggregatable?: boolean;
175175
displayField?: string;
176-
defaultOrderBy?: OrderBy[];
177-
fields: EntityFieldDefinition[];
176+
defaultOrderBy?: readonly OrderBy[];
177+
fields: readonly EntityFieldDefinition[];
178178

179179
/**
180180
* Use this field to explicitly mark/unmark an entity model as a many-to-many relation entity.
181181
* If not set, graphql-magic will try to infer it based on its fields.
182182
*/
183183
manyToManyRelation?: boolean;
184184

185-
constraints?: ConstraintDefinition[];
185+
constraints?: readonly ConstraintDefinition[];
186186

187187
// temporary fields for the generation of migrations
188188
deleted?: true;
@@ -216,7 +216,7 @@ export type ConstraintDefinition =
216216
events: readonly ('INSERT' | 'UPDATE' | 'DELETE')[];
217217
forEach: 'ROW' | 'STATEMENT';
218218
deferrable?: 'INITIALLY DEFERRED' | 'INITIALLY IMMEDIATE';
219-
function: { name: string; args?: string[] };
219+
function: { name: string; args?: readonly string[] };
220220
};
221221

222222
export type CheckConstraintDefinition = Extract<ConstraintDefinition, { kind: 'check' }>;
@@ -232,4 +232,4 @@ export type InputModelDefinition = Extract<ModelDefinition, { kind: 'input' }>;
232232
export type EntityModelDefinition = Extract<ModelDefinition, { kind: 'entity' }>;
233233
export type UnionModelDefinition = Extract<ModelDefinition, { kind: 'union' }>;
234234

235-
export type ModelDefinitions = ModelDefinition[];
235+
export type ModelDefinitions = readonly ModelDefinition[];

src/models/models.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ export class Models {
7373
public objects: ObjectModel[];
7474
public entities: EntityModel[];
7575
public unions: UnionModel[];
76-
public definitions: ModelDefinitions;
76+
public definitions: ModelDefinition[];
7777

7878
constructor(definitions: ModelDefinitions) {
79-
this.definitions = cloneDeep(definitions);
79+
this.definitions = cloneDeep(definitions) as ModelDefinition[];
8080
this.definitions.push(
8181
{
8282
kind: 'scalar',

src/schema/generate.ts

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -70,58 +70,60 @@ export const generateDefinitions = ({
7070
],
7171
[...(model.parent ? [model.parent] : []), ...(model.interfaces || [])],
7272
),
73-
input(`${model.name}Where`, [
74-
...model.fields
75-
.filter(({ kind, unique, filterable }) => (unique || filterable) && kind !== 'relation')
76-
.map((field) => ({
77-
name: field.name,
78-
type: field.type,
73+
...[false, true].map((isSubWhere) =>
74+
input(`${model.name}${isSubWhere ? 'Sub' : ''}Where`, [
75+
...model.fields
76+
.filter(({ kind, unique, filterable }) => (unique || filterable) && kind !== 'relation')
77+
.map((field) => ({
78+
name: field.name,
79+
type: field.type,
80+
list: true,
81+
default: typeof field.filterable === 'object' ? field.filterable.default : undefined,
82+
nonNull: isSubWhere ? false : typeof field.filterable === 'object' && field.filterable.nonNull === true,
83+
})),
84+
...model.fields
85+
.filter(({ comparable }) => comparable)
86+
.flatMap((field) => [
87+
{ name: `${field.name}_GT`, type: field.type },
88+
{ name: `${field.name}_GTE`, type: field.type },
89+
{ name: `${field.name}_LT`, type: field.type },
90+
{ name: `${field.name}_LTE`, type: field.type },
91+
]),
92+
...model.relations
93+
.filter(({ field: { filterable } }) => filterable)
94+
.map(({ name, targetModel, field }) => ({
95+
name,
96+
type: `${targetModel.name}Where`,
97+
nonNull: typeof field.filterable === 'object' && field.filterable.nonNull === true,
98+
})),
99+
...model.reverseRelations
100+
.filter(({ field: { reverseFilterable } }) => reverseFilterable)
101+
.flatMap((relation) => [
102+
{
103+
name: `${relation.name}_SOME`,
104+
type: `${relation.targetModel.name}Where`,
105+
},
106+
{
107+
name: `${relation.name}_NONE`,
108+
type: `${relation.targetModel.name}Where`,
109+
},
110+
]),
111+
{
112+
name: 'NOT',
113+
type: `${model.name}SubWhere`,
114+
},
115+
{
116+
name: 'AND',
117+
type: `${model.name}SubWhere`,
79118
list: true,
80-
default: typeof field.filterable === 'object' ? field.filterable.default : undefined,
81-
nonNull: typeof field.filterable === 'object' && field.filterable.nonNull === true,
82-
})),
83-
...model.fields
84-
.filter(({ comparable }) => comparable)
85-
.flatMap((field) => [
86-
{ name: `${field.name}_GT`, type: field.type },
87-
{ name: `${field.name}_GTE`, type: field.type },
88-
{ name: `${field.name}_LT`, type: field.type },
89-
{ name: `${field.name}_LTE`, type: field.type },
90-
]),
91-
...model.relations
92-
.filter(({ field: { filterable } }) => filterable)
93-
.map(({ name, targetModel, field }) => ({
94-
name,
95-
type: `${targetModel.name}Where`,
96-
nonNull: typeof field.filterable === 'object' && field.filterable.nonNull === true,
97-
})),
98-
...model.reverseRelations
99-
.filter(({ field: { reverseFilterable } }) => reverseFilterable)
100-
.flatMap((relation) => [
101-
{
102-
name: `${relation.name}_SOME`,
103-
type: `${relation.targetModel.name}Where`,
104-
},
105-
{
106-
name: `${relation.name}_NONE`,
107-
type: `${relation.targetModel.name}Where`,
108-
},
109-
]),
110-
{
111-
name: 'NOT',
112-
type: `${model.name}Where`,
113-
},
114-
{
115-
name: 'AND',
116-
type: `${model.name}Where`,
117-
list: true,
118-
},
119-
{
120-
name: 'OR',
121-
type: `${model.name}Where`,
122-
list: true,
123-
},
124-
]),
119+
},
120+
{
121+
name: 'OR',
122+
type: `${model.name}SubWhere`,
123+
list: true,
124+
},
125+
]),
126+
),
125127
input(
126128
`${model.name}WhereUnique`,
127129
model.fields.filter(({ unique }) => unique).map((field) => ({ name: field.name, type: field.type })),

0 commit comments

Comments
 (0)