2020-06-23 00:09:42 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Resolvers
|
|
|
|
class UserResolver < BaseResolver
|
2022-07-23 23:45:48 +05:30
|
|
|
include Gitlab::Graphql::Authorize::AuthorizeResource
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
description 'Retrieve a single user'
|
|
|
|
|
|
|
|
type Types::UserType, null: true
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
argument :id, Types::GlobalIDType[User],
|
2020-06-23 00:09:42 +05:30
|
|
|
required: false,
|
2021-03-08 18:12:59 +05:30
|
|
|
description: 'ID of the User.'
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
argument :username, GraphQL::Types::String,
|
2020-06-23 00:09:42 +05:30
|
|
|
required: false,
|
2021-03-08 18:12:59 +05:30
|
|
|
description: 'Username of the User.'
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
def ready?(id: nil, username: nil)
|
|
|
|
unless id.present? ^ username.present?
|
|
|
|
raise Gitlab::Graphql::Errors::ArgumentError, 'Provide either a single username or id'
|
|
|
|
end
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve(id: nil, username: nil)
|
2022-07-23 23:45:48 +05:30
|
|
|
authorize!
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
if id
|
|
|
|
GitlabSchema.object_from_id(id, expected_type: User)
|
|
|
|
else
|
|
|
|
batch_load(username)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def batch_load(username)
|
|
|
|
BatchLoader::GraphQL.for(username).batch do |usernames, loader|
|
|
|
|
User.by_username(usernames).each do |user|
|
2023-07-09 08:55:56 +05:30
|
|
|
loader.call(username, user)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
|
|
|
def authorize!
|
|
|
|
raise_resource_not_available_error! unless context[:current_user].present?
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end
|