Graphql不返回Rails中的关联对象

时间:2018-04-25 15:57:34

标签: ruby-on-rails graphql

我试图返回链接到关联的GraphQL查询,但我得到以下信息: enter image description here

在查询中,我希望演员返回所有演员(有3个),而我只是得到名字:"演员"?

非常感谢任何帮助!

Rails服务器日志: enter image description here

我的代码如下:

应用程序/ graphql /类型/ movie_type.rb

Types::MovieType = GraphQL::ObjectType.define do
  name "Movie"

  field :id, !types.ID
  field :title, !types.String
  field :description, !types.String
  field :release_year, !types.Int
  field :actors do
    type Types::ActorType 
    argument :size, types.Int, default_value: 10
    resolve -> (movie, args, ctx) {
      movie.actors.limit(args[:size])
    }
  end
end

应用程序/ graphql /类型/ query_type.rb

field :movie, Types::MovieType do
    argument :id, !types.ID
    resolve -> (obj, args, ctx) {
      Movie.find(args[:id])
    }
end

应用程序/模型/ movie.rb

class Movie < ApplicationRecord
  has_and_belongs_to_many :actors
  has_and_belongs_to_many :users
end

schema.rb

ActiveRecord::Schema.define(version: 20180424112411) do

  create_table "actors", force: :cascade do |t|
    t.string "name"
    t.string "image"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "actors_movies", id: false, force: :cascade do |t|
    t.integer "actor_id", null: false
    t.integer "movie_id", null: false
  end

  create_table "movies", force: :cascade do |t|
    t.string "title"
    t.integer "release_year"
    t.text "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
end

1 个答案:

答案 0 :(得分:0)

我找到了答案,在movie_type.rb中我需要这个改变:

类型类型[Types :: ActorType]

我想这肯定是因为将它设置为返回一个数组并且没有返回单个实体。