- 
                Notifications
    You must be signed in to change notification settings 
- Fork 35
Open
Description
Describe the bug
When wiring the default graphql.java.extended.validation setup with the RuntimeWiringConfigurer provided by the new Spring for GraphQL project, custom directives that are registered via the builders directive() method don't seem to apply anymore.
To Reproduce
- Implement the official graphql-javaDateFormattingDirective:
directive @DateFormat on FIELD_DEFINITION
type Person {
    id: ID!
    name: String!
    createdAt: String! @DateFormat
}public class DateFormattingDirective implements SchemaDirectiveWiring {
    private static final String FORMAT = "format";
    @Override
    public GraphQLFieldDefinition onField(
        SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> env) {
        GraphQLFieldDefinition fieldDefinition = env.getElement();
        GraphQLFieldsContainer fieldsContainer = env.getFieldsContainer();
        // DataFetcherFactories.wrapDataFetcher is a helper to wrap data fetchers so that
        // CompletionStage is handled correctly along with POJOs
        DataFetcher<?> originalFetcher = env.getCodeRegistry()
            .getDataFetcher(fieldsContainer, fieldDefinition);
        DataFetcher<?> dataFetcher = DataFetcherFactories.wrapDataFetcher(originalFetcher,
            ((dataFetchingEnvironment, value) -> {
                if (value instanceof LocalDateTime localDateTime
                    && dataFetchingEnvironment.containsArgument(FORMAT)) {
                    String dateTimePattern = dataFetchingEnvironment.getArgument(FORMAT);
                    DateTimeFormatter dateTimeFormatter = buildFormatter(dateTimePattern);
                        return dateTimeFormatter.format(localDateTime);
                    }
                }
                return value;
            }));
        // This will extend the field by adding a new "format" argument to it for the date
        // formatting which allows clients to opt into.
        FieldCoordinates coordinates = FieldCoordinates.coordinates(fieldsContainer,
            fieldDefinition);
        env.getCodeRegistry().dataFetcher(coordinates, dataFetcher);
        return fieldDefinition.transform(builder -> builder.argument(
            GraphQLArgument.newArgument().name(FORMAT).type(Scalars.GraphQLString)));
    }
    private static DateTimeFormatter buildFormatter(String format) {
        return DateTimeFormatter.ofPattern(format);
    }
}- Wire the custom DateFormattingDirectivealongside the standard setup of thegraphql.java.extended.validationproject:
@Configuration
public class GraphQLConfig {
  
    @Bean
    public RuntimeWiringConfigurer validationConfigurer() {
        ValidationRules validationRules = ValidationRules.newValidationRules()
            .onValidationErrorStrategy(OnValidationErrorStrategy.RETURN_NULL)
            .build();
        ValidationSchemaWiring schemaWiring = new ValidationSchemaWiring(validationRules);
        return builder -> builder.directiveWiring(schemaWiring).build();
    }
    @Bean
    public RuntimeWiringConfigurer directiveConfigurer() {
        return builder -> builder
            .directive("DateFormat", new DateFormattingDirective())
            .build();
    }
}- See the functionality of the custom DateFormattingDirectivedisappear.
Without graphql.java.extended.validation:

With graphql.java.extended.validation:

Note, Registering both
graphql.java.extended.validationSchemaWiringandCustomDirectiveinside a singleBeanwont help either.
lkorth and yeikel
Metadata
Metadata
Assignees
Labels
No labels