模式不接受用于映射的 GraphQL Java 自定义标量类型

xet*_*a11 4 java graphql graphql-java

我尝试为 GraphQL Java 添加自定义标量类型。我需要它来解析 Map 而不为它创建类型,因为它是我逻辑中的常见返回类型。

我按照说明(这里http : //graphql-java.readthedocs.io/en/latest/scalars.html)创建了一个标量类型。

这是我的 MapScalar.java

public class MapScalar {
  private static final Logger LOG = LoggerFactory.getLogger(MapScalar.class);

  public static final GraphQLScalarType MAP = new GraphQLScalarType("Map", "A custom map scalar type", new Coercing() {
    @Override
    public Object serialize(Object dataFetcherResult) throws CoercingSerializeException {
      Map map = null;
      try {
        map = Map.class.cast(dataFetcherResult);
      } catch (ClassCastException exception) {
        throw new CoercingSerializeException("Could not convert " + dataFetcherResult + " into a Map", exception);
      }
      return map;
    }

    @Override
    public Object parseValue(Object input) throws CoercingParseValueException {
      LOG.warn("parseValue called");
      return null;
    }

    @Override
    public Object parseLiteral(Object input) throws CoercingParseLiteralException {
      LOG.warn("parseLiteral called");
      return null;
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

我将此标量实例添加到 RunTimeWiring

final RuntimeWiring runtimeWiring = newRuntimeWiring()
        .type(queryTypeFactory.getQueryBaseQueryType()) // just convenience methods I made
        .type(queryTypeFactory.getPageQueryType(viewName)) // ...
        .type(queryTypeFactory.getContentQueryType(viewName)) // ...
        .type(queryTypeFactory.getPictureQueryType()) // ...
        .type(queryTypeFactory.getSettingQueryType()) // just convenience methods I made
        .scalar(MapScalar.MAP) // added new scalar here
        .build();
Run Code Online (Sandbox Code Playgroud)

我定义了这个 MapDataFetcher

@Component
public class MapDataFetcher implements DataFetcher {

  @Override
  public Object get(DataFetchingEnvironment environment) {
    String fieldName = environment.getField().getName();
    Content source = Content.class.cast(environment.getSource());
    return source.getStruct(fieldName).toNestedMaps(); // returns a Map<String,Object>
  }

}
Run Code Online (Sandbox Code Playgroud)

此字段/标量的架构定义如下:

type Content {
    //... other fields
    settings: Map
}
Run Code Online (Sandbox Code Playgroud)

调试时RunTimeWiring一切似乎都很好。标量已添加到默认标量中:

在此处输入图片说明

仍然出现此错误

SCHWERWIEGEND: Servlet.service() for servlet [cae] in context with path [/blueprint] threw exception [Request processing failed; nested exception is SchemaProblem{errors=[The field type 'Map' is not present when resolving type 'Content' [@10:1], The field type 'Map' is not present when resolving type 'Setting' [@28:1]]}] with root cause
SchemaProblem{errors=[The field type 'Map' is not present when resolving type 'Content' [@10:1], The field type 'Map' is not present when resolving type 'Setting' [@28:1]]}
Run Code Online (Sandbox Code Playgroud)

我在教程中找不到任何方法来找出我遗漏了什么以使其正常工作。我知道这里缺少类型。但是创建一个新的类型newRunTimeWiring().type()是用于创建非标量类型不是吗?还是我还需要Map在那里创建一个类型?

xet*_*a11 8

只有一件小事没有在教程中提到。我不得不添加这个所以Map实际上被认为是一个scala类型

添加到模式定义文件:

scalar Map
Run Code Online (Sandbox Code Playgroud)