Ruby on rails 轨道&x2B;GraphQL-未知属性';字符';游戏
我对rails和graphql都是新手。我试图实现一些简单的API,包含3种类型的对象、游戏、角色和引号。每个游戏可以有多个角色。以下是我写的类型和突变。(问题在于游戏和角色,所以我只粘贴它们。) 游戏类型:Ruby on rails 轨道&x2B;GraphQL-未知属性';字符';游戏,ruby-on-rails,ruby,graphql,Ruby On Rails,Ruby,Graphql,我对rails和graphql都是新手。我试图实现一些简单的API,包含3种类型的对象、游戏、角色和引号。每个游戏可以有多个角色。以下是我写的类型和突变。(问题在于游戏和角色,所以我只粘贴它们。) 游戏类型: module Types class GameType < Types::BaseObject field :id, ID, null: false field :name, String, null: false field :characters,
module Types
class GameType < Types::BaseObject
field :id, ID, null: false
field :name, String, null: false
field :characters, [Types::CharacterType], null: true do
#pagination, and cap number of characters retrieved to 20
argument :first, Integer, default_value: 20, required: false, prepare: ->(limit, ctx) {[limit, 20].min}
argument :offset, Integer, default_value: 0, required: false
end
field :quotes, [Types::QuoteType], null: true do
#pagination, and cap number of quotes retrieved to 20
argument :first, Integer, default_value: 20, required: false, prepare: ->(limit, ctx) {[limit, 20].min}
argument :offset, Integer, default_value: 0, required: false
end
field :character_count, Integer, null: true
field :quote_count, Integer, null: true
def characters(first:, offset:)
object.characters.first(first).offset(offset)
end
def quotes(first:, offset:)
object.quotes.first(first).offset(offset)
end
def character_count
object.characters.count
end
def quote_count
object.quotes.count
end
end
end
CreateGame工作正常,但CreateCharacter给了我此错误(CreateCharacter中已标记行):
未定义的方法“字符”#
/var/lib/gems/2.5.0/gems/activemodel-6.0.2.2/lib/active\u model/attribute\u methods.rb:431:in'method\u missing'
/home/krypt/myStuff/projects/gameq/gameq_api/app/graphql/mutations/create_character.rb:19:in'resolve'
我该如何解决这个问题?谢谢 在角色模型中,您与游戏有关联:
class Character < ApplicationRecord
belongs_to :game
end
这将生成您缺少的Game#characters
方法。Rails魔术在这里再次发挥作用。因为您使用了名称characters
,它将知道如何查找所有Character
记录,这些记录具有指向游戏的游戏id
。它如何知道使用游戏id
作为外键?使用类的名称-类游戏
class Mutations::CreateGame < Mutations::BaseMutation
argument :name, String, required: true
field :game, Types::GameType, null: false
field :errors, [String], null: false
def resolve(name:)
game = Game.new(name: name)
if game.save
# Successful creation, return the created object with no errors
{
game: game,
errors: [],
}
else
# Failed save, return the errors to the client
{
game: nil,
errors: game.errors.full_messages
}
end
end
end
class Mutations::CreateCharacter < Mutations::BaseMutation
argument :name, String, required: true
argument :gameId, Integer, required: true
field :character, Types::CharacterType, null: false
field :errors, [String], null: false
def resolve(name:, gameId:)
game = Game.find(gameId)
if game.nil?
return {
character: nil,
errors: game.errors.full_messages,
}
end
character = Character.new(name: name, game: game)
game = game.update(characters: game.characters + [character])
^^^^^^ error on this line
if character.save
# Successful creation, return the created object with no errors
{
character: character,
errors: [],
}
else
# Failed save, return the errors to the client
{
character: nil,
errors: character.errors.full_messages + game.errors.full_messages
}
end
end
end
class Game < ApplicationRecord
end
class Character < ApplicationRecord
belongs_to :game
end
ActiveRecord::Schema.define(version: 2020_04_30_071223) do
create_table "characters", force: :cascade do |t|
t.integer "game_id", null: false
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["game_id"], name: "index_characters_on_game_id"
end
create_table "games", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "quotes", force: :cascade do |t|
t.integer "game_id", null: false
t.integer "character_id", null: false
t.string "text"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["character_id"], name: "index_quotes_on_character_id"
t.index ["game_id"], name: "index_quotes_on_game_id"
end
add_foreign_key "characters", "games"
add_foreign_key "quotes", "characters"
add_foreign_key "quotes", "games"
end
undefined method `characters' for #<Game:0x00007fdf4c207c30>
/var/lib/gems/2.5.0/gems/activemodel-6.0.2.2/lib/active_model/attribute_methods.rb:431:in `method_missing'
/home/krypt/myStuff/projects/gameq/gameq_api/app/graphql/mutations/create_character.rb:19:in `resolve'
class Character < ApplicationRecord
belongs_to :game
end
class Game < ApplicationRecord
has_many :characters
end