【GraphQL】_typename千万不要写成一样的

简介: 【GraphQL】_typename千万不要写成一样的

这个问题我已经经历过两三次了:*graphql 服务端定义接口时,千万不要把每个接口中的_typename写成一样的


在go语言中,graphql 的实现里头, 错误的示例如下:

var responseDimensionReadingType = graphql.NewObject(graphql.ObjectConfig{
    Name: "ResponseCommon",
    Fields: graphql.Fields{
        "code":    &graphql.Field{Type: graphql.Int},
        "content": &graphql.Field{Type: graphql.NewList(dimensionReadingType)},
        "count":   &graphql.Field{Type: graphql.Int},
        "msg":     &graphql.Field{Type: messageType},
    },
    Description: "",
})
var responseDimensionWritingType = graphql.NewObject(graphql.ObjectConfig{
    Name: "ResponseCommon",
    Fields: graphql.Fields{
        "code":    &graphql.Field{Type: graphql.Int},
        "content": &graphql.Field{Type: graphql.NewList(dimensionWritingType)},
        "count":   &graphql.Field{Type: graphql.Int},
        "msg":     &graphql.Field{Type: messageType},
    },
    Description: "",
})


  • 可以看到,这两个graphql类型定义中,都将 Name 写成了 ResponseCommon

这个 Name 对应的就是前面说的 _typenameimage.png

当两个接口(里面的_typename同名)同时存在时就会出错。


image.png

报错如下:

image.png


{"data":null,"errors":[{"message":"Variable \"$from_id\" cannot be non-input type \"String!\".","locations":[{"line":1,"column":34}]},{"message":"Unknown type \"String\".","locations":[{"line":1,"column":34}]},{"message":"Variable \"$from_nickname\" cannot be non-input type \"String!\".","locations":[{"line":1,"column":59}]},{"message":"Unknown type \"String\".","locations":[{"line":1,"column":59}]},{"message":"Variable \"$content\" cannot be non-input type \"String!\".","locations":[{"line":1,"column":78}]},{"message":"Unknown type \"String\".","locations":[{"line":1,"column":78}]},{"message":"Variable \"$from_id\" of type \"\" used in position expecting type \"String!\".","locations":[{"line":1,"column":24},{"line":3,"column":14}]},{"message":"Variable \"$content\" of type \"\" used in position expecting type \"String!\".","locations":[{"line":1,"column":68},{"line":4,"column":14}]},{"message":"Variable \"$from_nickname\" of type \"\" used in position expecting type \"String!\".","locations":[{"line":1,"column":43},{"line":5,"column":20}]}]}
  • 报错信息,我是没明白啥意思,顺着报错信息找问题,一点头绪都没。

调查这个问题时,感觉离谱之处在于:我随便保留两个接口中的其中一个,都不会有问题,两个接口同时出现才会报错。


记忆中,第一次出现这个问题,我debug,debug了一天没找着原因,后来还是整整回滚了一个版本的代码才解决;第二次遇到这个问题,也是花了好久才知道是_typename的事儿。这次必须记下来,警醒自己!




目录
相关文章
|
Java 开发者 Spring
深入理解Spring Boot的@ComponentScan注解
【4月更文挑战第22天】在构建 Spring Boot 应用时,@ComponentScan 是一个不可或缺的工具,它使得组件发现变得自动化和高效。这篇博客将详细介绍 @ComponentScan 的基本概念、关键属性及其在实际开发中的应用。
904 4
|
芯片
STM32CubeMX 串口数据收发
STM32CubeMX 串口数据收发
391 2
|
11月前
哪些情况可能会导致 try-catch 语句不捕获异常?
【10月更文挑战第12天】在实际应用中,可能还会存在其他一些情况导致异常不被捕获。因此,在使用`try-catch`语句时,需要仔细考虑各种可能的情况,以确保异常能够被正确地捕获和处理。
1158 56
|
BI Go
Go语言复数完全攻略 复杂运算不再难!
Go语言复数完全攻略 复杂运算不再难!
478 0
|
JSON JavaScript 前端开发
技术心得:利用JsonSchema校验json数据内容的合规性
技术心得:利用JsonSchema校验json数据内容的合规性
722 0
|
前端开发 JavaScript
前端 CSS 经典:CSS 原子化
前端 CSS 经典:CSS 原子化
223 0
|
Android开发
Android基础知识:什么是Fragment?与Activity的区别是什么?
Android基础知识:什么是Fragment?与Activity的区别是什么?
2394 54
|
算法 编译器 Swift
【Swift开发专栏】Swift与Objective-C的对比
【4月更文挑战第30天】Swift与Objective-C对比:Swift语法简洁,支持元组、泛型和闭包,提高可读性;性能优化,使用LLVM编译器,与Objective-C兼容,便于迁移项目;生态系统活跃,苹果官方支持,丰富资源库。Objective-C虽历史悠久,但逐渐边缘化。对于新项目和开发者,Swift是更佳选择,驱动iOS开发创新。
975 0
|
传感器 JavaScript 前端开发
前端框架Svelte放弃TS,如何使用纯JS实现类型检查?
前端框架Svelte放弃TS,如何使用纯JS实现类型检查?
180 0
|
弹性计算 运维 监控
一文读懂 Serverless 的起源、发展和落地实践
Serverless 适合哪些业务场景?它可以对业务产生何种价值呢?
14877 6
一文读懂 Serverless 的起源、发展和落地实践