我正在尝试用graphql-ruby实现一个联合类型。
我遵循了official documentation,但得到了下面列出的错误。
这是我当前的代码。
module Types
class AudioClipType < Types::BaseObject
field :id, Int, null: false
field :duration, Int, null: false
end
end
module Types
class MovieClipType < Types::BaseObject
field :id, Int, null: false
field :previewURL, String, null: false
field :resolution, Int, null: false
end
end
module Types
class MediaItemType < Types::BaseUnion
possible_types Types::AudioClipType, Types::MovieClipType
def self.resolve_type(object, context)
if object.is_a?(AudioClip)
Types::AudioClipType
else
Types::MovieClipType
end
end
end
end
module Types
class PostType < Types::BaseObject
description 'Post'
field :id, Int, null: false
field :media_item, Types::MediaItemType, null: true
end
end下面是graphql查询。
{
posts {
id
mediaItem {
__typename
... on AudioClip {
id
duration
}
... on MovieClip {
id
previewURL
resolution
}
}
}
}当我发送查询时,我得到了以下错误。
Failed to implement Post.mediaItem, tried:
- `Types::PostType#media_item`, which did not exist
- `Post#media_item`, which did not exist
- Looking up hash key `:media_item` or `"media_item"` on `#<Post:0x007fb385769428>`, but it wasn't a Hash
To implement this field, define one of the methods above (and check for typos找不到任何打字错误或其他东西。
我是不是遗漏了什么??
发布于 2020-01-28 05:24:33
您没有定义父类型(您的联盟的超类)。
所以添加
class Types::BaseUnion < GraphQL::Schema::Union
end现在你的继承链将保持一致。
https://stackoverflow.com/questions/58351105
复制相似问题