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

Argument validation on Directives is inconsistent #5209

Open
olbrich opened this issue Jan 20, 2025 · 1 comment
Open

Argument validation on Directives is inconsistent #5209

olbrich opened this issue Jan 20, 2025 · 1 comment

Comments

@olbrich
Copy link

olbrich commented Jan 20, 2025

Describe the bug

When defining a directive, if one sets validations for the arguments, unexpected things happen.

For example...

module Directives
  class Feature < GraphQL::Schema::Directive
    description "Directs the executor to run this only based on the state of a server-side feature flag."

    argument :if, String, required: false, validates: { format: /\d+/,  }, description: "Include the element if the feature flag is enabled."
    argument :unless, String, required: false, validates: { format: /\d+/,  }, description: "Include the element if the feature flag is disabled."
    
    # validates required: { one_of: %i[if unless] }  # Doesn't work at all

    ...
end

With an argument definition like this we would expect a validation failure when invoked like @feature(if: "flag") because the validation checks for numbers instead of strings (this is a contrived validation, BTW).

What actually happens is that the include? method is called with a GraphQL::ExecutionError as the second argument (which should be a GraphQL::Execution::Interpreter::Arguments of the directive).

I also observe that it is not possible to add a validates requires: { one_of: %i[if unless] } for a directive, that just fails.

Versions

    graphql-enterprise (1.4.0)
      graphql (>= 2.0.18)
      graphql-pro (>= 1.24.0)
    graphql-pro (1.27.4)
      graphql (>= 1.7.5)

Rails 7.2.2.1

Expected behavior

  • I'd expect an argument validation error to be raised back to the executor and generate an appropriate error message.
  • Use of validates ... should work as documented for directive arguments as well.
@rmosolgo
Copy link
Owner

rmosolgo commented Feb 6, 2025

Hey, sorry for the trouble with this. Directive arguments have definitely not gotten a lot of attention 😅 . I agree it should work the way you describe here. I'll take a look soon and follow up here!

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

No branches or pull requests

2 participants