Skip to content

Commit

Permalink
Merge branch 'beta'
Browse files Browse the repository at this point in the history
  • Loading branch information
macrozone committed May 5, 2023
2 parents fae06e1 + 38d5db4 commit a5f4e5c
Show file tree
Hide file tree
Showing 28 changed files with 1,222 additions and 1,072 deletions.
4 changes: 3 additions & 1 deletion packages/backend/src/addCrudResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { extendType, arg, intArg } from "nexus";
import * as Helpers from "nexus-plugin-prisma/src/typegen/helpers";

const addCrudResolvers = <
AliasPrefix extends string,
ModelName extends keyof Helpers.GetGen<"outputs"> & string,
>(
resourceName: ModelName,
options?: ResourceOptions<ModelName> & CommonOptions,
options?: ResourceOptions<AliasPrefix, ModelName> &
CommonOptions<AliasPrefix>,
) => setupCrudResolvers({ extendType, arg, intArg }, resourceName, options);

export default addCrudResolvers;
3 changes: 2 additions & 1 deletion packages/backend/src/setupCrudResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const makePrefixedFullName = (name: string, prefix?: string) => {
};

const setupCrudResolvers = <
AliasPrefix extends string,
ModelName extends keyof Helpers.GetGen<"outputs"> & string,
>(
{ extendType, arg, intArg },
Expand All @@ -18,7 +19,7 @@ const setupCrudResolvers = <
customize,
aliasPrefix,
enableOrderByRelation = true,
}: ResourceOptions<ModelName> & CommonOptions = {},
}: ResourceOptions<AliasPrefix, ModelName> & CommonOptions<AliasPrefix> = {},
) => {
const typeName = upperFirst(resourceName);

Expand Down
67 changes: 47 additions & 20 deletions packages/backend/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,67 @@
import * as Helpers from "nexus-plugin-prisma/src/typegen/helpers";
import { GetNexusPrisma } from "nexus-plugin-prisma/src/typegen/static";
import { BaseRelationOptions } from "nexus-plugin-prisma/typegen";

type Arg0<T extends (...args: any) => any> = Parameters<T>[0];
type CustomizeOpt<T extends (...args: any) => any> = (
config: Exclude<Arg0<T>, undefined>,
) => Arg0<T>;
type Prefix<What extends string, Prefix extends string> = Prefix extends ``
? Uncapitalize<What>
: `${Prefix}${Capitalize<What>}`;

type QueryCrud = GetNexusPrisma<"Query", "crud">;
type MutationCrud = GetNexusPrisma<"Mutation", "crud">;
type MutationConfig<
MutationName extends string,
AliasPrefix extends string,
ModelName extends keyof Helpers.GetGen<"outputs"> & string,
> = BaseRelationOptions<
"Mutation",
Prefix<ModelName, MutationName>,
Prefix<Prefix<ModelName, MutationName>, AliasPrefix>,
ModelName
>;

export type Customize<
AliasPrefix extends string,
ModelName extends keyof Helpers.GetGen<"outputs"> & string,
ModelNamePlural = `${Uncapitalize<ModelName>}s`,
ModelNamePlural extends string = `${Uncapitalize<ModelName>}s`,
One = BaseRelationOptions<
"Query",
Uncapitalize<ModelName>,
Prefix<ModelName, AliasPrefix>,
ModelName
>,
Many = BaseRelationOptions<
"Query",
Uncapitalize<ModelNamePlural>,
Prefix<ModelNamePlural, AliasPrefix>,
ModelName
>,
CreateOne = MutationConfig<"createOne", AliasPrefix, ModelName>,
UpdateOne = MutationConfig<"updateOne", AliasPrefix, ModelName>,
UpdateMany = MutationConfig<"updateMany", AliasPrefix, ModelName>,
UpsertOne = MutationConfig<"upsertOne", AliasPrefix, ModelName>,
DeleteOne = MutationConfig<"deleteOne", AliasPrefix, ModelName>,
DeleteMany = MutationConfig<"deleteMany", AliasPrefix, ModelName>,
> = {
one?: CustomizeOpt<QueryCrud[Uncapitalize<ModelName>]>;
many?: CustomizeOpt<QueryCrud[ModelNamePlural]>;
createOne?: CustomizeOpt<MutationCrud[`createOne${Capitalize<ModelName>}`]>;
updateOne?: CustomizeOpt<MutationCrud[`updateOne${Capitalize<ModelName>}`]>;
updateMany?: CustomizeOpt<MutationCrud[`updateMany${Capitalize<ModelName>}`]>;
upsertOne?: CustomizeOpt<MutationCrud[`upsertOne${Capitalize<ModelName>}`]>;
deleteOne?: CustomizeOpt<MutationCrud[`deleteOne${Capitalize<ModelName>}`]>;
deleteMany?: CustomizeOpt<MutationCrud[`deleteMany${Capitalize<ModelName>}`]>;
one?: (config: One) => One;
many?: (config: Many) => Many;
createOne?: (config: CreateOne) => CreateOne;
updateOne?: (config: UpdateOne) => UpdateOne;
updateMany?: (config: UpdateMany) => UpdateMany;
upsertOne?: (config: UpsertOne) => UpsertOne;
deleteOne?: (config: DeleteOne) => DeleteOne;
deleteMany?: (config: DeleteMany) => DeleteMany;
};

export type CommonOptions = {
aliasPrefix?: string;
export type CommonOptions<AliasPrefix extends string> = {
aliasPrefix?: AliasPrefix;
enableOrderByRelation?: boolean;
};
export type ResourceOptions<
AliasPrefix extends string,
ModelName extends keyof Helpers.GetGen<"outputs"> & string,
> = {
/**
* whether to display a warning to secure the mutations and resolvers (defaults to true)
*/
printSecurityWarning?: boolean;
aliasPrefix?: string;
customize?: Customize<ModelName>;
aliasPrefix?: AliasPrefix;
customize?: Customize<AliasPrefix, ModelName>;
enableOrderByRelation?: boolean;
};
Loading

0 comments on commit a5f4e5c

Please sign in to comment.