Skip to content

Better validation and transformation of graphql input values #1211

@gipeshka

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.

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