You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Better validation and transformation of graphql input values #1211
I have spent some time looking for a solution on how to implement data validation in graphql using directives. The idea seems very nice and clear: schema directives define validation rules to be applied to the data, eg
However current approach described in the documentation refers to output types of directives only and suggests usage of Consumer<GraphQLFieldDefinition.Builder> together with GraphQLFieldDefinition.dataFetcher.
Applying the same approach to GraphQLInputObjectField wouldn't work since GraphQLInputObjectField doesn't have any kind of data transformer.
to GraphQLInputObjectField for the purpose of usage of directives? This could be used in ValuesResolver.coerceValueAstForInputObject.
To supply a DataTransformer to ValuesResolver one could use the same wiring approach as one currently used to update dataFetcher on GraphQLFieldDefinition.
Reacted by Rafael, Juan Carlos Mejías Rodríguez, Tomasz Jędrzejewski, Arid Clown and Vitali R
@gipeshka - I changed the title of this issue to better reflect the idea behind this. I also wanted to explore some thoughts on your ideas and also further ideas on validating and transforming graphql data
One of the capabilities missing in graphql-java (and perhaps graphql spec itself) is the ability
to perform deeper validation of input.
A greater validation system would ideally have the following aspects
The ability to run before the execution of the query fields happens
The ability for SDL directives to be specified indicating extensible validation
The ability for a pluggable "aspect" code to be created that interprets those directives and injects runtime behaviour
It should not require a Instrumentation to be added in order to get this happening (or if it does it should be automatic)
There is design challenge about where these validation hooks should live.
For example imagine SDL like the following
type Query {
searchPages(criteria : SearchCriteria!, first : Int, after : String, last : Int, before : String) : Results
searchComments(criteria : SearchCriteria!, first : Int, after : String, last : Int, before : String) : Results
}
input SearchCriteria {
complexFilter : String
simpleFilter : String
orderBy : String
}
Now imagine that
the SearchCriteria.complexFilter field has a certain SQL like syntax, that the SearchCriteria.simpleFilter has a different syntax
the SearchCriteria.orderBy` field is a comma seperated list of columns.
you can have a complexFilter or a simpleFilter but not both and you MUST have one of the other
that first / after arguments must be specified OR the last / after fields must be specified but not both (relay style)
Where would the validation hooks live for such a system?
Today the only code hook place is the DataFetcher attached to the field. But this is run ONLY during field execution
and not before in a query validation stage.
The SearchCriteria input type validation rules are common across all fields that use it (in this case) and hence
its validation could be hooked to it versus the fields that might use it.
The field arguments around first/after or last/before would belong to the field. But is it a DataFetchingException that indicates this or could it be a pre field validation? This concern is more about the "name" of the error that results in the output. Is it a validation error or is it a execution error? I think its the former
So imagine we try to start using directives to enforce some of these validations. I have thrown in some more example types
Also please dont concentrate on the syntax of the directives. They are illustrative only not proposed synaxt
in any way
type Query {
searchPages(criteria : SearchCrieria!, first : Int, after : String, last : Int, before : String)
: Results @Validator(className:"com.package.RelayParamValidation")
searchComments(criteria : SearchCrieria!, first : Int, after : String, last : Int, before : String)
: Results @Validator(className:"com.package.RelayParamValidation")
}
type Mutation {
walkIntoABar( visitor : BarVisitor) : BarDetails
partyWalksIntoABar( visitors : [BarVisitor]) : BarDetails
}
input SearchCriteria {
complexFilter : String
simpleFilter : String
orderBy : String
}
input BarVisitor {
age : Int @Max(value : 18)
dollarsInWallet : Float @Min(value : 1)
wearingShoes : Boolean @AssertTrue
}
Today we have a interface called graphql.schema.idl.SchemaDirectiveWiring that is called back on every directivesas it
is encountered. So we have the SDL hooks to know about directives.
However where such a SchemaDirectiveWiring would put the code behind the directive is the real question I mention
above.
Lets consider BarVisitor fo the moment. One can assume that ANY time a BarVisitor is input to the system it should
follow these validation directives. But what about lists. Would be handle this implicitly that all members
of the list are to be validated. I am guessing yes.
One good question is where to put the code for input validation. If we copied the previous pattern we would attach it to each field of the Input object like we do on GraphqlFieldDefinitions.
But this is insufficient. For example a field like mutation { someObject(String input) has only a scalar type so putting code on the input object itself is no good.
Looking at the examples above we have
validation associated with a specific query / mutation field
validiation associated with specific input object fields regardless of query field
validation potentially associated with the input object itself (for all its fields say)
So it seems we need a registry that can take a named "node" and associate a validation bit of code with it. Those nodes could be "query fields (and hence their arguments)" , "input object fields" or "input objects" as a whole.
This starts to beg the question of having a SchemaExecutionRegistry that contains validation (by field/type name) and potentially later the "data fetchers" we have today again associated by field name.
This SchemaExecutionRegistry is probably a peer top the GraphqlSchema or at least a top level object inside the schema (since today you get the schema to get data fetchers).
Reacted by Ophir Radnitz, Radu Ciumag and Phuong Phally (ភួង ផល្លី)
@bbakerman I was wondering if there were any updates to this and how validation should be approached?
At the moment we are using directives through graphql.schema.idl.SchemaDirectiveWiring and we can validate as expected, but we haven't been able to 'chain' errors successfully.
For example, if we have a query with 2 arguments and a 'size' directive which validates min and max size on both of the arguments, it will only return 1 validation error at a time. We would like to validate all arguments passed to the query and return those to the client if possible.
Is this a limitation with directives at the moment or are we misunderstanding how validation errors should be generated through directives? Most of the examples I've been able to find focus more on transformation of data (i.e. something like a date formatter), rather than on validating schema elements.
I'm also interested in having a support for validating input values, based on custom directives. I'm trying to implement a support for the following use case:
Currently, the only solution I came up with is writing a custom instrumentation that listens to the beginning of every field, performs a deep scan of input object values, and compares them with the input type definitions to find any custom directives like this one. This scan basically repeats the job that has already been done in ValuesResolver, and by-passes SchemaDirectiveWiring entirely. (note: I've just started implementing it, so I'm not 100% sure that it would work, but for now I don't see any reason why it shouldn't)
BTW. I'm really impressed with the extensibility of GraphQL-Java. This is actually the first time I encountered some limitation, and even with that I was able to invent some workaround, using other extension points :).
BTW2. @bbakerman I can help with implementing that feature (or at least trying :D), I would just need some hints about the general architecture you'd like to see related to handling use cases like this.
I have thought long and hard on this and tried to design new "SPI" to allow validation of input values.
However after much thought and code tribulation I have come to the position of adding now SPI but in fact doing what you describe
That is the validation is a precursor to DataFetching and that DataFetcher
"enhancement" is the way to achieve it.
I have come to beleive this is the right approach because validationof the data to be executed needs to happen when all the query is valid ready to be executed.
The inputs to "input data validation" need to be perfectly "query valid" in GQK terms before you run actual business values validation. I have come to beleive that confusing "GQL query validation" eg structure of the text and inputs is not the same as validation of the input data.
I have tried to make it extensible via plugin validation rules. Whether it can truly do that easily requires some like you to help shape it out.
I have started also on a Java EL implementation where you put in Java EL rules in directives and have them run eg '${args.containsOneOf('a','b')}'
This is a side project of side project at the moment. I would love to collaborate with others to help shape this out into a published library and likely under the graphql-java project eventually.
@zyxist or anyone else on this thread interested in helping?
@bbakerman It looks like the library solves additional extra issues I'd still have to deal with, especially proper error reporting. I've just forked the code and I'll check it out, whether it is possible to write a validation that I need.
Activity
[-]Apply directive's behaviour on InputObjectField[/-][+]Better validation of graphql input values[/+]bbakerman commentedon Oct 21, 2018
@gipeshka - I changed the title of this issue to better reflect the idea behind this. I also wanted to explore some thoughts on your ideas and also further ideas on validating and transforming graphql data
[-]Better validation of graphql input values[/-][+]Better validation and transformation of graphql input values[/+]bbakerman commentedon Oct 24, 2018
One of the capabilities missing in graphql-java (and perhaps graphql spec itself) is the ability
to perform deeper validation of input.
A greater validation system would ideally have the following aspects
Instrumentationto be added in order to get this happening (or if it does it should be automatic)There is design challenge about where these validation hooks should live.
For example imagine SDL like the following
Now imagine that
SearchCriteria.complexFilterfield has a certain SQL like syntax, that theSearchCriteria.simpleFilterhas a different syntaxcomplexFilteror asimpleFilterbut not both and you MUST have one of the otherfirst / afterarguments must be specified OR thelast / afterfields must be specified but not both (relay style)Where would the validation hooks live for such a system?
Today the only code hook place is the
DataFetcherattached to the field. But this is run ONLY during field executionand not before in a query validation stage.
The
SearchCriteriainput type validation rules are common across all fields that use it (in this case) and henceits validation could be hooked to it versus the fields that might use it.
The field arguments around
first/afterorlast/beforewould belong to the field. But is it aDataFetchingExceptionthat indicates this or could it be a pre field validation? This concern is more about the "name" of the error that results in the output. Is it a validation error or is it a execution error? I think its the formerbbakerman commentedon Oct 24, 2018
So imagine we try to start using directives to enforce some of these validations. I have thrown in some more example types
Also please dont concentrate on the syntax of the directives. They are illustrative only not proposed synaxt
in any way
Today we have a interface called
graphql.schema.idl.SchemaDirectiveWiringthat is called back on every directivesas itis encountered. So we have the SDL hooks to know about directives.
However where such a SchemaDirectiveWiring would put the code behind the directive is the real question I mention
above.
Lets consider BarVisitor fo the moment. One can assume that ANY time a BarVisitor is input to the system it should
follow these validation directives. But what about lists. Would be handle this implicitly that all members
of the list are to be validated. I am guessing yes.
If I do this query
Then does the whole query fail or does the
oldGuyfield (that is valid) complete as expected.Or is this a choice an implementation choose to make some how?
bbakerman commentedon Nov 4, 2018
One good question is where to put the code for input validation. If we copied the previous pattern we would attach it to each field of the
Inputobject like we do onGraphqlFieldDefinitions.But this is insufficient. For example a field like
mutation { someObject(String input)has only a scalar type so putting code on the input object itself is no good.Looking at the examples above we have
input objectfields regardless of query fieldSo it seems we need a registry that can take a named "node" and associate a validation bit of code with it. Those nodes could be "query fields (and hence their arguments)" , "input object fields" or "input objects" as a whole.
This starts to beg the question of having a
SchemaExecutionRegistrythat contains validation (by field/type name) and potentially later the "data fetchers" we have today again associated by field name.This
SchemaExecutionRegistryis probably a peer top theGraphqlSchemaor at least a top level object inside the schema (since today you get the schema to get data fetchers).telbot commentedon Jul 26, 2019
@bbakerman I was wondering if there were any updates to this and how validation should be approached?
At the moment we are using directives through
graphql.schema.idl.SchemaDirectiveWiringand we can validate as expected, but we haven't been able to 'chain' errors successfully.For example, if we have a query with 2 arguments and a 'size' directive which validates min and max size on both of the arguments, it will only return 1 validation error at a time. We would like to validate all arguments passed to the query and return those to the client if possible.
Is this a limitation with directives at the moment or are we misunderstanding how validation errors should be generated through directives? Most of the examples I've been able to find focus more on transformation of data (i.e. something like a date formatter), rather than on validating schema elements.
Thanks.
zyxist commentedon Aug 15, 2019
I'm also interested in having a support for validating input values, based on custom directives. I'm trying to implement a support for the following use case:
Currently, the only solution I came up with is writing a custom instrumentation that listens to the beginning of every field, performs a deep scan of input object values, and compares them with the input type definitions to find any custom directives like this one. This scan basically repeats the job that has already been done in
ValuesResolver, and by-passesSchemaDirectiveWiringentirely. (note: I've just started implementing it, so I'm not 100% sure that it would work, but for now I don't see any reason why it shouldn't)BTW. I'm really impressed with the extensibility of GraphQL-Java. This is actually the first time I encountered some limitation, and even with that I was able to invent some workaround, using other extension points :).
BTW2. @bbakerman I can help with implementing that feature (or at least trying :D), I would just need some hints about the general architecture you'd like to see related to handling use cases like this.
bbakerman commentedon Aug 17, 2019
I have thought long and hard on this and tried to design new "SPI" to allow validation of input values.
However after much thought and code tribulation I have come to the position of adding now SPI but in fact doing what you describe
That is the validation is a precursor to DataFetching and that DataFetcher
"enhancement" is the way to achieve it.
I have come to beleive this is the right approach because validationof the data to be executed needs to happen when all the query is valid ready to be executed.
The inputs to "input data validation" need to be perfectly "query valid" in GQK terms before you run actual business values validation. I have come to beleive that confusing "GQL query validation" eg structure of the text and inputs is not the same as validation of the input data.
To that end I have a POC in my personal repo
https://github.com/bbakerman/graphql-java-extended-validation
Its exactly what you describe.
I have tried to make it extensible via plugin validation rules. Whether it can truly do that easily requires some like you to help shape it out.
I have started also on a Java EL implementation where you put in Java EL rules in directives and have them run eg
'${args.containsOneOf('a','b')}'This is a side project of side project at the moment. I would love to collaborate with others to help shape this out into a published library and likely under the graphql-java project eventually.
@zyxist or anyone else on this thread interested in helping?
zyxist commentedon Aug 17, 2019
@bbakerman It looks like the library solves additional extra issues I'd still have to deal with, especially proper error reporting. I've just forked the code and I'll check it out, whether it is possible to write a validation that I need.
cliedeman commentedon Sep 4, 2019
I am also somewhat interested in this. I want to automatically decode relay ids to ensure the correct type is sent for arguments and mutation inputs.
E.g.
I currently have this working automatically for encoding on field resolution
Ciaran
bbakerman commentedon Sep 5, 2019
The personal library is now available in a 0.0.1 form here
https://github.com/graphql-java/graphql-java-extended-validation
A relay field might do this
andimarek commentedon Jan 15, 2020
closing this ad the extended library is available