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

can't use auto generated types in graphql resolvers #184

Open
esmaeilzadeh opened this issue Aug 7, 2023 · 7 comments
Open

can't use auto generated types in graphql resolvers #184

esmaeilzadeh opened this issue Aug 7, 2023 · 7 comments

Comments

@esmaeilzadeh
Copy link

esmaeilzadeh commented Aug 7, 2023

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?

@unlight
Copy link
Owner

unlight commented Aug 7, 2023

Are you using swc as a typescript transpiler?

@esmaeilzadeh
Copy link
Author

esmaeilzadeh commented Aug 8, 2023 via email

@esmaeilzadeh
Copy link
Author

esmaeilzadeh commented Aug 9, 2023

@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)

@jasonmacdonald
Copy link

@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.

@Kuro091
Copy link

Kuro091 commented Sep 8, 2023

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

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

@vanics
Copy link

vanics commented Apr 21, 2024

Same problem here. Using swc

@VictorBasso36
Copy link

VictorBasso36 commented May 27, 2024

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
    });
  }
  
}

 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants