MichalLytek / MichalLytek/typegraphql-prisma

Generating correct return types for prisma actions resolver methods

Open
#107 3 comments 1 reaction 0 assignees View on GitHub
enhancement priority:low
Dominant language
TypeScript
Stars
918
Forks
130
PR merge metrics
No merged PRs in 30d

Description

**Describe the Bug**

For example, the generated aggregate return type is incorrect. Currently it could be something like:

```typescript
AggregatePost {
count: PostCountAggregate | null;
avg: PostAvgAggregate | null;
sum: PostSumAggregate | null;
min: PostMinAggregate | null;
max: PostMaxAggregate | null;
}
```

But when typing `ctx` correctly instead of as `any`, thus correctly typing `prisma`, using it like so

```typescript
@Query(() => AggregatePost, { nullable: false })
async aggregatePost(
@Ctx() { prisma }: Context,
@Info() info: GraphQLResolveInfo,
@Args() args: AggregatePostArgs,
): Promise {
const aggregation = await prisma.post.aggregate({
...args,
...transformFields(graphqlFields(info)),
});

return aggregation;
}
```

results in the following error:

```
Type 'GetPostAggregateType<{ where?: PostWhereInput | undefined; orderBy?: PostOrderByInput[] | undefined; cursor?: PostWhereUniqueInput | undefined; take?: number | undefined; skip?: number | undefined; }>' is missing the following properties from type 'AggregatePost': count, avg, sum, min, max
```

**To Reproduce**

1. Generate resolvers
2. Type `ctx` properly instead of as `any`, or correctly type return type of `getPrismaFromContext`
3. Return type does not match

Same happens for generated relation resolvers (e.g. author of post) since Prisma's `findUnique` can return `null`. Also `groupBy` seems to have mismatched typing as well.

**Expected Behavior**

The return type of the generated resolvers should match what is returned by the Prisma client.

**Environment (please complete the following information):**

- OS: macOS Big Sur
- Node: v14.16.1
- `typegraphql-prisma` version: `^0.13.1`
- Prisma 2 version: `~2.20.1`
- TypeScript version: `^4.2.3`

**Additional Context**

I am probably not using `typegraphql-prisma` as most people because I am mostly using the generated types, models and helpers and using them to create my own resolvers instead of importing the generated resolvers directly, so maybe my use case is not relevant to others but I hope this is useful nevertheless.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the generated resolvers and the Prisma action return types, comparing aggregate, relation, and groupBy methods, including getPrismaFromContext. Confirm that generated resolver return types match the values returned by the Prisma client, then reproduce the typed ctx example to verify the mismatch is resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api, backend
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.