Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 通过避免N+;graphql中的1个查询_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 通过避免N+;graphql中的1个查询

Ruby on rails 通过避免N+;graphql中的1个查询,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在使用graphiqlubygem来使用graphql。我正在尝试获取用户列表及其用户配置文件和公司。但是,它需要花费5分钟以上的时间,并多次点击api。它甚至使我的服务器崩溃。有人能帮我吗 这就是我所做的 module Queries module Users extend ActiveSupport::Concern include GraphQL::Schema::Member::GraphQLTypeNames included do fie

我正在使用
graphiqluby
gem来使用graphql。我正在尝试获取用户列表及其用户配置文件和公司。但是,它需要花费5分钟以上的时间,并多次点击api。它甚至使我的服务器崩溃。有人能帮我吗

这就是我所做的

module Queries
  module Users
    extend ActiveSupport::Concern
    include GraphQL::Schema::Member::GraphQLTypeNames

    included do
      field :users, [Types::UserType], null: false
      field :user, Types::UserType, null: false do
        argument :id, ID, required: true
      end
      field :currentUser, Types::UserType, null: false
    end

    # Get all users list except admins
    def users
      User.where.not(roles: ['admin'])
      # context[:current_user].logs.info!('User Data Fetched') && user
    end

    def user(id:)
      User.find(id)
    end

    def current_user
      context[:current_user]
    end
  end
end


module Types
  class UserType < Types::BaseObject
    field :id, ID, null: false
    field :email, String, null: false
    field :user_profile, Types::UserProfileType, null: false
    field :grants, [Types::GrantType], null: true
    field :companies, [Types::CompanyType], null: true
    field :my_companies_with_grants, [Types::CompanyGrantType], null: true

    def grants
      Grant.where(user_id: object.id)
    end

    def my_companies_with_grants
      current_user = User.find_by(id: object.id)
      company = current_user.companies
      userid = current_user.id
      company.map {|comp| { company: comp, grants: comp.get_user_grants(userid), company_id: comp.id } }
    end
  end
end
为什么加载所有用户花费的时间太长?怎样才能使它更有效率

更新

class ApiSchema < GraphQL::Schema
  mutation(Types::MutationType)
  query(Types::QueryType)

  use GraphQL::Batch
end
但是我得到了
“消息”:“字段'userProfile'缺少必需的参数:ids”
问题


我设置了
graphql批处理
,但无法按照我使用的方式(类型和解析程序)确定应该在哪里使用RecordLoader

有多少用户,有多少授权?你查过日志了吗?最有可能是N+1查询。看起来像是N+1查询。有300多个用户,授权也差不多。我发现了graphql batch gem,但不知道如何实现它。也许最好问一个关于批量加载的新问题。我认为编辑同样的内容听起来不错。有多少用户,有多少授权?你查过日志了吗?最有可能是N+1查询。看起来像是N+1查询。有300多个用户,grants也差不多。我发现了graphql批处理gem,但不知道如何实现它。也许最好问一个关于批处理加载的新问题。我认为编辑同样的内容听起来不错。
class ApiSchema < GraphQL::Schema
  mutation(Types::MutationType)
  query(Types::QueryType)

  use GraphQL::Batch
end
class RecordLoader < GraphQL::Batch::Loader
  def initialize(model)
    @model = model
  end

  def perform(ids)
    @model.where(id: ids).each { |record| fulfill(record.id, record) }
    ids.each { |id| fulfill(id, nil) unless fulfilled?(id) }
  end
end
 field :user_profile, [Types::UserProfileType, null: true], null: false do
      argument :id, [ID], required: true
 end



 def user_profile(ids:)
      RecordLoader.for(UserProfile).load_many(ids)
  end