在 graphql-ruby 中定义输入类型

Dor*_*Ron 5 ruby-on-rails graphql react-apollo graphql-ruby

我正在尝试使用 graphql-ruby 和 rails 为过滤器实现输入类型。

基本输入类型如下所示:

module Types
  class BaseInputObject < GraphQL::Schema::InputObject
  end
end
Run Code Online (Sandbox Code Playgroud)

我自己的输入类型看起来像:

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
Run Code Online (Sandbox Code Playgroud)

query_type 的方法标题如下所示:

    field :filtered_photos, [Types::PhotoType], null: true do
      argument :filters, Types::PhotoFilterType, 'filters for photo', required: true
    end
Run Code Online (Sandbox Code Playgroud)

查询如下:

const FILTER_QUERY = gql`
  query getFilteredPhotos($filters: PhotoFilterType!) {
    filteredPhotos(filters: $filters) {
      id
      title
      width
      height
    }
  }
`
Run Code Online (Sandbox Code Playgroud)

使用 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
        }
      }
    })
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

{消息:“PhotoFilterType 不是定义的输入类型(在 $filters 上)”,...}

但是当我与 rails 控制台交互时,我看到:

irb(main):004:0> Types::PhotoFilterType
=> Types::PhotoFilterType
Run Code Online (Sandbox Code Playgroud)

所以我不认为未定义的类型是一个问题。

知道出了什么问题以及如何解决吗?提前致谢。

Dor*_*Ron 4

通过更改解决了问题:

const FILTER_QUERY = gql`
  query getFilteredPhotos($filters: PhotoFilter!) { // not PhotoFilterType
    filteredPhotos(filters: $filters) {
      id
      title
      width
      height
    }
  }
`
Run Code Online (Sandbox Code Playgroud)