Skip to content

Commit

Permalink
Merge pull request #122 from smartive/feat/none
Browse files Browse the repository at this point in the history
feat: Reverse filter none
  • Loading branch information
thommymo authored Dec 20, 2023
2 parents d87202d + b5c1478 commit ee20e16
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DATABASE_HOST=localhost
DATABASE_NAME=postgres
DATABASE_USER=postgres
DATABAE_PASSWORD=password
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ yarn-error.log*

*.tgz

.env

/tmp
5 changes: 2 additions & 3 deletions src/resolvers/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const SPECIAL_FILTERS: Record<string, string> = {
GTE: '?? >= ?',
LT: '?? < ?',
LTE: '?? <= ?',
SOME: 'SOME',
};

export type WhereNode = {
Expand Down Expand Up @@ -89,7 +88,7 @@ const applyWhere = (node: WhereNode, where: Where, ops: QueryBuilderOps, joins:
if (specialFilter) {
const [, actualKey, filter] = specialFilter;

if (filter === 'SOME') {
if (filter === 'SOME' || filter === 'NONE') {
const reverseRelation = node.model.getReverseRelation(actualKey);
const rootTableAlias = `${node.tableAlias}__W__${key}`;
const targetModel = reverseRelation.targetModel;
Expand All @@ -107,7 +106,7 @@ const applyWhere = (node: WhereNode, where: Where, ops: QueryBuilderOps, joins:

// TODO: make this work with subtypes
ops.push((query) =>
query.whereExists((subQuery) => {
query[filter === 'SOME' ? 'whereExists' : 'whereNotExists']((subQuery) => {
void subQuery
.from(`${targetModel.name} as ${aliases.getShort(tableAlias)}`)
.whereRaw(`?? = ??`, [
Expand Down
14 changes: 10 additions & 4 deletions src/schema/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@ export const generateDefinitions = ({
})),
...model.reverseRelations
.filter(({ field: { reverseFilterable } }) => reverseFilterable)
.map((relation) => ({
name: `${relation.name}_SOME`,
type: `${relation.targetModel.name}Where`,
})),
.flatMap((relation) => [
{
name: `${relation.name}_SOME`,
type: `${relation.targetModel.name}Where`,
},
{
name: `${relation.name}_NONE`,
type: `${relation.targetModel.name}Where`,
},
]),
]),
input(
`${model.name}WhereUnique`,
Expand Down
32 changes: 32 additions & 0 deletions tests/api/__snapshots__/query.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ exports[`query processes reverseFilters correctly 1`] = `
],
},
],
"noneFloat0": [
{
"id": "ba5d94a8-0035-4e45-9258-2f7676eb8d18",
"manyObjects": [
{
"float": 0.5,
},
],
},
],
"noneFloat0_5": [],
"noneFloat2": [
{
"id": "226a20e8-5c18-4423-99ca-eb0df6ff4fdd",
"manyObjects": [
{
"float": 0.5,
},
{
"float": 0,
},
],
},
{
"id": "ba5d94a8-0035-4e45-9258-2f7676eb8d18",
"manyObjects": [
{
"float": 0.5,
},
],
},
],
"withFloat0": [
{
"id": "226a20e8-5c18-4423-99ca-eb0df6ff4fdd",
Expand Down
18 changes: 18 additions & 0 deletions tests/api/query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ describe('query', () => {
float
}
}
noneFloat0: anotherObjects(where: { manyObjects_NONE: { float: 0 } }) {
id
manyObjects {
float
}
}
noneFloat0_5: anotherObjects(where: { manyObjects_NONE: { float: 0.5 } }) {
id
manyObjects {
float
}
}
noneFloat2: anotherObjects(where: { manyObjects_NONE: { float: 2 } }) {
id
manyObjects {
float
}
}
}
`)
).toMatchSnapshot();
Expand Down
1 change: 1 addition & 0 deletions tests/generated/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export type AnotherObjectOrderBy = {
export type AnotherObjectWhere = {
deleted?: InputMaybe<Array<Scalars['Boolean']['input']>>;
id?: InputMaybe<Array<Scalars['ID']['input']>>;
manyObjects_NONE?: InputMaybe<SomeObjectWhere>;
manyObjects_SOME?: InputMaybe<SomeObjectWhere>;
};

Expand Down
17 changes: 12 additions & 5 deletions tests/generated/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type AnotherObjectOrderBy = {
export type AnotherObjectWhere = {
deleted?: InputMaybe<Array<Scalars['Boolean']['input']>>;
id?: InputMaybe<Array<Scalars['ID']['input']>>;
manyObjects_NONE?: InputMaybe<SomeObjectWhere>;
manyObjects_SOME?: InputMaybe<SomeObjectWhere>;
};

Expand Down Expand Up @@ -856,7 +857,7 @@ export type SomeQueryQuery = { manyObjects: Array<{ __typename: 'SomeObject', id
export type ReverseFiltersQueryQueryVariables = Exact<{ [key: string]: never; }>;


export type ReverseFiltersQueryQuery = { first: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, second: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }> };
export type ReverseFiltersQueryQuery = { all: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, withFloat0: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, withFloat0_5: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, noneFloat0: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, noneFloat0_5: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }> };

export type DeleteAnotherObjectMutationMutationVariables = Exact<{
id: Scalars['ID']['input'];
Expand Down Expand Up @@ -1050,10 +1051,16 @@ export namespace SomeQuery {
export namespace ReverseFiltersQuery {
export type Variables = ReverseFiltersQueryQueryVariables;
export type query = ReverseFiltersQueryQuery;
export type first = NonNullable<(NonNullable<ReverseFiltersQueryQuery['first']>)[number]>;
export type manyObjects = NonNullable<(NonNullable<NonNullable<(NonNullable<ReverseFiltersQueryQuery['first']>)[number]>['manyObjects']>)[number]>;
export type second = NonNullable<(NonNullable<ReverseFiltersQueryQuery['second']>)[number]>;
export type _manyObjects = NonNullable<(NonNullable<NonNullable<(NonNullable<ReverseFiltersQueryQuery['second']>)[number]>['manyObjects']>)[number]>;
export type all = NonNullable<(NonNullable<ReverseFiltersQueryQuery['all']>)[number]>;
export type manyObjects = NonNullable<(NonNullable<NonNullable<(NonNullable<ReverseFiltersQueryQuery['all']>)[number]>['manyObjects']>)[number]>;
export type withFloat0 = NonNullable<(NonNullable<ReverseFiltersQueryQuery['withFloat0']>)[number]>;
export type _manyObjects = NonNullable<(NonNullable<NonNullable<(NonNullable<ReverseFiltersQueryQuery['withFloat0']>)[number]>['manyObjects']>)[number]>;
export type withFloat0_5 = NonNullable<(NonNullable<ReverseFiltersQueryQuery['withFloat0_5']>)[number]>;
export type __manyObjects = NonNullable<(NonNullable<NonNullable<(NonNullable<ReverseFiltersQueryQuery['withFloat0_5']>)[number]>['manyObjects']>)[number]>;
export type noneFloat0 = NonNullable<(NonNullable<ReverseFiltersQueryQuery['noneFloat0']>)[number]>;
export type ___manyObjects = NonNullable<(NonNullable<NonNullable<(NonNullable<ReverseFiltersQueryQuery['noneFloat0']>)[number]>['manyObjects']>)[number]>;
export type noneFloat0_5 = NonNullable<(NonNullable<ReverseFiltersQueryQuery['noneFloat0_5']>)[number]>;
export type ____manyObjects = NonNullable<(NonNullable<NonNullable<(NonNullable<ReverseFiltersQueryQuery['noneFloat0_5']>)[number]>['manyObjects']>)[number]>;
}

export namespace DeleteAnotherObjectMutation {
Expand Down
1 change: 1 addition & 0 deletions tests/generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ input AnotherObjectWhere {
id: [ID!]
deleted: [Boolean!]
manyObjects_SOME: SomeObjectWhere
manyObjects_NONE: SomeObjectWhere
}

input AnotherObjectWhereUnique {
Expand Down

0 comments on commit ee20e16

Please sign in to comment.