【问题标题】:Graphql-Ruby Mapping using Active Record使用 Active Record 的 Graphql-Ruby 映射
【发布时间】:2019-10-14 09:44:52
【问题描述】:

我对使用 Rails 完全陌生,我正在尝试使用 ruby​​-graphql 创建一个基本的 crud 应用程序,以对 sqlite3 数据库进行带有活动记录的简单查找查询。 我设置了一个用户类型

  class UserType < Types::BaseObject
    description "A user type "
    field :id, ID, null: false
    field :username, String, null: false
    field :email, String, null: false
    field :email_confirmed, Boolean, null:false
    field :first_name, String , null:false
    field :last_name, String, null:false
    field :biography, String, null:true
    field :avatar, String, null:true
    field :created_at, GraphQL::Types::ISO8601DateTime, null:false
    field :updated_at, GraphQL::Types::ISO8601DateTime, null:false
  end

然后我设置查询:

field :user, UserType, null:false,
      description: "Get a single user" do
      argument :username, String, required: true
      end
    def user(username:)
      User.find(username)
    end

我的查询是:

{
  user(username:"test"){
    username
  }
}

我收到以下错误:

{
  "error": {
    "message": "no implicit conversion of #<Class:0x000000000b975440> into Hash",
    "backtrace": [
      "C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/graphql-1.9.13/lib/graphql/schema/field.rb:519:in `block in resolve'",

如果有人可以帮助我,我将不胜感激。 如何在 Ruby / Rails 中进行这种转换?

【问题讨论】:

  • 这里还有一个问题,.find 用于按 id 查找记录。如果您想按另一个唯一列查找记录,请使用.find_by(username: username)。这也是关键字参数的一种有点奇怪的用法。
  • 谢谢你,max,我实际上不知道正确的用法我只是想破解并学习,同时做更好的方法?
  • 好吧,如果你只有一个论点,那么它是一个位置论点更有意义。我会把这个方法命名为告诉你它实际上做了什么的东西,比如find_user_by_username
  • 当你使用 graphql-ruby graphql-ruby.org/fields/arguments 时是否一样,我只是想按照这里的文档。即使使用 find_by 仍然显示相同的错误
  • 我也尝试使用 find 和 id ,但我仍然遇到同样的问题。如果有人有任何线索或他们认为他们知道发生了什么,我将非常感谢任何建议或建议。

标签: ruby-on-rails ruby graphql-ruby


【解决方案1】:

原来我使用的 Ruby gem 升级到新的 gem 存在问题,它解决了问题。见-https://github.com/rmosolgo/graphql-ruby/issues/2537

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 2011-10-25
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多