我尝试为GraphQL Java添加自定义标量类型。我需要它来解析Map而不为它创建一个类型,因为它是我逻辑中常见的返回类型。
我按照指示( here :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;
}
});
}
我将此标量实例添加到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();
我定义了这个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>
}
}
此字段/标量的架构定义如下:
type Content {
//... other fields
settings: Map
}
调试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]]}
我在教程中找不到任何后果,找出我错过的内容以使其工作。我知道这里缺少一种类型。但是创建一个带有newRunTimeWiring().type()
的新类型是用于创建非标量类型的不是吗?或者我还需要在那里创建Map
类型吗?
答案 0 :(得分:2)
在教程中没有人提到过一件小事。
我必须添加这个,所以Map
实际上被识别为scala类型
添加到架构定义文件:
scalar Map