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

Using multiple @ArgsType() classes combines those arguments into one object and assigns it to all parameters which are an @ArgsType #3085

Closed
2 of 4 tasks
NilsMoller opened this issue Nov 22, 2023 · 1 comment

Comments

@NilsMoller
Copy link

NilsMoller commented Nov 22, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When using multiple parameters in a resolver (code-first), having multiple parameters which are @ArgsType() classes, they combine the objects together and assign the same object to all @ArgsType parameters.

Minimum reproduction code

https://github.com/NilsMoller/multiple-args-type-params-combine-minimum-repro

Steps to reproduce

  1. Run nest new
  2. Run npm i @nestjs/graphql @nestjs/apollo @apollo/server graphql
  3. Modify app.module.ts:
@Module({
  imports: [
    GraphQLModule.forRoot<ApolloDriverConfig>({
      driver: ApolloDriver,
      autoSchemaFile: true,
    }),
  ],
  controllers: [AppController],
  providers: [AppService, AppResolver],
})
export class AppModule {}
  1. Create an app.resolver.ts class:
@ArgsType()
export class Args1 {
  @Field() prop1: string;
}

@ArgsType()
export class Args2 {
  @Field() prop2: string;
}

@Resolver()
export class AppResolver {
  @Query(() => String)
  hello(@Args() arg1: Args1, @Args() arg2: Args2) {
    return 'Hello World!';
  }
}
  1. Start the app and breakpoint on return 'Hello World!' and see both arg1 and arg2 to be the same object.
arg1 = Object { prop1: "prop1", prop2: "prop2" }
arg2 = Object { prop1: "prop1", prop2: "prop2" }

Expected behavior

To have the arguments receive the same object structure as their class defines:

arg1 = Object { prop1: "prop1" }
arg2 = Object { prop2: "prop2" }

This would cause problems when both classes have the same key. In that case, I would expect to have one override the other (perhaps the last one passed), or an error to be thrown before the server starts.

Package version

^12.0.11

Graphql version

@apollo/server: ^4.9.5
@nestjs/apollo: ^12.0.11
graphql: ^16.8.1

NestJS version

^10.0.0

Node.js version

18.17.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kamilmysliwiec
Copy link
Member

You should never use multiple @Args() annotations in one method as there's only one args object in the context of a graphql resolver function, and Nest doesn't perform any pre-invocation object restructuring based on the class metadata due to performance reasons (+ it doesn't seem to be very useful/common use case anyway).

This is the expected behavior. Apologies if this caused any inconvenience!

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

No branches or pull requests

2 participants