简体   繁体   中英

Kickstarter type dictionary equivalent in spring boot graphql starter

When using spring-boot-starter-graphql Spring is using a simple name to class mapping. Eg for a mutation:

mutation {
  addCategory(id: 6, name: "Green Fruits", products: [8, 2, 3]) {
    name
    products {
      name
    }
  }
}

Spring will check for a mutation called addCategory and for a class named Product .

graphql-java-kickstart offers a way to maintain a dictionary for classes not matching that pattern: https://www.graphql-java-kickstart.com/tools/schema-definition/#type-dictionary

Is there anything similar for spring-boot-starter-graphql ?

Spring's TypeResolver seem to to this for Interfaces and Unions

By default Spring for GraphQL will use a ClassNameTypeResolver to find the GraphQL Object Type from the value returned by the data fetcher. This is looking at the simple class name of the value and if it is not successful, it also navigates its super types including base classes and interfaces.

In case the Class name doesn't match any of the type names in the schema, you can configure a custom type resolver and have additional logic there.

You can instantiate a ClassNameTypeResolver and set its

ClassNameTypeResolver classNameTypeResolver = new ClassNameTypeResolver();
classNameTypeResolver.setClassNameExtractor((klass) -> {
    // Look up into a custom dictionnary
    // fall back to klass.getSimpleName() if none found
});

This can be contributed through a GraphQlSourceBuilderCustomizer in Spring Boot. See https://docs.spring.io/spring-graphql/docs/current-SNAPSHOT/reference/html/#execution-graphqlsource-default-type-resolver .

There is no such dictionary, but you can override the default behavior and customize the type resolver.

Take a look here :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM