【发布时间】:2019-06-12 22:01:12
【问题描述】:
在graphql 中使用Input data 搜索时遇到问题:
@RestController
@RequestMapping("/api/dictionary/")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DictionaryController {
@Value("classpath:items.graphqls")
private Resource schemaResource;
private GraphQL graphQL;
private final DictionaryService dictionaryService;
@PostConstruct
public void loadSchema() throws IOException {
File schemaFile = schemaResource.getFile();
TypeDefinitionRegistry registry = new SchemaParser().parse(schemaFile);
RuntimeWiring wiring = buildWiring();
GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(registry, wiring);
graphQL = GraphQL.newGraphQL(schema).build();
}
private RuntimeWiring buildWiring() {
DataFetcher<String> fetcher9 = dataFetchingEnvironment ->
getByInput((dataFetchingEnvironment.getArgument("example")));
return RuntimeWiring.newRuntimeWiring()
.type("Query", typeWriting ->
typeWriting
.dataFetcher("getByInput", fetcher9)
)
.build();
}
public String getByInput(Character character) {
return "testCharacter";
}
}
items.graphqls文件内容:
type Query {
getByInput(example: Character): String
}
input Character {
name: String
}
当要求这样的资源时:
query {
getByInput (example: {name: "aa"} )
}
字符 DTO:
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Character {
protected String name;
}
我有一个错误:
"Exception while fetching data (/getByInput) : java.util.LinkedHashMap cannot be cast to pl.graphql.Character",
查询应该是什么样子?
编辑
如果我改成:
public String getByInput(Object character)
代码运行良好 - 但我想转换为工作。
【问题讨论】:
标签: java graphql graphql-java