我想使用 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",
作为回应。
这是一个无效的查询,因为您的大括号没有内容(即 { }
)。不过,您的架构表明查询应该简单得多:
{ getDataTypes }