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

Class Transformer not working with ArgsType fields #3311

Open
3 of 4 tasks
kpervin opened this issue Sep 5, 2024 · 1 comment
Open
3 of 4 tasks

Class Transformer not working with ArgsType fields #3311

kpervin opened this issue Sep 5, 2024 · 1 comment

Comments

@kpervin
Copy link

kpervin commented Sep 5, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

If I define an ArgsType:

@ArgsType()
class ExampleArgs {
  @Field(() => String)
  @Transform(({ value }) => Number(value))
  number: number;
}

and use it in a resolver:

@Resolver()
export class ExampleResolver {
  @Query(() => Boolean)
  exampleQuery(
    @Args() args: ExampleArgs
  ): boolean {
    console.log(args);
    return true;
  }
}

and then attempt the query with params { number: "1" }, the transform function runs but does not actually serialize. Instead, we simply get the sent input.

Minimum reproduction code

https://stackblitz.com/edit/request-context-nestjs-issue?file=src%2Fapp.module.ts

Steps to reproduce

  1. Open Stackblitz link and wait for project launch.
  2. Navigate to the GQL playground
  3. Enter the following query:
query ExampleQuery($number: String!, $trueNumber: Float!) {
  exampleQuery(number: $number, trueNumber: $trueNumber) {
    number
    trueNumber
  }
}

{
  "number": "1",
  "trueNumber": 1
}
  1. View the console logs on the server showing the discrepancy:
    image

Expected behavior

The args should be serialized, such that given the above example:

Input: { number: "1" }
Output: { number: 1 }

This is expected due to this response in another issue.

Package version

@nestjs/apollo: ^12.2.0

Graphql version

graphql: ^16.9.0
@apollo/server: ^4.11.0
class-transformer: ^0.5.1

NestJS version

^10.4.1

Node.js version

20.9.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kpervin
Copy link
Author

kpervin commented Sep 6, 2024

The main reason for attempting to use @Transform here is because of not being able to use Field-level Pipes or field-middleware if I want to create an ArgsType class.

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

1 participant