我们可以在我的 Spring Boot REST api 中调用 GraphQL 查询吗

问题描述

我对 GraphQL 非常陌生,我有一个场景,我必须在微服务中添加新的 REST 端点并在内部调用 GraphQLQuery 以利用服务中的响应负载。

如何实现?

我试过这个但得到异常:

    GraphQLObjectType.Builder queryBuilder = GraphQLObjectType.newObject()
            .name("GetAllCatalogCategories")
            .description("Get All Catalog Categories");
    GraphQLSchema schema = GraphQLSchema.newSchema()
            .query(queryBuilder)
            .build();

    GraphQL graphQL = GraphQL.newGraphQL(schema)
            .build();
    String variableJson = "{ \"catalogueId\": \"9\",\"searchTerm\": \"\",\"allLevels\": true,\"firstCourses\": 5,\"filter\": { \"deliveryMethods\": [],\"supplierIds\": [] } }";
    try {

        TypeReference<HashMap<String,Object>> typeRef = new TypeReference<>() {
        };
        HashMap<String,Object> variableMap = new ObjectMapper().readValue(variableJson,typeRef);

        ExecutionInput executionInput = ExecutionInput.newExecutionInput()
                .query("query GetAllCatalogCategories($catalogueId: ID!,$searchTerm: String,$allLevels: Boolean,$firstCourses: Int!,$filter: CatalogueCourseFilterInput!) { catalogue(catalogueId: $catalogueId) { id courses(first: 0,searchTerm: $searchTerm,courseGroup: CATEGORIZED,catalogueCourseFilterInput: $filter) { totalCount } categories(searchTerm: $searchTerm,allLevels: $allLevels,catalogueCourseFilterInput: $filter) { totalCount nodes { id name coursesCount parentCategoryId subCategoriesCount courses: courses(first: $firstCourses,catalogueCourseFilterInput: $filter) { totalCount nodes { id code title price{ buyPrice{ publicPrice{ baseCost } clientPrice{ baseCost forecast } kpPrice{ forecast{ cost } budgeted{ cost } } supplierPrice{ budgeted{ cost } forecast{ cost } supplier{ id details{ exclFromSellPriceCalc } } } } costType sellPriceOverride{ perDelegateClientPrice perDelegateKpPrice perDelegatePublicPrice } } deliveryMethod supplier { details { name } } } } } } } }")
                .variables(variableMap)
                .operationName("GetAllCatalogCategories")
                .build();

        ExecutionResult executionResult = graphQL.execute(executionInput);

        Object data = executionResult.getData();
        List<GraphQLError> errors = executionResult.getErrors();
    } catch (JsonProcessingException e) {

    }

这给了我以下异常:

块引用 ExecutionResultImpl{errors=[ValidationError{validationErrorType=UnknownType,queryPath=null,message=UnknownType 类型的验证错误:未知类型 ID,locations=[SourceLocation{line=1,column=45}],description='Unknown type ID'},ValidationError{validationErrorType=UnknownType,message=UnknownType 类型的验证错误: Unknown type Int,locations=[SourceLocation{line=1,column=107}],description='Unknown type Int'},ValidationError{validationErrorType =UnknownType,queryPath=null,message=UnknownType 类型的验证错误:未知类型 CatalogueCourseFilterInput,locations=[SourceLocation{line=1,column=122}],description='Unknown type CatalogueCourseFilterInput'},ValidationError{validationErrorType=FieldUndefined,queryPath =[目录],消息=字段未定义类型的验证错误:“GetAllCatalogCategories”类型中的字段“目录”未定义@“目录”,位置=[SourceLocation{行=1,列=153}],描述=“字段”目录' 在类型 'GetAllC 中atalogCategories' 未定义'}],data=null,dataPresent=false,extensions=null}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...