程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

SpringBoot 集成 GraphQL 遇到的问题

发布于2021-05-29 22:49     阅读(1286)     评论(0)     点赞(12)     收藏(0)


主要是现在网上关于 GraphQL 的资料比较少,当我在学习的时候出了问题网上也是基本找不到解决方案,所以在此记录下来,后续遇到其他问题也会一一进行记录进来。

一、 GraphQL 插件

其实我们 Idea 安装的 GraphQL 本身就带有代码检查功能,只是它仅限于检查我们写的 .graphql 脚本文件,所以

当 graphql 文件没有问题时

在这里插入图片描述

当 graphql 文件有问题时

比如,我随便写一个错误的语法,我在 root.graphql 中定义的是 Query ,这里继承一个不存在的 Query1 类型,这个时候不光代码会有红色波浪线和标红显示

在这里插入图片描述
插件也会给我们提示
在这里插入图片描述

不过就算插件会给我们错误提示,也需要我们自己仔细根据错误提示分析解决,因为网上资料太少,不像别的框架,拿出去一百度一堆解决方法。

二、后台代码编写方面需要注意的点

1、 接口名一致性

错误信息:

org.springframework.beans.BeanInstantiationException: Failed to instantiate [graphql.kickstart.tools.SchemaParser]: Factory method ‘schemaParser’ threw exception; nested exception is graphql.kickstart.tools.resolver.FieldResolverError: No method or field found as defined in schema :5 with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment] as the last argument), in priority order:

graphql.kickstart.tools.resolver.FieldResolverError: No method or field found as defined in schema :5 with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment] as the last argument), in priority order:

问题截图:

在这里插入图片描述

出现问题原因及解决方法:

graphql 文件中,type Query 或者 type Mutation 定义的方法名,必须与我们后端实现 GraphQLQueryResolver 或 GraphQLMutationResolver 之后写的接口名称一致,如出现此问题请检查是否一致

2、属性名一致性

错误信息:

org.springframework.beans.BeanInstantiationException: Failed to instantiate [graphql.kickstart.tools.SchemaParser]: Factory method ‘schemaParser’ threw exception; nested exception is graphql.kickstart.tools.resolver.FieldResolverError: No method or field found as defined in schema :35 with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment] as the last argument), in priority order:

graphql.kickstart.tools.resolver.FieldResolverError: No method or field found as defined in schema :35 with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment] as the last argument), in priority order:

问题截图:

在这里插入图片描述

出现问题原因及解决方法:

graphql 文件中,定义的对象的属性列名,必须在 Java 实体类中一一进行对应,如出现此问题请检查是否一致或缺少对应属性

三、当我们使用 playground 或者其他图形化工具时

1、查询对象中的对象属性或者集合对象属性

错误信息:

{
“errors”: [
{
“message”: “Validation error of type SubSelectionRequired: Sub selection required for type Teacher of field teacher @ ‘listClazz/teacher’”,
“locations”: []
}
],
“data”: null
}

问题截图:

在这里插入图片描述

出现问题原因及解决方法:

当我们查询的对象中有对象属性或者集合对象属性,我们想要查询对象属性的信息,必须要把这个对象属性中我们想要查询的属性也列出来
比如我这里是一个 teacher 对象,我必须列出 teacher 对象中,我想查询的列,否则我就直接不查询 teacher
比如,写出 teacher 对象 我想要的属性
在这里插入图片描述
或者直接不查 teacher
在这里插入图片描述

2、查询对象中的对象属性或者集合对象属性

错误信息:

{
“error”: “Unexpected end of JSON input”
}

问题截图:

在这里插入图片描述

出现问题原因及解决方法:

当我们使用 input 输入对象时,我们传入的 input 对象参数为 json 类型,出现此问题,多半是我们的 json 格式错误

3、 Query 类型和 Mutation 类型不能同时调用

错误信息:

{
“errors”: [
{
“message”: “Validation error of type LoneAnonymousOperationViolation: Operation addByInput is following anonymous operation.”,
“locations”: []
}
],
“data”: null
}

问题截图:

在这里插入图片描述

出现问题原因及解决方法:

在图形化工具中,Query 类型和 Mutation 类型不能同时调用

原文链接:https://blog.csdn.net/wxw1997a/article/details/117307351



所属网站分类: 技术文章 > 博客

作者:举起你的手来

链接:http://www.javaheidong.com/blog/article/207643/3e422d58e2f247f70009/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

12 0
收藏该文
已收藏

评论内容:(最多支持255个字符)