Search in sources :

Example 1 with FieldValidationEnvironment

use of graphql.execution.instrumentation.fieldvalidation.FieldValidationEnvironment in project graphql-java by graphql-java.

the class InstrumentationExamples method fieldValidation.

private void fieldValidation() {
    ExecutionPath fieldPath = ExecutionPath.parse("/user");
    FieldValidation fieldValidation = new SimpleFieldValidation().addRule(fieldPath, new BiFunction<FieldAndArguments, FieldValidationEnvironment, Optional<GraphQLError>>() {

        @Override
        public Optional<GraphQLError> apply(FieldAndArguments fieldAndArguments, FieldValidationEnvironment environment) {
            String nameArg = fieldAndArguments.getArgumentValue("name");
            if (nameArg.length() > 255) {
                return Optional.of(environment.mkError("Invalid user name", fieldAndArguments));
            }
            return Optional.empty();
        }
    });
    FieldValidationInstrumentation instrumentation = new FieldValidationInstrumentation(fieldValidation);
    GraphQL.newGraphQL(schema).instrumentation(instrumentation).build();
}
Also used : FieldValidationInstrumentation(graphql.execution.instrumentation.fieldvalidation.FieldValidationInstrumentation) FieldValidationEnvironment(graphql.execution.instrumentation.fieldvalidation.FieldValidationEnvironment) Optional(java.util.Optional) SimpleFieldValidation(graphql.execution.instrumentation.fieldvalidation.SimpleFieldValidation) SimpleFieldValidation(graphql.execution.instrumentation.fieldvalidation.SimpleFieldValidation) FieldValidation(graphql.execution.instrumentation.fieldvalidation.FieldValidation) FieldAndArguments(graphql.execution.instrumentation.fieldvalidation.FieldAndArguments) ExecutionPath(graphql.execution.ExecutionPath)

Aggregations

ExecutionPath (graphql.execution.ExecutionPath)1 FieldAndArguments (graphql.execution.instrumentation.fieldvalidation.FieldAndArguments)1 FieldValidation (graphql.execution.instrumentation.fieldvalidation.FieldValidation)1 FieldValidationEnvironment (graphql.execution.instrumentation.fieldvalidation.FieldValidationEnvironment)1 FieldValidationInstrumentation (graphql.execution.instrumentation.fieldvalidation.FieldValidationInstrumentation)1 SimpleFieldValidation (graphql.execution.instrumentation.fieldvalidation.SimpleFieldValidation)1 Optional (java.util.Optional)1