Skip to content

Commit

Permalink
feat: update typegraphql-prisma-nestjs to 0.2600.1
Browse files Browse the repository at this point in the history
  • Loading branch information
EndyKaufman committed Jul 10, 2023
1 parent 6199eb6 commit c43118e
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 50 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,51 @@ Result
}
```

### Mutation by Prisma2 + TypeGraphQL generator + use data from headers in data for insert to prisma = Generated Crud

Rule:

```ts
setTransformArgsIntoPrismaArgs((info: GraphQLResolveInfo, args: any, ctx: any) => {
if (info.fieldName === UserCrudResolver.prototype.createOneUser.name && ctx.req.headers.email) {
(args as CreateOneUserArgs).data.email = ctx.req.headers.email;
}
return args;
});
```

Query

```graphql
mutation {
createOneUser(data: { username: "user", email: "user@user.com", password: "secret" }) {
email
username
password
}
}
```

Http headers:

```js
{"email":"newnew"}
```

Result

```js
{
"data": {
"createUser": {
"email": "newnew",
"username": "user",
"password": "secret"
}
}
}
```

## Links

https://github.com/nestjs/nest/tree/master/sample/23-type-graphql
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"ts-loader": "9.2.2",
"ts-node": "10.0.0",
"tsconfig-paths": "3.9.0",
"typegraphql-prisma-nestjs": "0.2600.0",
"typegraphql-prisma-nestjs": "0.2600.1",
"typescript": "4.3.5"
}
}
13 changes: 11 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { Module, Provider } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { PrismaClient } from '@prisma/client';
import { crudResolvers, resolvers } from './dal';
import { GraphQLResolveInfo } from 'graphql';
import { CreateOneUserArgs, UserCrudResolver, crudResolvers } from './dal';
import { setTransformArgsIntoPrismaArgs } from './dal/helpers';
import { RecipesModule } from './recipes/recipes.module';

const prisma = new PrismaClient({
log: ['query'],
});

setTransformArgsIntoPrismaArgs((info: GraphQLResolveInfo, args: any, ctx: any) => {
if (info.fieldName === UserCrudResolver.prototype.createOneUser.name && ctx.req.headers.email) {
(args as CreateOneUserArgs).data.email = ctx.req.headers.email;
}
return args;
});

@Module({
imports: [
RecipesModule,
Expand All @@ -20,6 +29,6 @@ const prisma = new PrismaClient({
context: ({ req }) => ({ req, prisma }),
}),
],
providers: (crudResolvers as unknown) as Provider<any>[],
providers: crudResolvers as unknown as Provider<any>[],
})
export class AppModule {}
9 changes: 9 additions & 0 deletions src/dal/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ export function transformCountFieldIntoSelectRelationsCount(_count: object) {
}
}

export let transformArgsIntoPrismaArgs = function <TArgs = Record<string, any>, TContext = any>(info: GraphQLResolveInfo, args: TArgs, ctx: TContext): TArgs {
return args;
};

export function setTransformArgsIntoPrismaArgs(newTransformArgsIntoPrismaArgs: typeof transformArgsIntoPrismaArgs) {
transformArgsIntoPrismaArgs = newTransformArgsIntoPrismaArgs;
}





4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/AggregateUserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { GraphQLResolveInfo } from "graphql";
import { AggregateUserArgs } from "./args/AggregateUserArgs";
import { User } from "../../../models/User";
import { AggregateUser } from "../../outputs/AggregateUser";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class AggregateUserResolver {
Expand All @@ -12,7 +12,7 @@ export class AggregateUserResolver {
})
async aggregateUser(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: AggregateUserArgs): Promise<AggregateUser> {
return getPrismaFromContext(ctx).user.aggregate({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...transformInfoIntoPrismaArgs(info),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/CreateOneUserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Args, ArgsType, Context, Field, Float, ID, Info, InputType, Int, Mutati
import type { GraphQLResolveInfo } from "graphql";
import { CreateOneUserArgs } from "./args/CreateOneUserArgs";
import { User } from "../../../models/User";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class CreateOneUserResolver {
Expand All @@ -12,7 +12,7 @@ export class CreateOneUserResolver {
async createOneUser(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: CreateOneUserArgs): Promise<User> {
const { _count } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.create({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/DeleteManyUserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { GraphQLResolveInfo } from "graphql";
import { DeleteManyUserArgs } from "./args/DeleteManyUserArgs";
import { User } from "../../../models/User";
import { AffectedRowsOutput } from "../../outputs/AffectedRowsOutput";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class DeleteManyUserResolver {
Expand All @@ -13,7 +13,7 @@ export class DeleteManyUserResolver {
async deleteManyUser(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: DeleteManyUserArgs): Promise<AffectedRowsOutput> {
const { _count } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.deleteMany({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/DeleteOneUserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Args, ArgsType, Context, Field, Float, ID, Info, InputType, Int, Mutati
import type { GraphQLResolveInfo } from "graphql";
import { DeleteOneUserArgs } from "./args/DeleteOneUserArgs";
import { User } from "../../../models/User";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class DeleteOneUserResolver {
Expand All @@ -12,7 +12,7 @@ export class DeleteOneUserResolver {
async deleteOneUser(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: DeleteOneUserArgs): Promise<User | null> {
const { _count } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.delete({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/FindFirstUserOrThrowResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Args, ArgsType, Context, Field, Float, ID, Info, InputType, Int, Mutati
import type { GraphQLResolveInfo } from "graphql";
import { FindFirstUserOrThrowArgs } from "./args/FindFirstUserOrThrowArgs";
import { User } from "../../../models/User";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class FindFirstUserOrThrowResolver {
Expand All @@ -12,7 +12,7 @@ export class FindFirstUserOrThrowResolver {
async findFirstUserOrThrow(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: FindFirstUserOrThrowArgs): Promise<User | null> {
const { _count } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.findFirstOrThrow({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/FindFirstUserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Args, ArgsType, Context, Field, Float, ID, Info, InputType, Int, Mutati
import type { GraphQLResolveInfo } from "graphql";
import { FindFirstUserArgs } from "./args/FindFirstUserArgs";
import { User } from "../../../models/User";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class FindFirstUserResolver {
Expand All @@ -12,7 +12,7 @@ export class FindFirstUserResolver {
async findFirstUser(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: FindFirstUserArgs): Promise<User | null> {
const { _count } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.findFirst({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/FindManyUserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Args, ArgsType, Context, Field, Float, ID, Info, InputType, Int, Mutati
import type { GraphQLResolveInfo } from "graphql";
import { FindManyUserArgs } from "./args/FindManyUserArgs";
import { User } from "../../../models/User";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class FindManyUserResolver {
Expand All @@ -12,7 +12,7 @@ export class FindManyUserResolver {
async users(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: FindManyUserArgs): Promise<User[]> {
const { _count } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.findMany({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/FindUniqueUserOrThrowResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Args, ArgsType, Context, Field, Float, ID, Info, InputType, Int, Mutati
import type { GraphQLResolveInfo } from "graphql";
import { FindUniqueUserOrThrowArgs } from "./args/FindUniqueUserOrThrowArgs";
import { User } from "../../../models/User";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class FindUniqueUserOrThrowResolver {
Expand All @@ -12,7 +12,7 @@ export class FindUniqueUserOrThrowResolver {
async getUser(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: FindUniqueUserOrThrowArgs): Promise<User | null> {
const { _count } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.findUniqueOrThrow({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/FindUniqueUserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Args, ArgsType, Context, Field, Float, ID, Info, InputType, Int, Mutati
import type { GraphQLResolveInfo } from "graphql";
import { FindUniqueUserArgs } from "./args/FindUniqueUserArgs";
import { User } from "../../../models/User";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class FindUniqueUserResolver {
Expand All @@ -12,7 +12,7 @@ export class FindUniqueUserResolver {
async user(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: FindUniqueUserArgs): Promise<User | null> {
const { _count } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.findUnique({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/GroupByUserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { GraphQLResolveInfo } from "graphql";
import { GroupByUserArgs } from "./args/GroupByUserArgs";
import { User } from "../../../models/User";
import { UserGroupBy } from "../../outputs/UserGroupBy";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class GroupByUserResolver {
Expand All @@ -13,7 +13,7 @@ export class GroupByUserResolver {
async groupByUser(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: GroupByUserArgs): Promise<UserGroupBy[]> {
const { _count, _avg, _sum, _min, _max } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.groupBy({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...Object.fromEntries(
Object.entries({ _count, _avg, _sum, _min, _max }).filter(([_, v]) => v != null)
),
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/UpdateManyUserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { GraphQLResolveInfo } from "graphql";
import { UpdateManyUserArgs } from "./args/UpdateManyUserArgs";
import { User } from "../../../models/User";
import { AffectedRowsOutput } from "../../outputs/AffectedRowsOutput";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class UpdateManyUserResolver {
Expand All @@ -13,7 +13,7 @@ export class UpdateManyUserResolver {
async updateManyUser(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: UpdateManyUserArgs): Promise<AffectedRowsOutput> {
const { _count } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.updateMany({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/UpdateOneUserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Args, ArgsType, Context, Field, Float, ID, Info, InputType, Int, Mutati
import type { GraphQLResolveInfo } from "graphql";
import { UpdateOneUserArgs } from "./args/UpdateOneUserArgs";
import { User } from "../../../models/User";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class UpdateOneUserResolver {
Expand All @@ -12,7 +12,7 @@ export class UpdateOneUserResolver {
async updateOneUser(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: UpdateOneUserArgs): Promise<User | null> {
const { _count } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.update({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dal/resolvers/crud/User/UpsertOneUserResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Args, ArgsType, Context, Field, Float, ID, Info, InputType, Int, Mutati
import type { GraphQLResolveInfo } from "graphql";
import { UpsertOneUserArgs } from "./args/UpsertOneUserArgs";
import { User } from "../../../models/User";
import { transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";
import { transformArgsIntoPrismaArgs, transformInfoIntoPrismaArgs, getPrismaFromContext, transformCountFieldIntoSelectRelationsCount } from "../../../helpers";

@Resolver(_of => User)
export class UpsertOneUserResolver {
Expand All @@ -12,7 +12,7 @@ export class UpsertOneUserResolver {
async upsertOneUser(@Context() ctx: any, @Info() info: GraphQLResolveInfo, @Args() args: UpsertOneUserArgs): Promise<User> {
const { _count } = transformInfoIntoPrismaArgs(info);
return getPrismaFromContext(ctx).user.upsert({
...args,
...transformArgsIntoPrismaArgs(info, args, ctx),
...(_count && transformCountFieldIntoSelectRelationsCount(_count)),
});
}
Expand Down
Loading

0 comments on commit c43118e

Please sign in to comment.