我有一个由两种具体类型实现的接口类型
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
.
你能发现上面的错误吗?