我正在使用这个端点:
@PostMapping("graphql")
public ResponseEntity<Object> getResource(@RequestBody Object query) { // String query
ExecutionResult result;
if (query instanceof String) {
result = graphQL.execute(query.toString()); // if plain text
} else{
String queryString = ((HashMap) query).get("query").toString();
Object variables = ((HashMap) query).get("variables");
ExecutionInput input = ExecutionInput.newExecutionInput()
.query(queryString)
.variables((Map<String, Object>) variables) // "var1" -> "test1"
.build();
result = graphQL.execute(input);
}
return new ResponseEntity<Object>(result, HttpStatus.OK);
}
当我没有变量时,它工作正常:
query {
getItem(dictionaryType: "test1") {
code
name
description
}
}
当我添加variable
它开始失败时,请参见此处:
query {
getItem(dictionaryType: $var1) {
code
name
description
}
}
在我的schema
我定义的query
部分如下:
type Query {
getItem(dictionaryType: String): TestEntity
}
在java
代码中:
@Value("classpath:test.graphqls")
private Resource schemaResource;
private GraphQL graphQL;
@PostConstruct
private 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() {
initializeFetchers();
return RuntimeWiring.newRuntimeWiring()
.type("Query", typeWriting -> typeWriting
.dataFetcher("getItem", dictionaryItemFetcher)
)
.build();
}
private void initializeFetchers() {
dictionaryItemFetcher = dataFetchingEnvironment ->
dictionaryService.getDictionaryItemsFirstAsString(dataFetchingEnvironment.getArgument("dictionaryType"));
}