Skip to content

Commit

Permalink
(re)add support for nested types
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed May 10, 2023
1 parent 00a7d50 commit 8d136fc
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 13 deletions.
66 changes: 64 additions & 2 deletions packages/core/src/fields/filters/providers/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type StringFilterType = graphql.InputObjectType<{
contains: graphql.Arg<typeof graphql.String>;
startsWith: graphql.Arg<typeof graphql.String>;
endsWith: graphql.Arg<typeof graphql.String>;
not: graphql.Arg<StringFilterType>;
not: graphql.Arg<NestedStringFilterType>;
}>;

const StringFilter: StringFilterType = graphql.inputObject({
Expand All @@ -60,7 +60,69 @@ const StringFilter: StringFilterType = graphql.inputObject({
contains: graphql.arg({ type: graphql.String }),
startsWith: graphql.arg({ type: graphql.String }),
endsWith: graphql.arg({ type: graphql.String }),
not: graphql.arg({ type: StringFilter }),
not: graphql.arg({ type: NestedStringFilter }),
}),
});

type NestedStringNullableFilterType = graphql.InputObjectType<{
equals: graphql.Arg<typeof graphql.String>; // can be null
in: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>; // can be null
notIn: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>; // can be null
lt: graphql.Arg<typeof graphql.String>;
lte: graphql.Arg<typeof graphql.String>;
gt: graphql.Arg<typeof graphql.String>;
gte: graphql.Arg<typeof graphql.String>;
contains: graphql.Arg<typeof graphql.String>;
startsWith: graphql.Arg<typeof graphql.String>;
endsWith: graphql.Arg<typeof graphql.String>;
not: graphql.Arg<NestedStringNullableFilterType>; // can be null
}>;

const NestedStringNullableFilter: NestedStringNullableFilterType = graphql.inputObject({
name: 'NestedStringNullableFilter',
fields: () => ({
equals: graphql.arg({ type: graphql.String }), // can be null
in: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }), // can be null
notIn: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }), // can be null
lt: graphql.arg({ type: graphql.String }),
lte: graphql.arg({ type: graphql.String }),
gt: graphql.arg({ type: graphql.String }),
gte: graphql.arg({ type: graphql.String }),
contains: graphql.arg({ type: graphql.String }),
startsWith: graphql.arg({ type: graphql.String }),
endsWith: graphql.arg({ type: graphql.String }),
not: graphql.arg({ type: NestedStringNullableFilter }), // can be null
}),
});

type NestedStringFilterType = graphql.InputObjectType<{
equals: graphql.Arg<typeof graphql.String>;
in: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>;
notIn: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>;
lt: graphql.Arg<typeof graphql.String>;
lte: graphql.Arg<typeof graphql.String>;
gt: graphql.Arg<typeof graphql.String>;
gte: graphql.Arg<typeof graphql.String>;
contains: graphql.Arg<typeof graphql.String>;
startsWith: graphql.Arg<typeof graphql.String>;
endsWith: graphql.Arg<typeof graphql.String>;
not: graphql.Arg<NestedStringFilterType>;
}>;

const NestedStringFilter: NestedStringFilterType = graphql.inputObject({
name: 'NestedStringFilter',
fields: () => ({
equals: graphql.arg({ type: graphql.String }),
in: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }),
notIn: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }),
lt: graphql.arg({ type: graphql.String }),
lte: graphql.arg({ type: graphql.String }),
gt: graphql.arg({ type: graphql.String }),
gte: graphql.arg({ type: graphql.String }),
contains: graphql.arg({ type: graphql.String }),
startsWith: graphql.arg({ type: graphql.String }),
endsWith: graphql.arg({ type: graphql.String }),
not: graphql.arg({ type: NestedStringFilter }),
}),
});

Expand Down
66 changes: 64 additions & 2 deletions packages/core/src/fields/filters/providers/postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type StringFilterType = graphql.InputObjectType<{
startsWith: graphql.Arg<typeof graphql.String>;
endsWith: graphql.Arg<typeof graphql.String>;
mode: graphql.Arg<typeof QueryMode>;
not: graphql.Arg<StringFilterType>;
not: graphql.Arg<NestedStringFilterType>;
}>;

const StringFilter: StringFilterType = graphql.inputObject({
Expand All @@ -66,7 +66,69 @@ const StringFilter: StringFilterType = graphql.inputObject({
startsWith: graphql.arg({ type: graphql.String }),
endsWith: graphql.arg({ type: graphql.String }),
mode: graphql.arg({ type: QueryMode }),
not: graphql.arg({ type: StringFilter }),
not: graphql.arg({ type: NestedStringFilter }),
}),
});

