我想使用graphql
返回枚举的所有值。
我有schema
:
schema {
query: Query
}
type Query {
getDataTypes: [DictionaryType]
}
enum DictionaryType{
RISK
SALES_CHANNEL
PERSON_TYPE
}
我们有普通的Java枚举:
public enum DictionaryType {
RISK,
SALES_CHANNEL,
PERSON_TYPE
}
和Controller
的配置:
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<Set<DictionaryType>> fetcher3 = dataFetchingEnvironment -> {
return dictionaryService.getDictionaryTypes();
};
return RuntimeWiring.newRuntimeWiring().type("Query", typeWriting ->
typeWriting
.dataFetcher("getDataTypes", fetcher3))
.build();
}
@PostMapping("getDataTypes")
public ResponseEntity<Object> getDataTypes(@RequestBody String query) {
ExecutionResult result = graphQL.execute(query);
return new ResponseEntity<Object>(result, HttpStatus.OK);
}
}
当我将POST
设为http://localhost:50238/getDataTypes
时
与身体:
{
getDataTypes {
}
}
我得到"errorType": "InvalidSyntax",
作为回应。
答案 0 :(得分:2)
这是无效查询,因为您的括号没有内容(即APP_INITIALIZER
)。您的模式建议查询应该更简单:
{ }