Skip to content

Configure ConversionService for controller argument conversion #148

Closed
@yingzhuo

Description

@yingzhuo

ConversionService means org.springframework.core.convert.ConversionService

My example here:

type Query {
    getFoo(input: String): String
}
@Controller
public class FooController {

    @QueryMapping
    public String getFoo(@Argument("input") @FooFormat Foo input) {
        return input.getText();
    }

    @Getter
    @Setter
    @AllArgsConstructor
    public static class Foo {
        private final String text;
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.PARAMETER})
    public static @interface FooFormat {
        public String pattern() default "xxx";
    }

    @Component
    public static class FooFormatFactory implements AnnotationFormatterFactory<FooFormat> {
        @Override
        public Set<Class<?>> getFieldTypes() {
            return Collections.singleton(Foo.class);
        }

        @Override
        public Printer<?> getPrinter(FooFormat annotation, Class<?> fieldType) {
            return (Printer<Foo>) (object, locale) -> {
                System.out.println("Never Show This Message. (2)");
                System.out.println("Never Show This Message. (2)");
                System.out.println("Never Show This Message. (2)");
                return object.getText();
            };
        }

        @Override
        public Parser<?> getParser(FooFormat annotation, Class<?> fieldType) {
            return (Parser<Foo>) (text, locale) -> {
                System.out.println("Never Show This Message. (1)");
                System.out.println("Never Show This Message. (1)");
                System.out.println("Never Show This Message. (1)");
                return new Foo(text);
            };
        }
    }

}

FooFormatFactory do not works. Maybe make it similar to SpringMVC is good idea?

And why Foo's constructor is invoked by spring-graphql? I know Foo has only one constructor, and it has one parameter of type String. In my opinon, a Exception throwing is more reasonable.

Metadata

Metadata

Assignees

Labels

in: coreIssues related to config and core supporttype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions