【问题标题】:Graphql return enum collectionGraphql 返回枚举集合
【发布时间】:2019-06-08 15:39:32
【问题描述】:

我想使用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 } 工作,谢谢。

标签: java spring-boot graphql


【解决方案1】:

这是一个无效的查询,因为您的大括号没有内容(即{ })。您的架构表明查询应该更简单:

{ getDataTypes }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-10
    • 2021-07-12
    • 1970-01-01
    • 2020-01-08
    • 2015-11-28
    • 2015-10-14
    • 2013-01-25
    • 2011-04-09
    相关资源
    最近更新 更多