diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /app/graphql/resolvers/user_resolver.rb | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'app/graphql/resolvers/user_resolver.rb')
-rw-r--r-- | app/graphql/resolvers/user_resolver.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/app/graphql/resolvers/user_resolver.rb b/app/graphql/resolvers/user_resolver.rb new file mode 100644 index 00000000000..a34cecba491 --- /dev/null +++ b/app/graphql/resolvers/user_resolver.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module Resolvers + class UserResolver < BaseResolver + description 'Retrieve a single user' + + type Types::UserType, null: true + + argument :id, GraphQL::ID_TYPE, + required: false, + description: 'ID of the User' + + argument :username, GraphQL::STRING_TYPE, + required: false, + description: 'Username of the User' + + 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) + 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| + loader.call(user.username, user) + end + end + end + end +end |