nestjs / graphql

GraphQL (TypeScript) module for Nest framework (node.js) 🍷
https://docs.nestjs.com/graphql/quick-start
MIT License
1.45k stars 391 forks source link

Params with the Args decorator are not decorated in resolvers after inheritance #3277

Closed andre-byrne closed 1 month ago

andre-byrne commented 1 month ago

Is there an existing issue for this?

Current behavior

When I create a new resolver by extending an existing resolver, fields that had Args decorators do not have the corresponding graphql params. For example, given:

import { Args, Int, Query, ResolveField, Resolver } from '@nestjs/graphql';
import { Author } from './models/author.model';

@Resolver((of) => Author)
export class AuthorsResolver {
  constructor() {}

  @Query((returns) => Author)
  async author(@Args('id', { type: () => Int }) id: number) {
    return { id };
  }

  @ResolveField()
  async posts(@Args('language') language: string) {
    return [{ id: 1 }];
  }
}

When I extend the resover like this,

import { Args, Int, Query, Resolver } from '@nestjs/graphql';
import { AuthorsResolver } from 'src/authors/authors.resolver';
import { Blog } from './models/blog.model';

@Resolver((of) => Blog)
export class BlogsResolver extends AuthorsResolver {
  constructor() {
    super();
  }

  @Query((returns) => Blog)
  async blog(@Args('id', { type: () => Int }) id: number) {
    return { id };
  }
}

Then I expect to be able to query posts like this

  blog(id:$id) {
    posts(language:"en") {
      id
    }
  }

Currently this is an error:

        "message": "Unknown argument \"language\" on field \"Blog.posts\".",

Minimum reproduction code

https://github.com/andre-byrne/args-not-inherited-example/tree/main

Steps to reproduce

  1. npm i
  2. npm run start
  3. in the playground try this:
query ($id: Int!) {
  author(id:$id) {

    posts(language:"en") {
      id
    }
  }

  blog(id:$id) {
    posts(language:"en") {
      id
    }
  }
}

You will see that the blog field does not permit the language argument.

Expected behavior

When I create a new resolver by extending an existing resolver, I expect all the fields from the parent class to be present in my new resolver just as they were in the parent. If the parent has a posts field with a language argument, then the child should have a posts field with a language argument.

Package version

12.2.0

Graphql version

    "@apollo/server": "^4.10.5",
    "@nestjs/apollo": "^12.2.0",
    "@nestjs/common": "^10.3.2",
    "@nestjs/core": "^10.3.2",
    "@nestjs/graphql": "^12.2.0",
    "@nestjs/platform-express": "^10.3.2",
    "graphql": "^16.9.0",
    "reflect-metadata": "^0.2.1",
    "rxjs": "^7.8.1"

NestJS version

10.3.2

Node.js version

20.14.0

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 1 month ago

Thank you for taking the time to submit your report! From the looks of it, this could be better discussed on our Discord. If you haven't already, please join here and send a new post in the #⁠ 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.