Adrinalin4ik / Nestjs-Graphql-Tools

NestJS Graphql Tools is a flexible solution that provides a bunch of decorators for solving problems like n+1 request, filtering, sorting, pagination, polymorphic relation, graphql field extraction. It is fully based on decorators. To use it you can just add a decorator to your resolver.
GNU General Public License v3.0
80 stars 8 forks source link

Use library along with global pipes #22

Closed YannisSerli closed 1 year ago

YannisSerli commented 1 year ago

In the release 0.7.17, when we follow the readme adding @Filter() parameter with type of Brackets from typeorm library, an error is throw: where.whereFactory is not a function.

Changing the type Brackets into any solve the problems.

Adrinalin4ik commented 1 year ago

Hey! I'll take a look.

Adrinalin4ik commented 1 year ago

@YannisSerli can you give more information please. I want to know typeorm and graphql versions. Also attach some code snippet.

YannisSerli commented 1 year ago

The typeorm and graphql versions:

"@nestjs/typeorm": "^9.0.1" "@nestjs/graphql": "^10.1.7"

A code snippet that doesn't work:

  /**
   * Find all persons
   * @returns all persons
   */
  @Query(() => [Person], { name: 'persons' })
  @UseGuards(GqlAuthGuard, CoachResearcherGuard)
  @GraphqlFilter()
  @GraphqlSorting()
  async findAll(
    @Filter(() => Person) filter: Brackets,
    @Sorting(() => Person) sorting: SortArgs<Person>,
    @Paginator() paginator,
  ): Promise<Person[]> {
    const query = repository.createQueryBuilder();
    query.where(filter);
    query.orderBy(sorting);
    query.offset(paginator.page * paginator.per_page).limit(paginator.per_page);
    return query.getMany();
  }
Adrinalin4ik commented 1 year ago

@YannisSerli Can you please look at src/entities/user/user.resolver. There's no guards here. Probably UseGuards overrides all the rest guards. Can you please try to move you UseGuards after @GraphqlSorting()?

Adrinalin4ik commented 1 year ago

Another thing what you can do is to change the logic in GqlAuthGuard and CoachResearcherGuard. You need to call action and spread all parameters to it. Also, make sure that you don't override initial method there, because in this case you will loose the context.

YannisSerli commented 1 year ago

I moved UseGuards after @GraphqlSorting and even removed the UseGuards but the problem stay the same.

YannisSerli commented 1 year ago

I think the problem comes from lib/filters/query.builder.ts line 19: if (parameters && 'whereFactory' in parameters) return parameters;

Because using Brackets the if condition became true but whereFactory is undefined.

Adrinalin4ik commented 1 year ago

That's super strange and if you run example that inside of src folder it will work. You mentioned that having any type instead of bracket makes it works. At the same time types don't exist in the final bundle as it compiles to js. How it can break?

Adrinalin4ik commented 1 year ago

Closing due to inactivity.

KyDenZ commented 1 year ago

I've had the same problem since I added validations to bootstrap:

app.useGlobalPipes(new ValidationPipe({ transform: true }));

Adrinalin4ik commented 1 year ago

@KyDenZ Oh, it looks like global pipes override results of controllers pipes. I'll take a look. For now, you can try to use pipes locally wrapping the methods in the controller. The first pipe should be yours, and after all the following decorators that comes fom the library.

Adrinalin4ik commented 1 year ago

I was able to reproduce the issue, working on it.

Adrinalin4ik commented 1 year ago

@KyDenZ @YannisSerli I hope it was fixed in the version 0.8.0 https://github.com/Adrinalin4ik/Nestjs-Graphql-Tools/releases/tag/0.8.0