Skip to content

Commit

Permalink
Add checking if prisma is provided in context
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Feb 23, 2021
1 parent 3b4c3b8 commit 4311dce
Show file tree
Hide file tree
Showing 132 changed files with 589 additions and 480 deletions.
9 changes: 9 additions & 0 deletions experiments/prisma/generated/type-graphql/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ export function transformFields(fields: Record<string, any>): Record<string, any
);
}

export function getPrismaFromContext(context: any) {
const prismaClient = context.prisma;
if (!prismaClient) {
throw new Error("Unable to find Prisma Client in GraphQL context. Please provide it under the `context.prisma` key.");
}
return prismaClient;
}


Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { GraphQLResolveInfo } from "graphql";
import { AggregateCategoryArgs } from "./args/AggregateCategoryArgs";
import { Category } from "../../../models/Category";
import { AggregateCategory } from "../../outputs/AggregateCategory";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class AggregateCategoryResolver {
@TypeGraphQL.Query(_returns => AggregateCategory, {
nullable: false
})
async aggregateCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Info() info: GraphQLResolveInfo, @TypeGraphQL.Args() args: AggregateCategoryArgs): Promise<AggregateCategory> {
return ctx.prisma.category.aggregate({
return getPrismaFromContext(ctx).category.aggregate({
...args,
...transformFields(graphqlFields(info as any)),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { GroupByCategoryArgs } from "./args/GroupByCategoryArgs";
import { UpdateCategoryArgs } from "./args/UpdateCategoryArgs";
import { UpdateManyCategoryArgs } from "./args/UpdateManyCategoryArgs";
import { UpsertCategoryArgs } from "./args/UpsertCategoryArgs";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";
import { Category } from "../../../models/Category";
import { AffectedRowsOutput } from "../../outputs/AffectedRowsOutput";
import { AggregateCategory } from "../../outputs/AggregateCategory";
Expand All @@ -25,77 +25,77 @@ export class CategoryCrudResolver {
nullable: true
})
async category(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: FindUniqueCategoryArgs): Promise<Category | null> {
return ctx.prisma.category.findUnique(args);
return getPrismaFromContext(ctx).category.findUnique(args);
}

@TypeGraphQL.Query(_returns => Category, {
nullable: true
})
async findFirstCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: FindFirstCategoryArgs): Promise<Category | null> {
return ctx.prisma.category.findFirst(args);
return getPrismaFromContext(ctx).category.findFirst(args);
}

@TypeGraphQL.Query(_returns => [Category], {
nullable: false
})
async categories(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: FindManyCategoryArgs): Promise<Category[]> {
return ctx.prisma.category.findMany(args);
return getPrismaFromContext(ctx).category.findMany(args);
}

@TypeGraphQL.Mutation(_returns => Category, {
nullable: false
})
async createCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: CreateCategoryArgs): Promise<Category> {
return ctx.prisma.category.create(args);
return getPrismaFromContext(ctx).category.create(args);
}

@TypeGraphQL.Mutation(_returns => AffectedRowsOutput, {
nullable: false
})
async createManyCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: CreateManyCategoryArgs): Promise<AffectedRowsOutput> {
return ctx.prisma.category.createMany(args);
return getPrismaFromContext(ctx).category.createMany(args);
}

@TypeGraphQL.Mutation(_returns => Category, {
nullable: true
})
async deleteCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: DeleteCategoryArgs): Promise<Category | null> {
return ctx.prisma.category.delete(args);
return getPrismaFromContext(ctx).category.delete(args);
}

@TypeGraphQL.Mutation(_returns => Category, {
nullable: true
})
async updateCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: UpdateCategoryArgs): Promise<Category | null> {
return ctx.prisma.category.update(args);
return getPrismaFromContext(ctx).category.update(args);
}

@TypeGraphQL.Mutation(_returns => AffectedRowsOutput, {
nullable: false
})
async deleteManyCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: DeleteManyCategoryArgs): Promise<AffectedRowsOutput> {
return ctx.prisma.category.deleteMany(args);
return getPrismaFromContext(ctx).category.deleteMany(args);
}

