Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omitted fields cannot be initialized #418

Open
mdesousa opened this issue Aug 31, 2023 · 2 comments
Open

Omitted fields cannot be initialized #418

mdesousa opened this issue Aug 31, 2023 · 2 comments
Labels
community Something initiated by the community question Further information is requested

Comments

@mdesousa
Copy link

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
@MichalLytek
Copy link
Owner

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.

@MichalLytek MichalLytek added question Further information is requested community Something initiated by the community labels Oct 18, 2023
@mdesousa
Copy link
Author

mdesousa commented Nov 6, 2023

hi @MichalLytek , here you go... hope it helps! https://github.com/holasivo/typegraphql-prisma-418

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Something initiated by the community question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants