我试图用graphql和rails实现过滤器的输入类型。
基本输入类型如下所示:
module Types
class BaseInputObject < GraphQL::Schema::InputObject
end
end我自己的输入类型如下所示:
module Types
class PhotoFilterType < Types::BaseInputObject
argument :attribution, String, "Filters by submitter", required: false
argument :country, String, "Filters by country", required: false
argument :year, Int, "Filters by year", required: false
end
endquery_type的方法头看起来如下所示:
field :filtered_photos, [Types::PhotoType], null: true do
argument :filters, Types::PhotoFilterType, 'filters for photo', required: true
end查询如下:
const FILTER_QUERY = gql`
query getFilteredPhotos($filters: PhotoFilterType!) {
filteredPhotos(filters: $filters) {
id
title
width
height
}
}
`使用react与后端交互-阿波罗,如下所示:
this.props.client.query({
query: FILTER_QUERY,
variables: {
filters: {
attribution: author.length > 0 ? author : '',
country: country.length > 0 ? country : '',
year: year ? year : 9999
}
}
})我得到以下错误
{消息:"PhotoFilterType不是定义的输入类型(在$filters上)“,…}
但是,当我与rails控制台交互时,我看到:
irb(main):004:0> Types::PhotoFilterType
=> Types::PhotoFilterType所以我不认为不确定的类型是个问题。
你知道哪里出了问题吗?怎么解决?提前谢谢。
发布于 2019-11-26 17:32:14
解决问题的办法是:
const FILTER_QUERY = gql`
query getFilteredPhotos($filters: PhotoFilter!) { // not PhotoFilterType
filteredPhotos(filters: $filters) {
id
title
width
height
}
}
`https://stackoverflow.com/questions/59040451
复制相似问题