Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add role search #229

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/console/graphql/resolvers/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ defmodule Console.GraphQl.Resolvers.User do

def list_roles(args, _) do
Role.ordered()
|> maybe_search(Role, args)
|> paginate(args)
end

Expand Down
1 change: 1 addition & 0 deletions lib/console/graphql/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ defmodule Console.GraphQl.Users do

connection field :roles, node_type: :role do
middleware Authenticated
arg :q, :string

resolve &User.list_roles/2
end
Expand Down
4 changes: 4 additions & 0 deletions lib/console/schema/role.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ defmodule Console.Schema.Role do
timestamps()
end

def search(query \\ __MODULE__, q) do
from(r in query, where: ilike(r.name, ^"%#{q}%"))
end

def ordered(query \\ __MODULE__, order \\ [asc: :name]) do
from(r in query, order_by: ^order)
end
Expand Down
49 changes: 22 additions & 27 deletions test/console/graphql/queries/user_queries_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ defmodule Console.GraphQl.UserQueriesTest do
{:ok, %{data: %{"users" => found}}} = run_query("""
query {
users(first: 5) {
edges {
node {
id
}
}
edges { node { id } }
}
}
""", %{}, %{current_user: hd(users)})
Expand All @@ -29,11 +25,7 @@ defmodule Console.GraphQl.UserQueriesTest do
{:ok, %{data: %{"users" => found}}} = run_query("""
query Search($q: String) {
users(q: $q, first: 5) {
edges {
node {
id
}
}
edges { node { id } }
}
}
""", %{"q" => "search"}, %{current_user: user})
Expand Down Expand Up @@ -65,9 +57,7 @@ defmodule Console.GraphQl.UserQueriesTest do

{:ok, %{data: %{"invite" => found}}} = run_query("""
query Invite($id: String!) {
invite(id: $id) {
email
}
invite(id: $id) { email }
}
""", %{"id" => "secure"})

Expand All @@ -82,9 +72,7 @@ defmodule Console.GraphQl.UserQueriesTest do
{:ok, %{data: %{"groups" => found}}} = run_query("""
query {
groups(first: 5) {
edges {
node { id }
}
edges { node { id } }
}
}
""", %{}, %{current_user: insert(:user)})
Expand All @@ -100,11 +88,7 @@ defmodule Console.GraphQl.UserQueriesTest do
{:ok, %{data: %{"groups" => found}}} = run_query("""
query Search($q: String) {
groups(q: $q, first: 5) {
edges {
node {
id
}
}
edges { node { id } }
}
}
""", %{"q" => "search"}, %{current_user: insert(:user)})
Expand All @@ -123,9 +107,7 @@ defmodule Console.GraphQl.UserQueriesTest do
{:ok, %{data: %{"groupMembers" => found}}} = run_query("""
query Members($id: ID!) {
groupMembers(groupId: $id, first: 5) {
edges {
node { id }
}
edges { node { id } }
}
}
""", %{"id" => group.id}, %{current_user: insert(:user)})
Expand All @@ -142,16 +124,29 @@ defmodule Console.GraphQl.UserQueriesTest do
{:ok, %{data: %{"roles" => found}}} = run_query("""
query {
roles(first: 5) {
edges {
node { id }
}
edges { node { id } }
}
}
""", %{}, %{current_user: insert(:user)})

assert from_connection(found)
|> ids_equal(roles)
end

test "it can search roles by name" do
insert_list(3, :role)
role = insert(:role, name: "query")

{:ok, %{data: %{"roles" => %{"edges" => [%{"node" => found}]}}}} = run_query("""
query Search($q: String) {
roles(first: 5, q: $q) {
edges { node { id } }
}
}
""", %{"q" => "quer"}, %{current_user: insert(:user)})

assert found["id"] == role.id
end
end

describe "externalToken" do
Expand Down