带有Rails的GraphQL gem,似乎找不到正确的类型定义?

时间:2018-07-29 23:29:06

标签: ruby-on-rails graphql

我已经这样定义了一个UserType:

class Types::UserType < GraphQL::Schema::Object
  field :id, ID, null: false
  field :username, String, null: false
  field :full_name, String, null: true
end

这些字段中的每一个都存在于Rails模型上,并且在GraphQL Gem 1.8之前的升级中,我能够在查询中使用full_name很好。

当我运行查询时:

query {
  users {
    username
    id
    full_name
  }
}

我得到:"message": "Field 'full_name' doesn't exist on type 'User'",

如果删除full_name,则会得到期望的数据。我以什么方式错误地解决了这个问题?作为参考,我的QueryType定义为:

class Types::QueryType < GraphQL::Schema::Object
  # Add root-level fields here.
  # They will be entry points for queries on your schema.
  field :users, [UserType, null: true], null: false do
    argument :id, Integer, required: false
  end

  def users(**args)
    args[:id]
    if args[:id]
      User.where(id: args[:id])
    else
      User.all
    end
  end
end

1 个答案:

答案 0 :(得分:2)

我认为问题在于您的查询中full_name应该是fullName。使用1.8.x版本时,架构中的字段会自动显示。

  

字段和参数名称应作为惯例强调。它们将被转换为基础GraphQL类型的camelCase,而将成为架构本身中的camelCase。

-http://graphql-ruby.org/type_definitions/objects.html