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

[Feature Request] Allow functions in directive parameters #422

Open
elijaholmos opened this issue Jul 10, 2023 · 3 comments · May be fixed by #825
Open

[Feature Request] Allow functions in directive parameters #422

elijaholmos opened this issue Jul 10, 2023 · 3 comments · May be fixed by #825

Comments

@elijaholmos
Copy link
Contributor

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.

@Code-Hex
Copy link
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?

@elijaholmos
Copy link
Contributor Author

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.

@Code-Hex
Copy link
Owner

Code-Hex commented Dec 9, 2023

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

rwysocki-equinix added a commit to rwysocki-equinix/graphql-codegen-typescript-validation-schema that referenced this issue Aug 21, 2024
* Add support for directives without arguments like `@required()`
* Add support for custom directive mapping

Closes Code-Hex#422, Code-Hex#781
rwysocki-equinix added a commit to rwysocki-equinix/graphql-codegen-typescript-validation-schema that referenced this issue Sep 20, 2024
* Add support for directives without arguments like `@required()`
* Add support for custom directive mapping

Closes Code-Hex#422, Code-Hex#781
@rwysocki-equinix rwysocki-equinix linked a pull request Sep 20, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants