Skip to content

feat(graphql): export field metadata helper for custom mapped types - #4192

Merged
kamilmysliwiec merged 1 commit into
masterfrom
feat/export-field-metadata
Oct 26, 2026
Merged

kamilmysliwiec merged 1 commit into
masterfrom
feat/export-field-metadata

Conversation

@kamilmysliwiec

Copy link
Copy Markdown
Member

PR Checklist

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

  • Feature

What is the current behavior?

Writing a custom mapped type (e.g., a filter input derived from an object type) requires the compiled field metadata of a class, which is only reachable through deep imports such as @nestjs/graphql/dist/schema-builder/utils/get-fields-and-decorator.util.

Issue Number: #1361

What is the new behavior?

getFieldsAndDecoratorForType(), the helper that the built-in mapped types (PartialType(), PickType(), OmitType(), IntersectionType()) are built on, is exported from @nestjs/graphql, together with UnableToFindFieldsError (thrown for classes that are not GraphQL types) and the PropertyMetadata type. It returns the class's fields (inherited ones included) and the class decorator that matches its kind.

import { Type } from '@nestjs/common';
import { Field, getFieldsAndDecoratorForType, InputType } from '@nestjs/graphql';

export function FilterType<T>(classRef: Type<T>) {
  const { fields } = getFieldsAndDecoratorForType(classRef);

  @InputType({ isAbstract: true })
  abstract class FilterInput {}

  for (const field of fields) {
    const type = field.typeFn();
    Field(() => [type], { nullable: true })(FilterInput.prototype, field.name);
  }
  return FilterInput as Type<unknown>;
}

The returned metadata is shared with the schema builder and documented as read-only.

Does this PR introduce a breaking change?

  • Yes
  • No

Closes #1361

@kamilmysliwiec
kamilmysliwiec merged commit 4c7d8e0 into master Oct 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accessing Field Metadata

1 participant