close

Filter

loading table of contents...

Headless Server Developer Manual / Version 2512.0

Table Of Contents

4.11.2 Adding a Custom Type Name Resolver

If the GraphQL type name does not follow the convention, you can implement a custom resolver. Add a TypeNameResolver<Content> Spring bean and return the GraphQL type name wrapped in an Optional, or an empty Optional to pass control to the next resolver in the chain — including the built-in convention resolver. Use @Order to ensure the custom resolver is executed before the default one.

To add a custom GraphQL type named CustomType which maps to the content type CMExampleDocument, add the type and the resolver:

type CustomType implements CMTeasable @inherit(from: ["CMTeasableImpl"]) {
  text: String @fetch(from: "text")
}
@Bean
@Order(0) // run before the built-in resolver
public TypeNameResolver<Content> myTypeNameResolver() {
  return content -> {
    if ("CMExampleDocument".equals(content.getType().getName())) {
      return Optional.of("CustomType");
    }
    return Optional.empty(); // fall through to the next resolver
  };
}

If the GraphQL type implements only standard interfaces (whose names match a standard existing content type), no further configuration is needed.

If it also implements a custom interface whose name does not match any content type name, also declare and register that interface.

type CustomType implements CustomTeasable @inherit(from: ["CMTeasableImpl"]) {
  text: String @fetch(from: "text")
}

interface CustomTeasable @inherit(from: ["CMTeasable"]) {
  text: String
}
@Bean
public ProvidesTypeNameResolver myProvidesTypeNameResolver() {
  return typeName -> "CustomTeasable".equals(typeName) ? Optional.of(true) : Optional.empty();
}

Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.