在 graphql-ruby 中定义输入类型
Defining input types in graphql-ruby
我正在尝试使用 graphql-ruby 和 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
end
query_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-apollo与后端交互如下:
this.props.client.query({
query: FILTER_QUERY,
variables: {
filters: {
attribution: author.length > 0 ? author : '',
country: country.length > 0 ? country : '',
year: year ? year : 9999
}
}
})
我收到以下错误
{message: "PhotoFilterType isn't a defined input type (on $filters)",…}
但是当我与 rails 控制台交互时,我看到:
irb(main):004:0> Types::PhotoFilterType
=> Types::PhotoFilterType
所以我不认为类型未定义是个问题。
知道出了什么问题以及如何解决吗?提前致谢。
问题已通过更改解决:
const FILTER_QUERY = gql`
query getFilteredPhotos($filters: PhotoFilter!) { // not PhotoFilterType
filteredPhotos(filters: $filters) {
id
title
width
height
}
}
`
我正在尝试使用 graphql-ruby 和 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
end
query_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-apollo与后端交互如下:
this.props.client.query({
query: FILTER_QUERY,
variables: {
filters: {
attribution: author.length > 0 ? author : '',
country: country.length > 0 ? country : '',
year: year ? year : 9999
}
}
})
我收到以下错误
{message: "PhotoFilterType isn't a defined input type (on $filters)",…}
但是当我与 rails 控制台交互时,我看到:
irb(main):004:0> Types::PhotoFilterType
=> Types::PhotoFilterType
所以我不认为类型未定义是个问题。
知道出了什么问题以及如何解决吗?提前致谢。
问题已通过更改解决:
const FILTER_QUERY = gql`
query getFilteredPhotos($filters: PhotoFilter!) { // not PhotoFilterType
filteredPhotos(filters: $filters) {
id
title
width
height
}
}
`