Graphql 使用 Input 类型来搜索数据
Graphql use Input type to search data
我在 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)
代码运行良好 - 但我想转换为工作。
对于类型为input
类型的输入参数,graphql-java
会将其转换为Map
.
在您的例子中,查询是 getByInput (example: {name: "aa"} )
,其中 example
参数是 input
类型。所以,
dataFetchingEnvironment.get("example");
将 return 具有结构 (key="name" , value="aa") 的映射。然后您尝试将映射转换为 Character
这肯定会给出你是一个错误,因为它们是完全不同的类型。
要将 Map 转换为 Character
,graphql-java
不会帮助您进行此类转换。您必须自己实现转换代码或使用其他库,例如 Jackson , Gson , Dozer or whatever libraries you like 将地图转换为您的域对象(即字符)。
我在 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)
代码运行良好 - 但我想转换为工作。
对于类型为input
类型的输入参数,graphql-java
会将其转换为Map
.
在您的例子中,查询是 getByInput (example: {name: "aa"} )
,其中 example
参数是 input
类型。所以,
dataFetchingEnvironment.get("example");
将 return 具有结构 (key="name" , value="aa") 的映射。然后您尝试将映射转换为 Character
这肯定会给出你是一个错误,因为它们是完全不同的类型。
要将 Map 转换为 Character
,graphql-java
不会帮助您进行此类转换。您必须自己实现转换代码或使用其他库,例如 Jackson , Gson , Dozer or whatever libraries you like 将地图转换为您的域对象(即字符)。