MichalLytek / MichalLytek/typegraphql-prisma

Omitted fields cannot be initialized

Open
#418 5 comments 0 reactions 0 assignees View on GitHub
community question
Dominant language
TypeScript
Stars
918
Forks
130
PR merge metrics
No merged PRs in 30d

Description

**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 = 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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the issue with the shown Prisma Person model, the addStatus middleware, and the createOnePerson mutation. Inspect the generated CRUD resolver and resolver.args.data flow to determine why the added status is not received; done means status is stored as "active" without the missing-argument error.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
backend-api-design, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.