Skip to content

Commit

Permalink
fix!: typing of args in customize resolve functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lubosmato committed Aug 3, 2022
1 parent b198952 commit 33b98bc
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lint-staged": ">=10",
"lodash": "^4.17.15",
"nexus": "^1.0.0",
"nexus-plugin-prisma": "^0.34.1",
"nexus-plugin-prisma": "^0.35.0",
"path": "^0.12.7",
"plain-tag": "^0.1.3",
"pluralize": "~7.0.0",
Expand Down
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 = false,
}: 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;
};
7 changes: 6 additions & 1 deletion packages/dataprovider/src/buildVariables/buildData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ const buildNewInputValue = (
(i) => i.name === ModifiersParams.delete,
);

if (setModifier && !connectModifier && !disconnectModifier && !deleteModifier) {
if (
setModifier &&
!connectModifier &&
!disconnectModifier &&
!deleteModifier
) {
// if its a date, convert it to a date
if (
setModifier.type.kind === "SCALAR" &&
Expand Down
12 changes: 7 additions & 5 deletions packages/dataprovider/src/testUtils/getTestIntrospection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { CommonOptions } from "../../../backend/src/types";
import { defaultOurOptions } from "../buildDataProvider";
import { testSchemaTypeGraphql } from "../../test-data/testSchemaTypeGraphql";

const buildIntrospection = async (
const buildIntrospection = async <Prefix extends string>(
rawSchema: GraphQLSchema,
options: CommonOptions,
options?: CommonOptions<Prefix>,
) => {
const schema = await graphql(rawSchema, getIntrospectionQuery()).then(
({ data: { __schema } }) => __schema,
Expand All @@ -20,7 +20,9 @@ const buildIntrospection = async (
}) as IntrospectionResult;
};

export const getTestIntrospectionNexus = async (options?: CommonOptions) => {
export const getTestIntrospectionNexus = async <Prefix extends string>(
options?: CommonOptions<Prefix>,
) => {
return await buildIntrospection(
testSchemaNexus({
...options,
Expand All @@ -30,8 +32,8 @@ export const getTestIntrospectionNexus = async (options?: CommonOptions) => {
);
};

export const getTestIntrospectionTypeGraphql = async (
options?: CommonOptions,
export const getTestIntrospectionTypeGraphql = async <Prefix extends string>(
options?: CommonOptions<Prefix>,
) => {
return await buildIntrospection(
await testSchemaTypeGraphql({
Expand Down
4 changes: 3 additions & 1 deletion packages/dataprovider/test-data/testSchemaNexus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const typegenPath = (p: string) => {
return process.env.PWD && join(process.env.PWD, p);
};

export const testSchemaNexus = (options: CommonOptions) => {
export const testSchemaNexus = <Prefix extends string>(
options: CommonOptions<Prefix>,
) => {
const User = objectType({
name: "User",
definition(t) {
Expand Down
4 changes: 3 additions & 1 deletion packages/dataprovider/test-data/testSchemaTypeGraphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class CustomUserResolver {
}
}

export const testSchemaTypeGraphql = async (options: CommonOptions) => {
export const testSchemaTypeGraphql = async <Prefix extends string>(
options: CommonOptions<Prefix>,
) => {
return buildSchema({
resolvers: [
CustomUserResolver,
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8205,10 +8205,10 @@ nerf-dart@^1.0.0:
resolved "https://registry.yarnpkg.com/nerf-dart/-/nerf-dart-1.0.0.tgz#e6dab7febf5ad816ea81cf5c629c5a0ebde72c1a"
integrity sha1-5tq3/r9a2Bbqgc9cYpxaDr3nLBo=

nexus-plugin-prisma@^0.34.1:
version "0.34.1"
resolved "https://registry.yarnpkg.com/nexus-plugin-prisma/-/nexus-plugin-prisma-0.34.1.tgz#a2a8680b6997c3854a61d64b7865354918cebc11"
integrity sha512-MeaBj95nI/nONPRQ73ayQsJkHEm3RSdZkcfgjV7eR0dqWPnqwpbuV2BI5rbyQmFFqr68rEu5j/jtd1Hn2IzSeg==
nexus-plugin-prisma@^0.35.0:
version "0.35.0"
resolved "https://registry.yarnpkg.com/nexus-plugin-prisma/-/nexus-plugin-prisma-0.35.0.tgz#ab725c88823ab38c18fed90cca72f05ee73f3425"
integrity sha512-zZh96Ol6eqDGGd2caZLayjDW5ruUxNdoglyAMa/bOTBfnxu1Keb9kOyD+n7zVV+Mot8t0GRvWS4XilXoKZF/Tg==
dependencies:
camelcase "^6.2.0"
endent "^2.0.1"
Expand Down

0 comments on commit 33b98bc

Please sign in to comment.