prisma-nestjs-graphql icon indicating copy to clipboard operation
prisma-nestjs-graphql copied to clipboard

can't use auto generated types in graphql resolvers

Open esmaeilzadeh opened this issue 2 years ago • 8 comments

Hi im using prisma with nestjs and graphql, I am trying to use prisma-nestjs-graphql to auto generate the graphql type and use it inside my resolver:

import { PrismaService } from 'nestjs-prisma';
import {
  Resolver,
  Query,
  Parent,
  Args,
  ResolveField,
  Subscription,
  Mutation,
} from '@nestjs/graphql';
import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection';
import { PubSub } from 'graphql-subscriptions';
import { UseGuards } from '@nestjs/common';
import { PaginationArgs } from '../common/pagination/pagination.args';
import { UserEntity } from '../common/decorators/user.decorator';
import { User } from '../users/models/user.model';
import { GqlAuthGuard } from '../auth/gql-auth.guard';
import { PostIdArgs } from './args/post-id.args';
import { UserIdArgs } from './args/user-id.args';
import { Post } from './models/post.model';
import { PostConnection } from './models/post-connection.model';
import { PostOrder } from './dto/post-order.input';
import { CreatePostInput } from './dto/createPost.input';
import {FindManyPostArgs, PostWhereInput} from "../generated/post";
@Query(() => [Post])
async posts(
  @Args({ name: 'where', nullable: true, type: () => PostWhereInput})
  where?:PostWhereInput
): Promise<Post[]> {
      return this.prisma.post.findMany({
        where:where
      })
}

but I faced this error:

Projects/webnegah/backend_base/dist/generated/user/input/user-where.js:8
        return UserWhereInput;
        ^
ReferenceError: Cannot access 'UserWhereInput' before initialization
    at Object.get [as UserWhereInput] (/home/mohammadreza/Projects/webnegah/backend_base/dist/generated/user/input/user-where.js:8:9)
    at Object.<anonymous> (/home/mohammadreza/Projects/webnegah/backend_base/dist/generated/user/input/user-nullable-relation-filter.js:28:51)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (/home/mohammadreza/Projects/webnegah/backend_base/dist/generated/post/input/post-where.js:16:37)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)

do you have any idea how to solve this issue?

esmaeilzadeh avatar Aug 07 '23 12:08 esmaeilzadeh

Are you using swc as a typescript transpiler?

unlight avatar Aug 07 '23 18:08 unlight

No I don't explicitly use this but I am using this boilerplate: https://github.com/notiz-dev/nestjs-prisma-starter which as I know it doesn't use swc neither.

On Mon, Aug 7, 2023, 11:21 PM Roman Vasilev @.***> wrote:

Are you using swc as a typescript transpiler?

— Reply to this email directly, view it on GitHub https://github.com/unlight/prisma-nestjs-graphql/issues/184#issuecomment-1668415872, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABAVQOBSIEXQFDHR6WLBOXLXUE2LRANCNFSM6AAAAAA3G453Y4 . You are receiving this because you authored the thread.Message ID: @.***>

esmaeilzadeh avatar Aug 08 '23 01:08 esmaeilzadeh

@unlight I think it is related to how I am using these generated types inside my project, do you have any working example that shows how we should use these generated types inside our resolvers? (especially an example that shows how to get a where clause from user when there is a relationship between 2 objects)

esmaeilzadeh avatar Aug 09 '23 06:08 esmaeilzadeh

@esmaeilzadeh Assuming I understand your question, you might want to have a look at https://paljs.com/plugins/select. This is what we use to add the additional relations to our Prisma queries based on what is asked for by the GraphQL client. Otherwise, you need to use a @ResolveField in your resolver, in order to add the relations to your response, as per the NestJS docs.

jasonmacdonald avatar Sep 03 '23 21:09 jasonmacdonald

have you resolved this yet? Current facing the same issue.

Also with the https://github.com/notiz-dev/nestjs-prisma-starter boilerplate

Kuro091 avatar Sep 08 '23 08:09 Kuro091

Same problem here. Using swc

vanics avatar Apr 21 '24 10:04 vanics

já resolveu isso? Enfrentando o mesmo problema.

Também com o https://github.com/notiz-dev/nestjs-prisma-starter boilerplate

i'm using the same boilerplate, I solved the problem as follows:

  • go to nest-cli.json:
{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "deleteOutDir": true,
    "builder": "tsc", //remove swc
    "typeCheck": true,
    "plugins": ["@nestjs/graphql/plugin"]
  }
}

example resolver:

 import { PrismaService } from 'nestjs-prisma';
import {
  Resolver,
  Query,
  Args,
} from '@nestjs/graphql';
import { CompanyWhereInput } from 'src/@generated/company/company-where.input';
import { Company } from '../@generated/company/company.model';

@Resolver(() => Company)
export class CompanyResolver {
  constructor(
    private prisma: PrismaService,
  ) {}

  @Query(() => [Company])
  async companys(@Args('where') where: CompanyWhereInput) {
    return await this.prisma.company.findMany({
      where
    });
  }
  
}

 

VictorBasso36 avatar May 27 '24 00:05 VictorBasso36

We must use SWC as our large code-base compiles much faster with it. Why is this an issue with SWC and is there a work-around / solution for it in sight?

I found this issue in the swc repo: https://github.com/swc-project/swc/issues/6498

konsti avatar May 28 '25 15:05 konsti