Skip to content

Better validation and transformation of graphql input values #1211

Closed

Description

@gipeshka

Hi guys,

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

input ConsumerData {
  name: String @length(min: 1, max: 20)
}

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.

Would it make sense to add something like

interface DataTransformer<T> {
    public T transform(T value)
}

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.

Activity

  1. changed the title [-]Apply directive's behaviour on InputObjectField[/-] [+]Better validation of graphql input values[/+] on Oct 21, 2018
  2. bbakerman commented on Oct 21, 2018

    @bbakerman
    Member

    @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

  3. changed the title [-]Better validation of graphql input values[/-] [+]Better validation and transformation of graphql input values[/+] on Oct 21, 2018
  4. bbakerman commented on Oct 24, 2018

    @bbakerman
    Member

    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

  5. bbakerman commented on Oct 24, 2018

    @bbakerman
    Member

    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.

    If I do this query

       query {
          oldGuy: walksIntoABar(visitor : {age : 25, dollarsInWallet : 100.0, wearingShoes : true})
    
          teenager : walksIntoABar(visitor : {age : 14, dollarsInWallet : 0.0, wearingShoes : true})
       }
    

    Then does the whole query fail or does the oldGuy field (that is valid) complete as expected.

    Or is this a choice an implementation choose to make some how?

  6. bbakerman commented on Nov 4, 2018

    @bbakerman
    Member

    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).

  7. telbot commented on Jul 26, 2019

    @telbot

    @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.

    Thanks.

  8. zyxist commented on Aug 15, 2019

    @zyxist

    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:

    directive @exactlyOneOf(fields: [String])
    
    input Something @exactlyOneOf(fields: ["foo", "bar"]) {
       foo: String
       bar: String
       joe: String
    }

    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.

  9. bbakerman commented on Aug 17, 2019

    @bbakerman
    Member

    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?

  10. zyxist commented on Aug 17, 2019

    @zyxist

    @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.

  11. cliedeman commented on Sep 4, 2019

    @cliedeman

    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.

    type SomeInput {
      fooId @RelayId(type: Foo)
    }
    

    I currently have this working automatically for encoding on field resolution

    Ciaran

  12. bbakerman commented on Sep 5, 2019

    @bbakerman
    Member

    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

    drivers( first : Int, after : String!, last : Int, before : String) : DriverConnection @Expression(value : "${args.containsOneOf('first','last') }"
    
  13. andimarek commented on Jan 15, 2020

    @andimarek
    Member

    closing this ad the extended library is available

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions