Graphql返回枚举集合

时间:2019-01-14 16:08:34

标签: java spring-boot graphql

我想使用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", 作为回应。

1 个答案:

答案 0 :(得分:2)

这是无效查询,因为您的括号没有内容(即APP_INITIALIZER)。您的模式建议查询应该更简单:

{  }