@TypeGraphQL.Mutation(_returns => AffectedRowsOutput, {
nullable: false
})
async updateManyCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: UpdateManyCategoryArgs): Promise<AffectedRowsOutput> {
return ctx.prisma.category.updateMany(args);
return getPrismaFromContext(ctx).category.updateMany(args);
}

@TypeGraphQL.Mutation(_returns => Category, {
nullable: false
})
async upsertCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: UpsertCategoryArgs): Promise<Category> {
return ctx.prisma.category.upsert(args);
return getPrismaFromContext(ctx).category.upsert(args);
}

@TypeGraphQL.Query(_returns => AggregateCategory, {
nullable: false
})
async aggregateCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Info() info: GraphQLResolveInfo, @TypeGraphQL.Args() args: AggregateCategoryArgs): Promise<AggregateCategory> {
return ctx.prisma.category.aggregate({
return getPrismaFromContext(ctx).category.aggregate({
...args,
...transformFields(graphqlFields(info as any)),
});
Expand All @@ -108,7 +108,7 @@ export class CategoryCrudResolver {
const { count, avg, sum, min, max } = transformFields(
graphqlFields(info as any)
);
return ctx.prisma.category.groupBy({
return getPrismaFromContext(ctx).category.groupBy({
...args,
...Object.fromEntries(
Object.entries({ count, avg, sum, min, max }).filter(([_, v]) => v != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as TypeGraphQL from "type-graphql";
import { CreateCategoryArgs } from "./args/CreateCategoryArgs";
import { Category } from "../../../models/Category";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class CreateCategoryResolver {
@TypeGraphQL.Mutation(_returns => Category, {
nullable: false
})
async createCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: CreateCategoryArgs): Promise<Category> {
return ctx.prisma.category.create(args);
return getPrismaFromContext(ctx).category.create(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import * as TypeGraphQL from "type-graphql";
import { CreateManyCategoryArgs } from "./args/CreateManyCategoryArgs";
import { Category } from "../../../models/Category";
import { AffectedRowsOutput } from "../../outputs/AffectedRowsOutput";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class CreateManyCategoryResolver {
@TypeGraphQL.Mutation(_returns => AffectedRowsOutput, {
nullable: false
})
async createManyCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: CreateManyCategoryArgs): Promise<AffectedRowsOutput> {
return ctx.prisma.category.createMany(args);
return getPrismaFromContext(ctx).category.createMany(args);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as TypeGraphQL from "type-graphql";
import { DeleteCategoryArgs } from "./args/DeleteCategoryArgs";
import { Category } from "../../../models/Category";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class DeleteCategoryResolver {
@TypeGraphQL.Mutation(_returns => Category, {
nullable: true
})
async deleteCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: DeleteCategoryArgs): Promise<Category | null> {
return ctx.prisma.category.delete(args);
return getPrismaFromContext(ctx).category.delete(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import * as TypeGraphQL from "type-graphql";
import { DeleteManyCategoryArgs } from "./args/DeleteManyCategoryArgs";
import { Category } from "../../../models/Category";
import { AffectedRowsOutput } from "../../outputs/AffectedRowsOutput";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class DeleteManyCategoryResolver {
@TypeGraphQL.Mutation(_returns => AffectedRowsOutput, {
nullable: false
})
async deleteManyCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: DeleteManyCategoryArgs): Promise<AffectedRowsOutput> {
return ctx.prisma.category.deleteMany(args);
return getPrismaFromContext(ctx).category.deleteMany(args);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as TypeGraphQL from "type-graphql";
import { FindFirstCategoryArgs } from "./args/FindFirstCategoryArgs";
import { Category } from "../../../models/Category";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class FindFirstCategoryResolver {
@TypeGraphQL.Query(_returns => Category, {
nullable: true
})
async findFirstCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: FindFirstCategoryArgs): Promise<Category | null> {
return ctx.prisma.category.findFirst(args);
return getPrismaFromContext(ctx).category.findFirst(args);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as TypeGraphQL from "type-graphql";
import { FindManyCategoryArgs } from "./args/FindManyCategoryArgs";
import { Category } from "../../../models/Category";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class FindManyCategoryResolver {
@TypeGraphQL.Query(_returns => [Category], {
nullable: false
})
async categories(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: FindManyCategoryArgs): Promise<Category[]> {
return ctx.prisma.category.findMany(args);
return getPrismaFromContext(ctx).category.findMany(args);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as TypeGraphQL from "type-graphql";
import { FindUniqueCategoryArgs } from "./args/FindUniqueCategoryArgs";
import { Category } from "../../../models/Category";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class FindUniqueCategoryResolver {
@TypeGraphQL.Query(_returns => Category, {
nullable: true
})
async category(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: FindUniqueCategoryArgs): Promise<Category | null> {
return ctx.prisma.category.findUnique(args);
return getPrismaFromContext(ctx).category.findUnique(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GraphQLResolveInfo } from "graphql";
import { GroupByCategoryArgs } from "./args/GroupByCategoryArgs";
import { Category } from "../../../models/Category";
import { CategoryGroupBy } from "../../outputs/CategoryGroupBy";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class GroupByCategoryResolver {
Expand All @@ -15,7 +15,7 @@ export class GroupByCategoryResolver {
const { count, avg, sum, min, max } = transformFields(
graphqlFields(info as any)
);
return ctx.prisma.category.groupBy({
return getPrismaFromContext(ctx).category.groupBy({
...args,
...Object.fromEntries(
Object.entries({ count, avg, sum, min, max }).filter(([_, v]) => v != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as TypeGraphQL from "type-graphql";
import { UpdateCategoryArgs } from "./args/UpdateCategoryArgs";
import { Category } from "../../../models/Category";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class UpdateCategoryResolver {
@TypeGraphQL.Mutation(_returns => Category, {
nullable: true
})
async updateCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: UpdateCategoryArgs): Promise<Category | null> {
return ctx.prisma.category.update(args);
return getPrismaFromContext(ctx).category.update(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import * as TypeGraphQL from "type-graphql";
import { UpdateManyCategoryArgs } from "./args/UpdateManyCategoryArgs";
import { Category } from "../../../models/Category";
import { AffectedRowsOutput } from "../../outputs/AffectedRowsOutput";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class UpdateManyCategoryResolver {
@TypeGraphQL.Mutation(_returns => AffectedRowsOutput, {
nullable: false
})
async updateManyCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: UpdateManyCategoryArgs): Promise<AffectedRowsOutput> {
return ctx.prisma.category.updateMany(args);
return getPrismaFromContext(ctx).category.updateMany(args);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as TypeGraphQL from "type-graphql";
import { UpsertCategoryArgs } from "./args/UpsertCategoryArgs";
import { Category } from "../../../models/Category";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Category)
export class UpsertCategoryResolver {
@TypeGraphQL.Mutation(_returns => Category, {
nullable: false
})
async upsertCategory(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Args() args: UpsertCategoryArgs): Promise<Category> {
return ctx.prisma.category.upsert(args);
return getPrismaFromContext(ctx).category.upsert(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { GraphQLResolveInfo } from "graphql";
import { AggregateClientArgs } from "./args/AggregateClientArgs";
import { Client } from "../../../models/Client";
import { AggregateClient } from "../../outputs/AggregateClient";
import { transformFields } from "../../../helpers";
import { transformFields, getPrismaFromContext } from "../../../helpers";

@TypeGraphQL.Resolver(_of => Client)
export class AggregateClientResolver {
@TypeGraphQL.Query(_returns => AggregateClient, {
nullable: false
})
async aggregateClient(@TypeGraphQL.Ctx() ctx: any, @TypeGraphQL.Info() info: GraphQLResolveInfo, @TypeGraphQL.Args() args: AggregateClientArgs): Promise<AggregateClient> {
return ctx.prisma.user.aggregate({
return getPrismaFromContext(ctx).user.aggregate({
...args,
...transformFields(graphqlFields(info as any)),
});
Expand Down
Loading

0 comments on commit 4311dce

Please sign in to comment.