interface - 界面中包含的 Graphql 类型未添加到 graphene-django 中的模式
问题描述
我有一个由两种具体类型实现的接口类型
interface InterfaceType {
id: ID!
name: String!
}
type Type1 implements InterfaceType {
aField: String
}
type Type2 implements InterfaceType {
anotherField: String
}
使用石墨烯-django:
class InterfaceType(graphene.Interface):
id = graphene.ID(required=True)
name = graphene.String(required=True)
class Type1(graphene_django.types.DjangoObjectType):
a_field = graphene.String(required=False)
class Meta:
model = Model1
interfaces = (InterfaceType,)
class Type2(graphene_django.types.DjangoObjectType):
another_field = graphene.String(required=False)
class Meta:
model = Model2
interfaces = (InterfaceType,)
只要某些查询或突变直接使用Type1
和,这就会起作用Type2
。但在我的情况下,它们只是通过InterfaceType
.
问题是当我尝试请求aField
或anotherField
通过内联片段时:
query {
interfaceQuery {
id
name
...on Type1 {
aField
}
...on Type2 {
anotherField
}
}
使用反应阿波罗:
import gql from 'graphql-tag';
const interfaceQuery = gql`
query {
interfaceQuery {
id
name
... on Type1 {
aField
}
... on Type2 {
anotherField
}
}
}
`;
我得到错误"Unknown type "Type1". Perhaps you meant ..."
这就像类型没有添加到架构中,因为它们没有直接使用 - 但我仍然需要它们才能查询aField
和anotherField
.
你能发现上面的错误吗?
解决方案
尝试显式地将Type1
和添加Type2
到您的架构中。石墨烯需要你的一点点指导才能知道你想将这些类型添加到你的模式中。
schema = graphene.Schema(
query=Query,
mutation=Mutation,
types=[
Type1,
Type2,
]
)
文档中也提到了这一点(尽管我猜有点容易忽略)。当您查看示例代码时,有这样一行:
schema = graphene.Schema(query=Query, types=[Human, Droid])
因此,这(顺便说一句)可能是一个很好的例子,说明为什么即使您可以写行长,最好不要将某些内容折叠成一行。
推荐阅读
- spring-boot - 重复订阅标识符 STOMP
- sql - 将新列和变量行添加到 SQL 选择查询作为输出并在其中执行算术运算
- c++ - C/C++ 跨平台 Timer As csharp System.Threading.Timer no boost 或其他
- flutter - 如何使用 CustomPainter 绘制形状以及每个函数的值如何工作?(又名cubicto beziers quadraticbezier等)
- c - 在 cmd 中打印和存储西班牙语字符(á、é、í、ñ...)
- python - 使用 Django Abstract User 时密码没有被散列
- asp.net - 如何访问共享项目中的自动映射器配置文件?
- c++ - 表达式必须是可修改的左值 - 设置固定长度数组的动态数组的元素时
- java - WindowManager.LayoutParams.FLAG-LAYOUT-IN-SCREEN 如何在 android 应用程序中绘制导航栏?
- android - 如何在 startDestination 片段上加载 viewModel