Commit bb7392b4 authored by Mario de la Ossa's avatar Mario de la Ossa

GraphQL: Add search param to Users resolver

Allows users to pass `search` to search by username and name.
parent f17d1cc4
......@@ -18,10 +18,14 @@ module Resolvers
required: false,
default_value: 'created_desc'
def resolve(ids: nil, usernames: nil, sort: nil)
argument :search, GraphQL::STRING_TYPE,
required: false,
description: "Query to search users by name, username, or primary email."
def resolve(ids: nil, usernames: nil, sort: nil, search: nil)
authorize!
::UsersFinder.new(context[:current_user], finder_params(ids, usernames, sort)).execute
::UsersFinder.new(context[:current_user], finder_params(ids, usernames, sort, search)).execute
end
def ready?(**args)
......@@ -42,11 +46,12 @@ module Resolvers
private
def finder_params(ids, usernames, sort)
def finder_params(ids, usernames, sort, search)
params = {}
params[:sort] = sort if sort
params[:username] = usernames if usernames
params[:id] = parse_gids(ids) if ids
params[:search] = search if search
params
end
......
---
title: Add search param to Users GraphQL type
merge_request: 46609
author:
type: added
......@@ -16708,6 +16708,11 @@ type Query {
"""
last: Int
"""
Query to search users by name, username, or primary email.
"""
search: String
"""
Sort users by this criteria
"""
......
......@@ -48368,6 +48368,16 @@
},
"defaultValue": "created_desc"
},
{
"name": "search",
"description": "Query to search users by name, username, or primary email.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
},
{
"name": "after",
"description": "Returns the elements in the list that come after the specified cursor.",
......@@ -5,8 +5,8 @@ require 'spec_helper'
RSpec.describe Resolvers::UsersResolver do
include GraphqlHelpers
let_it_be(:user1) { create(:user) }
let_it_be(:user2) { create(:user) }
let_it_be(:user1) { create(:user, name: "SomePerson") }
let_it_be(:user2) { create(:user, username: "someone123784") }
describe '#resolve' do
it 'raises an error when read_users_list is not authorized' do
......@@ -43,6 +43,14 @@ RSpec.describe Resolvers::UsersResolver do
).to contain_exactly(user1, user2)
end
end
context 'when a search term is passed' do
it 'returns all users who match', :aggregate_failures do
expect(resolve_users(search: "some")).to contain_exactly(user1, user2)
expect(resolve_users(search: "123784")).to contain_exactly(user2)
expect(resolve_users(search: "someperson")).to contain_exactly(user1)
end
end
end
def resolve_users(args = {})
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment