Graphqls错误整理

最近公司接到了一个政府项目,需要使用graphql来完成前后端的交互,因为第一次使用踩了不少坑,目前也只是初步的使用,没有深入了解,先记录点遇到的常见bug,后续会继续添加。(后面应该不会添加了,楼主跳槽了)

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.coxautodev.graphql.tools.SchemaParser]: Factory method ‘schemaParser’ threw exception; nested exception is com.coxautodev.graphql.tools.FieldResolverError: No method found with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment] as the last argument), in priority order:stcsm.appropriation.controller.ExportControlle.findStcsmProjectsPage(~page, ~size, ~projectNumber, ~projectName, ~projectUnit, ~manageDepartmentCode, ~categoryCode, ~projectStateCode, ~arrangement)

在graphql中的findStcsmProjectsPage方法传递的参数缺失,检查后端接口与graphql文件对应的接口传递的参数是否一致

Caused by: com.coxautodev.graphql.tools.FieldResolverError: No method or field found with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment] as the last argument), in priority order:stcsm.appropriation.utils.MyPage.starst()

stcsm.appropriation.utils.MyPage.getStarst()
stcsm.appropriation.utils.MyPage.starst
graphql中配置返回对象MyPage中的starst参数在实体类中不存在

stcsm-appropriation -2020-06-15 15:01:47.311 [http-nio-8082-exec-1] WARN graphql.GraphQL - [parseAndValidate,511] - Query failed to validate : 'query getProjectList($userId: String, $page: Int, $size: Int, $projectNumber: String, $projectName: String, $projectUnit: String, $manageDepartmentCode: String, $categoryCode: String, $projectStateCode: String, $arrangement: String) {findStcsmProjectsPage(userId: $userId, page: $page, size: $size, projectNumber: $projectNumber, projectName: $projectName, projectUnit: $projectUnit, manageDepartmentCode: $manageDepartmentCode, categoryCode: $categoryCode, projectStateCode: $projectStateCode, arrangement: $arrangement) { total projectMsg { id __typename } __typename }

前端请求时报错:检查graphql返回参数中的属性与前端需求的属性是否一致

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
假设我们有一个GraphQL接口,在其中定义了一个查询类型`query`,并且有一个名为`getUser`的查询字段,用于查询用户的详细信息。 下面是Java代码示例,用于调用该GraphQL接口并查询用户信息: ```java import graphql.GraphQL; import graphql.schema.DataFetchingEnvironment; import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import graphql.schema.StaticDataFetcher; import graphql.schema.idl.RuntimeWiring; import graphql.schema.idl.SchemaGenerator; import graphql.schema.idl.SchemaParser; import graphql.schema.idl.TypeDefinitionRegistry; import java.io.File; import java.io.IOException; import java.util.Map; import com.fasterxml.jackson.databind.ObjectMapper; public class GraphQLClient { public static void main(String[] args) throws IOException { // 读取GraphQL schema定义 File schemaFile = new File("schema.graphqls"); String schemaString = FileUtils.readFileToString(schemaFile, "UTF-8"); // 创建GraphQL schema解析器 SchemaParser schemaParser = new SchemaParser(); TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schemaString); // 创建运行时绑定 RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring() .type("Query", builder -> builder .dataFetcher("getUser", new StaticDataFetcher(Map.of( "id", 1, "name", "Alice", "email", "alice@example.com" ))) ) .build(); // 创建GraphQL schema生成器 SchemaGenerator schemaGenerator = new SchemaGenerator(); GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring); // 创建GraphQL实例 GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build(); // 定义查询语句 String query = "{ getUser { id name email } }"; // 执行查询 Map<String, Object> result = graphQL.execute(query).getData(); // 输出结果 ObjectMapper objectMapper = new ObjectMapper(); System.out.println(objectMapper.writeValueAsString(result)); } } ``` 在上面的代码中,我们首先读取GraphQL schema定义文件并解析它,然后创建运行时绑定以定义查询字段的数据抓取器。 接下来,我们使用GraphQL schema生成器创建实际的GraphQL schema,并使用它创建一个GraphQL实例来执行查询。 最后,我们定义查询语句,执行查询,并输出结果。在这个示例中,我们只查询了一个用户,但是你可以根据你的需要修改查询语句和数据抓取器来查询更多的信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值