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

ResolveField ignores field middleware #2587

Open
2 of 4 tasks
equal-matt opened this issue Jan 10, 2023 · 5 comments
Open
2 of 4 tasks

ResolveField ignores field middleware #2587

equal-matt opened this issue Jan 10, 2023 · 5 comments

Comments

@equal-matt
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When resolving a field with @ResolveField, any middleware defined on the corresponding ObjectType's @Field definition is ignored.

This is particularly annoying when a subset of fields for an object are resolved. Without also finding the resolver for the field that you're looking at, there is no guarantee that your middleware will execute as specified. To handle middleware-like transformations consistently, you are forced to handle all middleware-like transformations in field resolvers by hand.

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-rkgnvv?file=src/example/example.resolver.ts&view=editor

Steps to reproduce

Use the GraphQL playground in the preview section to query for

query {
  allExamples {
    fieldMiddlewareField
    resolveMiddlewareField
    bothMiddlewareField
  }
}

Expected behavior

Middleware defined in an ObjectType's @Field decorator should apply to the returned values from the @ResolveField handler.

Package version

10.1.7

Graphql version

"@nestjs/apollo": "^10.1.7",
"@nestjs/graphql": "^10.1.7",
"@nestjs/platform-express": "^9.0.0",
"apollo-server-express": "^3.11.1",
"graphql": "^16.6.0",

NestJS version

9.2.1

Node.js version

16.14.2

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@choco14t
Copy link
Contributor

@equal-matt
I tried to fix example.resolver.ts, field middleware works correctly.
I don't know details, it seems that returnType in @ResolveField needs to define type.

import { Query, ResolveField, Resolver } from '@nestjs/graphql';
import { Example } from './example.model';

@Resolver((_) => Example)
export class ExampleResolver {
  @Query(() => [Example])
  allExamples() {
    return [{ fieldMiddlewareField: '  will be trimmed  ' }];
  }

  @ResolveField(() => String, {
    middleware: [async (_, next) => (await next()).trim()],
  })
  resolveMiddlewareField() {
    return "  won't be trimmed  ";
  }

  @ResolveField(() => String, {
    middleware: [async (_, next) => (await next()).trim()],
  })
  bothMiddlewareField() {
    return "  still won't be trimmed  ";
  }
}

@equal-matt
Copy link
Author

Nice - can confirm that makes the field resolver middleware take precedence over the model middleware.

@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

@equal-matt
Copy link
Author

If I get a moment I will, but I'd encourage anyone else to pick it up if they have the time. I'm pretty swamped right now!

@troywweber7
Copy link

troywweber7 commented Jan 23, 2024

Not sure if it's related, but field middleware also doesn't appear to work when defined on an @InputType. e.g.

@InputType('SomeInput')
class SomeGqlInput {
    @Field(() => String, {
        description: 'The account email address.',
        nullable: false,
        // Field middleware in the below array doesn't seem to run
        middleware: [],
    })
    public email!: Email;
}

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

4 participants