Skip to content

[Feature Request] Allow functions in directive parameters #422

Closed

Description

@elijaholmos

I have a use case where I would like to be able to pass more than just a string as a directive argument. For example, if I have a directive designed to enforce uniqueness on arrays (in Zod), I would want to use the refine() method like so:

z.array(z.string()).refine(items => new Set(items).size === items.length);

Translating this to typescript-validation-schema:

directive @array(
  unique: Boolean = false
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION | ARGUMENT_DEFINITION

input TestInput {
	userIds: [Int] @array(unique: true)
}

With my CodegenConfig defined in a TypeScript file: (https://the-guild.dev/graphql/codegen/docs/config-reference/codegen-config)

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  overwrite: true,
  watch: false, 
  // ... other config ...
  generates: {
    'graphql/typings.ts': {
      plugins: [
        'typescript',
        'typescript-validation-schema',
      ],
      config: {
        strictScalars: true,
        schema: 'zod',
        directives: {
          array: {
            unique: ['refine', (items) => new Set(items).size === items.length],	// function passed as argument
          },
        },
      },
    },
  },
};

export default config;

However, that directive configuration yields the following error:
image

My request is that the typescript-validation-schema package can support receiving functions as arguments for custom directive definitions.

Activity

  1. Code-Hex commented on Aug 12, 2023

    @Code-Hex
    Owner

    @elijaholmos Thanks for your suggestion!
    I agree to accept this feature request. but this feature is difficult to treat in YAML configuration. Do you have any ideas?

  2. elijaholmos commented on Aug 14, 2023

    @elijaholmos
    ContributorAuthor

    graphql-codegen encourages the use of a typescript-based configuration by default, which naturally offers substantially more features than its yaml counterpart. We could perhaps simply not support this feature if a yaml configuration is present, as that would indeed be cumbersome to work with.

  3. Code-Hex commented on Dec 9, 2023

    @Code-Hex
    Owner

    @elijaholmos Could you give me PR for this implementation? Thanks!

  4. added a commit that references this issue on Aug 21, 2024
    d0a3bb8
  5. added a commit that references this issue on Sep 20, 2024
    b9ffecb
  6. Code-Hex commented on Apr 25, 2026

    @Code-Hex
    Owner

    Fixed in #1423. Directive configuration now accepts function values in directive parameters.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions