1# frozen_string_literal: true 2 3module Resolvers 4 class UserResolver < BaseResolver 5 description 'Retrieve a single user' 6 7 type Types::UserType, null: true 8 9 argument :id, Types::GlobalIDType[User], 10 required: false, 11 description: 'ID of the User.' 12 13 argument :username, GraphQL::Types::String, 14 required: false, 15 description: 'Username of the User.' 16 17 def ready?(id: nil, username: nil) 18 unless id.present? ^ username.present? 19 raise Gitlab::Graphql::Errors::ArgumentError, 'Provide either a single username or id' 20 end 21 22 super 23 end 24 25 def resolve(id: nil, username: nil) 26 if id 27 GitlabSchema.object_from_id(id, expected_type: User) 28 else 29 batch_load(username) 30 end 31 end 32 33 private 34 35 def batch_load(username) 36 BatchLoader::GraphQL.for(username).batch do |usernames, loader| 37 User.by_username(usernames).each do |user| 38 loader.call(user.username, user) 39 end 40 end 41 end 42 end 43end 44