主要是现在网上关于 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 类型不能同时调用