[英]Graphql use Input type to search data
I have problem searching with using Input data
in graphql
:我在使用
graphql
Input data
进行搜索时graphql
:
@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
file content: items.graphqls
文件内容:
type Query {
getByInput(example: Character): String
}
input Character {
name: String
}
When asking for resource like that:当请求这样的资源时:
query {
getByInput (example: {name: "aa"} )
}
Character DTO:字符 DTO:
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Character {
protected String name;
}
I've got an error:我有一个错误:
"Exception while fetching data (/getByInput) : java.util.LinkedHashMap cannot be cast to pl.graphql.Character",
How should the query look like?查询应该是什么样的?
Edit编辑
If i change to:如果我改为:
public String getByInput(Object character)
The codes runs fine - but i want convert to work.代码运行良好 - 但我想转换为工作。
For the input argument which the type is the input
type , graphql-java
will convert it to a Map
.对于类型为
input
类型的输入参数, graphql-java
会将其转换为Map
。
In your case the query is getByInput (example: {name: "aa"} )
which the example
argument is the input
type .在您的情况下,查询是
getByInput (example: {name: "aa"} )
,其中example
参数是input
type 。 So ,所以 ,
dataFetchingEnvironment.get("example");
will return a Map with the structure (key="name" , value="aa") .Then you try to cast the map to Character
which definitely gives you an error since they are totally different types.将返回一个具有结构的 Map (key="name" , value="aa") 。然后您尝试将映射转换为
Character
这肯定会给您一个错误,因为它们是完全不同的类型。
To convert a Map to a Character
, graphql-java
will not help you for such conversion.要将 Map 转换为
Character
, graphql-java
不会帮助您进行此类转换。 You have to implement the conversion codes by yourselves or use other libraries such as Jackson , Gson , Dozer or whatever libraries you like for converting a map to your domain object (ie Character).您必须自己实现转换代码或使用其他库,例如Jackson 、 Gson 、 Dozer 或您喜欢的任何库来将地图转换为域对象(即 Character)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.