MichalLytek / typegraphql-prisma

Prisma generator to emit TypeGraphQL types and CRUD resolvers from your Prisma schema

Home Page:https://prisma.typegraphql.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Omitted fields cannot be initialized

mdesousa opened this issue · comments

Describe the Bug
I have a model with a required property that should be initialized when the object is created.
To achieve this, we omit the field from the model and set the value in a resolver enhancer.
The enhancer initializes the value in resolver.args.data correctly. However the generated CRUD resolver is not receiving the object with the added properties and the prisma query fails.

Thanks!

To Reproduce
Define a prisma model like this:

model Person {
  id             String   @id @db.Uuid
  first_name     String
  last_name      String
  /// @TypeGraphQL.omit(input: ["create"])
  status         String
}

Then, initialize status in the ResolversEnhanceMap:

const addStatus: MiddlewareFn<GraphqlContext> = async (resolver, next) => {
  const { data } = resolver.args;
  const status = 'active';
  resolver.args.data = { ...data, status };
  return await next();
};

export const personResolversEnhanceMap: ResolversEnhanceMap = {
  Person: {
    createOnePerson: [UseMiddleware(addStatus)],
  },
};

Expected Behavior
When the createOnePerson mutation is invoked without the status, it should be initialized to "active" and stored in the database. However, the prisma query fails with an error because the status property is not set. It throws an error that says Argument status for data.status is missing.

Logs
If I add a console.log to print the value of args in the generated crud resolver, it shows that status is not set:

{
  data: {
    id: '2d1213e5-4c81-4078-a3f5-f0b0d57fe485',
    first_name: 'Joe',
    last_name: 'Michaels'
  }
}

Environment (please complete the following information):

  • OS: MacOS
  • Node 18.15.0
  • typegraphql-prisma version typegraphql-prisma@0.25.1
  • Prisma version 4.15.0
  • TypeScript version 5.2.2

Please create a minimal reproducible code example repository.
From the provided snippets, I don't see what's wrong and why it might be not working.