Skip to content

Commit

Permalink
add ability to configure field options for filter ops
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Aug 19, 2022
1 parent e7a3070 commit 148e135
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 51 deletions.
45 changes: 31 additions & 14 deletions packages/plugin-prisma-crud/src/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
FilterListOps,
FilterOps,
FilterShape,
OpsOptions,
PrismaFilterOptions,
PrismaListFilterOptions,
PrismaOrderByOptions,
Expand All @@ -28,20 +29,31 @@ const OrderByRefMap = new WeakMap<

schemaBuilder.prismaFilter = function prismaFilter<
Type extends InputType<SchemaTypes>,
Ops extends FilterOps,
Ops extends OpsOptions<SchemaTypes, Type, FilterOps>,
>(type: Type, { ops, name, ...options }: PrismaFilterOptions<SchemaTypes, Type, Ops>) {
const filterName = name ?? `${nameFromType(type, this)}Filter`;
const ref =
this.inputRef<Pick<FilterShape<InputShapeFromTypeParam<SchemaTypes, Type, true>>, Ops>>(
filterName,
);
this.inputRef<
Pick<
FilterShape<InputShapeFromTypeParam<SchemaTypes, Type, true>>,
Ops extends string[] ? Ops[number] : keyof Ops
>
>(filterName);

const opsOptions: Record<string, unknown> = Array.isArray(ops)
? ops.reduce<Record<string, {}>>((map, op) => {
// eslint-disable-next-line no-param-reassign
map[op] = {};
return map;
}, {})
: ops;

ref.implement({
...options,
fields: (t) => {
const fields: Record<string, InputFieldRef<unknown, 'InputObject'>> = {};

for (const op of ops) {
for (const op of Object.keys(opsOptions)) {
const isList = op === 'in' || op === 'notIn';
let fieldType: InputType<SchemaTypes> | [InputType<SchemaTypes>] = type;

Expand All @@ -54,19 +66,20 @@ schemaBuilder.prismaFilter = function prismaFilter<
fields[op] = t.field({
required: isList ? { list: false, items: true } : false,
type: fieldType,
...(opsOptions[op] as {}),
});
}

return fields as never;
},
});

return ref as InputRef<Pick<FilterShape<InputShapeFromTypeParam<SchemaTypes, Type, true>>, Ops>>;
return ref as never;
};

schemaBuilder.prismaListFilter = function prismaListFilter<
Type extends InputType<SchemaTypes>,
Ops extends FilterListOps,
Ops extends OpsOptions<SchemaTypes, Type, FilterListOps>,
>(type: Type, { name, ops, ...options }: PrismaListFilterOptions<SchemaTypes, Type, Ops>) {
let filterName = name;

Expand All @@ -78,29 +91,33 @@ schemaBuilder.prismaListFilter = function prismaListFilter<
: `List${typeName}`;
}

const ref = this.inputRef<{
[K in Ops]: InputShapeFromTypeParam<SchemaTypes, Type, true>;
}>(filterName);
const ref = this.inputRef(filterName);
const opsOptions: Record<string, unknown> = Array.isArray(ops)
? ops.reduce<Record<string, {}>>((map, op) => {
// eslint-disable-next-line no-param-reassign
map[op] = {};
return map;
}, {})
: ops;

ref.implement({
...options,
fields: (t) => {
const fields: Record<string, InputFieldRef<unknown, 'InputObject'>> = {};

for (const op of ops) {
for (const op of Object.keys(opsOptions)) {
fields[op] = t.field({
required: false,
type,
...(opsOptions[op] as {}),
});
}

return fields as never;
},
});

return ref as InputRef<{
[K in Ops]: InputShapeFromTypeParam<SchemaTypes, Type, true>;
}>;
return ref as never;
};

schemaBuilder.orderByEnum = function orderByEnum() {
Expand Down
9 changes: 1 addition & 8 deletions packages/plugin-prisma-crud/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
// Type map: https://github.com/prisma/prisma/blob/main/packages/client/src/runtime/utils/common.ts#L63

import {
BaseEnum,
InputRef,
InputShapeFromTypeParam,
InputType,
InputTypeParam,
SchemaTypes,
} from '@pothos/core';
import { BaseEnum, InputRef, InputType, SchemaTypes } from '@pothos/core';
import { PrismaModelTypes } from '@pothos/plugin-prisma';

export type ScalarFilters<T> = T extends { equals?: unknown }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prisma crud generates schema 1`] = `
"input IntFilter {
"input CommentWhere {
createdAt: DateTime
}
scalar DateTime
input IntFilter {
equals: Int
not: IntFilter
}
input ListCommentWhere {
some: CommentWhere
}
enum OrderBy {
asc
desc
Expand All @@ -22,6 +32,17 @@ input PostOrderBy {
title: OrderBy
}
input PostWhere {
author: UserWhere
\\"\\"\\"filter by author id\\"\\"\\"
authorId: IntFilter
comments: ListCommentWhere
createdAt: DateTime
id: IntFilter
title: String
}
input ProfileOrderBy {
bio: OrderBy
user: UserOrderBy
Expand All @@ -47,5 +68,9 @@ input StringListFilter {
input UserOrderBy {
name: OrderBy
profile: ProfileOrderBy
}
input UserWhere {
id: IntFilter
}"
`;
5 changes: 5 additions & 0 deletions packages/plugin-prisma-crud/tests/example/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const StringListFilter = builder.prismaListFilter(StringFilter, {
ops: ['every', 'some', 'none'],
});

builder.scalarType('DateTime', {
serialize: (value) => value.toISOString(),
parseValue: (value) => (typeof value === 'number' ? new Date(value) : new Date(String(value))),
});

builder.queryType({});

builder.queryField('post', (t) =>
Expand Down
29 changes: 1 addition & 28 deletions packages/plugin-prisma/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,12 @@ export type PrismaConnectionFieldOptions<
ResolveReturnShape
>,
'args' | 'resolve' | 'type'
<<<<<<< HEAD
> &
(InputShapeFromFields<Args> &
PothosSchemaTypes.DefaultConnectionArguments extends infer ConnectionArgs
? {
type: Type;
cursor: string & keyof Model['Where'];
cursor: string & keyof Model['WhereUnique'];
defaultSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number);
maxSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number);
resolve: (
Expand All @@ -527,32 +526,6 @@ export type PrismaConnectionFieldOptions<
) => MaybePromise<number>;
}
: never);
=======
> & {
type: Type;
cursor: string & keyof Model['WhereUnique'];
defaultSize?: number;
maxSize?: number;
resolve: (
query: {
include?: Model['Include'];
cursor?: {};
take: number;
skip: number;
},
parent: ParentShape,
args: InputShapeFromFields<Args> & PothosSchemaTypes.DefaultConnectionArguments,
context: Types['Context'],
info: GraphQLResolveInfo,
) => MaybePromise<Model['Shape'][]>;
totalCount?: (
parent: ParentShape,
args: InputShapeFromFields<Args> & PothosSchemaTypes.DefaultConnectionArguments,
context: Types['Context'],
info: GraphQLResolveInfo,
) => MaybePromise<number>;
};
>>>>>>> 67bda837 (wip)

export type RelatedConnectionOptions<
Types extends SchemaTypes,
Expand Down

0 comments on commit 148e135

Please sign in to comment.