typegraphql-prisma icon indicating copy to clipboard operation
typegraphql-prisma copied to clipboard

Omitted fields cannot be initialized

Open mdesousa opened this issue 10 months ago • 2 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 [email protected]
  • Prisma version 4.15.0
  • TypeScript version 5.2.2

mdesousa avatar Aug 31 '23 14:08 mdesousa