type NestedStringNullableFilterType = graphql.InputObjectType<{
equals: graphql.Arg<typeof graphql.String>; // can be null
in: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>; // can be null
notIn: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>; // can be null
lt: graphql.Arg<typeof graphql.String>;
lte: graphql.Arg<typeof graphql.String>;
gt: graphql.Arg<typeof graphql.String>;
gte: graphql.Arg<typeof graphql.String>;
contains: graphql.Arg<typeof graphql.String>;
startsWith: graphql.Arg<typeof graphql.String>;
endsWith: graphql.Arg<typeof graphql.String>;
not: graphql.Arg<NestedStringNullableFilterType>; // can be null
}>;

const NestedStringNullableFilter: NestedStringNullableFilterType = graphql.inputObject({
name: 'NestedStringNullableFilter',
fields: () => ({
equals: graphql.arg({ type: graphql.String }), // can be null
in: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }), // can be null
notIn: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }), // can be null
lt: graphql.arg({ type: graphql.String }),
lte: graphql.arg({ type: graphql.String }),
gt: graphql.arg({ type: graphql.String }),
gte: graphql.arg({ type: graphql.String }),
contains: graphql.arg({ type: graphql.String }),
startsWith: graphql.arg({ type: graphql.String }),
endsWith: graphql.arg({ type: graphql.String }),
not: graphql.arg({ type: NestedStringNullableFilter }), // can be null
}),
});

type NestedStringFilterType = graphql.InputObjectType<{
equals: graphql.Arg<typeof graphql.String>;
in: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>;
notIn: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>;
lt: graphql.Arg<typeof graphql.String>;
lte: graphql.Arg<typeof graphql.String>;
gt: graphql.Arg<typeof graphql.String>;
gte: graphql.Arg<typeof graphql.String>;
contains: graphql.Arg<typeof graphql.String>;
startsWith: graphql.Arg<typeof graphql.String>;
endsWith: graphql.Arg<typeof graphql.String>;
not: graphql.Arg<NestedStringFilterType>;
}>;

const NestedStringFilter: NestedStringFilterType = graphql.inputObject({
name: 'NestedStringFilter',
fields: () => ({
equals: graphql.arg({ type: graphql.String }),
in: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }),
notIn: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }),
lt: graphql.arg({ type: graphql.String }),
lte: graphql.arg({ type: graphql.String }),
gt: graphql.arg({ type: graphql.String }),
gte: graphql.arg({ type: graphql.String }),
contains: graphql.arg({ type: graphql.String }),
startsWith: graphql.arg({ type: graphql.String }),
endsWith: graphql.arg({ type: graphql.String }),
not: graphql.arg({ type: NestedStringFilter }),
}),
});

Expand Down
66 changes: 64 additions & 2 deletions packages/core/src/fields/filters/providers/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type StringFilterType = graphql.InputObjectType<{
contains: graphql.Arg<typeof graphql.String>;
startsWith: graphql.Arg<typeof graphql.String>;
endsWith: graphql.Arg<typeof graphql.String>;
not: graphql.Arg<StringFilterType>;
not: graphql.Arg<NestedStringFilterType>;
}>;

const StringFilter: StringFilterType = graphql.inputObject({
Expand All @@ -60,7 +60,69 @@ const StringFilter: StringFilterType = graphql.inputObject({
contains: graphql.arg({ type: graphql.String }),
startsWith: graphql.arg({ type: graphql.String }),
endsWith: graphql.arg({ type: graphql.String }),
not: graphql.arg({ type: StringFilter }),
not: graphql.arg({ type: NestedStringFilter }),
}),
});

type NestedStringNullableFilterType = graphql.InputObjectType<{
equals: graphql.Arg<typeof graphql.String>; // can be null
in: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>; // can be null
notIn: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>; // can be null
lt: graphql.Arg<typeof graphql.String>;
lte: graphql.Arg<typeof graphql.String>;
gt: graphql.Arg<typeof graphql.String>;
gte: graphql.Arg<typeof graphql.String>;
contains: graphql.Arg<typeof graphql.String>;
startsWith: graphql.Arg<typeof graphql.String>;
endsWith: graphql.Arg<typeof graphql.String>;
not: graphql.Arg<NestedStringNullableFilterType>; // can be null
}>;

const NestedStringNullableFilter: NestedStringNullableFilterType = graphql.inputObject({
name: 'NestedStringNullableFilter',
fields: () => ({
equals: graphql.arg({ type: graphql.String }), // can be null
in: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }), // can be null
notIn: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }), // can be null
lt: graphql.arg({ type: graphql.String }),
lte: graphql.arg({ type: graphql.String }),
gt: graphql.arg({ type: graphql.String }),
gte: graphql.arg({ type: graphql.String }),
contains: graphql.arg({ type: graphql.String }),
startsWith: graphql.arg({ type: graphql.String }),
endsWith: graphql.arg({ type: graphql.String }),
not: graphql.arg({ type: NestedStringNullableFilter }), // can be null
}),
});

type NestedStringFilterType = graphql.InputObjectType<{
equals: graphql.Arg<typeof graphql.String>;
in: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>;
notIn: graphql.Arg<graphql.ListType<graphql.NonNullType<typeof graphql.String>>>;
lt: graphql.Arg<typeof graphql.String>;
lte: graphql.Arg<typeof graphql.String>;
gt: graphql.Arg<typeof graphql.String>;
gte: graphql.Arg<typeof graphql.String>;
contains: graphql.Arg<typeof graphql.String>;
startsWith: graphql.Arg<typeof graphql.String>;
endsWith: graphql.Arg<typeof graphql.String>;
not: graphql.Arg<NestedStringFilterType>;
}>;

const NestedStringFilter: NestedStringFilterType = graphql.inputObject({
name: 'NestedStringFilter',
fields: () => ({
equals: graphql.arg({ type: graphql.String }),
in: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }),
notIn: graphql.arg({ type: graphql.list(graphql.nonNull(graphql.String)) }),
lt: graphql.arg({ type: graphql.String }),
lte: graphql.arg({ type: graphql.String }),
gt: graphql.arg({ type: graphql.String }),
gte: graphql.arg({ type: graphql.String }),
contains: graphql.arg({ type: graphql.String }),
startsWith: graphql.arg({ type: graphql.String }),
endsWith: graphql.arg({ type: graphql.String }),
not: graphql.arg({ type: NestedStringFilter }),
}),
});

Expand Down
33 changes: 26 additions & 7 deletions prisma-utils/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ${
`;
}

function generateTSType(scalar: Scalar, filter: DMMF.InputType) {
function generateTSType(scalar: Scalar, filter: DMMF.InputType, nesting: boolean = false) {
const gqlType = GRAPHQL_SCALARS[scalar];

// we use Boolean, Prisma uses Bool, oh well
Expand All @@ -69,6 +69,7 @@ function generateTSType(scalar: Scalar, filter: DMMF.InputType) {
const suffix = field.isNullable ? ` // can be null` : ``;

if (field.name === 'not') {
if (nesting) return ` ${field.name}: graphql.Arg<Nested${filterName}Type>;${suffix}`;
return ` ${field.name}: graphql.Arg<${filterName}Type>;${suffix}`;
}

Expand All @@ -86,7 +87,7 @@ function generateTSType(scalar: Scalar, filter: DMMF.InputType) {
].join('\n');
}

function generateGQLType(scalar: Scalar, filter: DMMF.InputType) {
function generateGQLType(scalar: Scalar, filter: DMMF.InputType, nesting: boolean = false) {
const gqlType = GRAPHQL_SCALARS[scalar];

// we use Boolean, Prisma uses Bool, oh well
Expand All @@ -104,6 +105,9 @@ function generateGQLType(scalar: Scalar, filter: DMMF.InputType) {
}

if (field.name === 'not') {
if (nesting) {
return ` ${field.name}: graphql.arg({ type: Nested${filterName} }),${suffix}`;
}
return ` ${field.name}: graphql.arg({ type: ${filterName} }),${suffix}`;
}

Expand All @@ -130,15 +134,30 @@ async function generate(provider: Provider) {
const prismaScalar = scalar === 'Boolean' ? 'Bool' : scalar;

for (const filter of prismaFilterTypes) {
// why? for String, the case insensitivity mode is not recursively supported
const nesting = scalar === 'String';

if (filter.name === `${prismaScalar}Filter`) {
filters.push(generateTSType(scalar, filter));
filters.push(generateGQLType(scalar, filter));
filters.push(generateTSType(scalar, filter, nesting));
filters.push(generateGQLType(scalar, filter, nesting));
}

if (filter.name === `${prismaScalar}NullableFilter`) {
filters.push(generateTSType(scalar, filter));
filters.push(generateGQLType(scalar, filter));
}

if (nesting) {
if (filter.name === `Nested${prismaScalar}Filter`) {
filters.push(generateTSType(scalar, filter));
filters.push(generateGQLType(scalar, filter));
}

if (filter.name === `Nested${prismaScalar}NullableFilter`) {
filters.push(generateTSType(scalar, filter));
filters.push(generateGQLType(scalar, filter));
}
}
}

exports_.push(
Expand All @@ -156,9 +175,9 @@ async function generate(provider: Provider) {
`import { graphql } from '../../../types/schema';`,

// case sensitivity is only supported for POSTGRES
...(provider === 'postgresql' ? [
`import { QueryMode } from '../../../types/next-fields';`,
] : []),
...(provider === 'postgresql'
? [`import { QueryMode } from '../../../types/next-fields';`]
: []),

...filters,
...exports_,
Expand Down

0 comments on commit 8d136fc

Please sign in to comment.