Replies: 2 comments 3 replies
-
It's not covered because the goal for documentation is to document good patterns and practices, and using directives for data validation is not one of those 😄 Problems with this approach:
Arguments don't hold any logic that is being executed at query time, only argument definition that validation rules and query executor access to find out:
If I was determined to put validation declaration in schema, I would use directives as dumb metadata holders, and implement Python util that would construct pydantic model from field's definition using field's declaration available from resolver's |
Beta Was this translation helpful? Give feedback.
-
Let me take a step back to explain why I am attempting to do data validation via directives in the first place, and maybe you can point out a better way of approaching this. When it comes to error handling, from the client's perspective there is a basic need to distinguish between "the problem is the client's fault" and "the problem is the server's fault", so that the client can make informed decisions, such as whether it's worth retrying the failed request. We return an HTTP 400 to indicate a client-based error (they supplied bad data in the query) and an HTTP 500 to indicate a server-based error (the query itself passed validation, but something unexpectedly went wrong during execution of that query). According to the documentation, the A If we do as you suggest, and move data validation into the resolver functions, then how can we distinguish between client-based errors and server-based errors so that we can choose HTTP 400 vs 500 to return to the client? |
Beta Was this translation helpful? Give feedback.
-
I'm confused about the use of directives vs validation rules when it comes to arguments. Ariadne's documentation doesn't seem to cover this particular case in great detail.
Suppose I wanted to implement regex-based validation for certain arguments in my schema:
I'm defining this as a directive rather than a validation rule because it's a re-usable concept that I want the ability to easily apply to various arguments throughout the SDL.
But how would you actually implement such a directive in code? The documentation only provides an example for a
FIELD_DEFINITION
directive, in which the given field'sresolve
function is customized: https://ariadnegraphql.org/docs/schema-directives#example-datetime-formatThere doesn't appear to be any similar hook on a GraphQL argument definition for attaching a validation function:
Taking a look at the
query_cost
validator inariadne.validation.query_cost
, it appears that this is both a directive and a validation rule. It defines a directive for use in the SDL, but the actual implementation does not subclassSchemaDirectiveVisitor
. Instead it subclassesValidationRule
and then inspects thedirectives
attribute onfield.ast_node
: https://github.com/mirumee/ariadne/blob/master/ariadne/validation/query_cost.py#L110This is awkward for multiple reasons:
make_executable_schema
and the validation rule to thegraphql()
function.ValidationRule
is going to be executed for every single argument, not just the ones with the directive applied.Is there no way to implement validation for an argument using an
ARGUMENT_DEFINITION
directive and only subclassingSchemaDirectiveVisitor
?Beta Was this translation helpful? Give feedback.
All reactions