-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow providing descriptions for enum values (#117)
* chore: update ash to use Ash.Type.Enum descriptions Contextually, remove an unreachable clause that caused dyalizer to fail, see ash-project/ash#873 for the commit that made it unreachable * feat: allow providing descriptions for enum values If the module exports `graphql_describe_enum_value/1`, use that. Otherwise, fallback to the Ash.Type.Enum value descriptions introduced in ash-project/ash#940. Close #116
- Loading branch information
Showing
9 changed files
with
158 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
defmodule AshGraphql.EnumTest do | ||
use ExUnit.Case, async: false | ||
|
||
setup do | ||
on_exit(fn -> | ||
Application.delete_env(:ash_graphql, AshGraphql.Test.Api) | ||
|
||
AshGraphql.TestHelpers.stop_ets() | ||
end) | ||
end | ||
|
||
test "enum without value descriptions returns a nil description" do | ||
{:ok, %{data: data}} = | ||
""" | ||
query { | ||
__type(name: "StatusEnum") { | ||
enumValues { | ||
name | ||
description | ||
} | ||
} | ||
} | ||
""" | ||
|> Absinthe.run(AshGraphql.Test.Schema) | ||
|
||
assert %{"name" => "CLOSED", "description" => nil} = | ||
data["__type"]["enumValues"] | ||
|> Enum.find(fn value -> value["name"] == "CLOSED" end) | ||
|
||
assert %{"name" => "OPEN", "description" => nil} = | ||
data["__type"]["enumValues"] | ||
|> Enum.find(fn value -> value["name"] == "OPEN" end) | ||
end | ||
|
||
test "Ash.Type.Enum value descriptions are used as description source" do | ||
{:ok, %{data: data}} = | ||
""" | ||
query { | ||
__type(name: "EnumWithAshDescription") { | ||
enumValues { | ||
name | ||
description | ||
} | ||
} | ||
} | ||
""" | ||
|> Absinthe.run(AshGraphql.Test.Schema) | ||
|
||
assert %{"name" => "FIZZ", "description" => "A fizz"} = | ||
data["__type"]["enumValues"] | ||
|> Enum.find(fn value -> value["name"] == "FIZZ" end) | ||
|
||
assert %{"name" => "BUZZ", "description" => "A buzz"} = | ||
data["__type"]["enumValues"] | ||
|> Enum.find(fn value -> value["name"] == "BUZZ" end) | ||
end | ||
|
||
test "graphql_describe_enum_value/1 overrides Ash.Type.Enum descriptions" do | ||
{:ok, %{data: data}} = | ||
""" | ||
query { | ||
__type(name: "EnumWithAshGraphqlDescription") { | ||
enumValues { | ||
name | ||
description | ||
} | ||
} | ||
} | ||
""" | ||
|> Absinthe.run(AshGraphql.Test.Schema) | ||
|
||
assert %{"name" => "FOO", "description" => "A foo"} = | ||
data["__type"]["enumValues"] | ||
|> Enum.find(fn value -> value["name"] == "FOO" end) | ||
|
||
assert %{"name" => "BAR", "description" => "A bar"} = | ||
data["__type"]["enumValues"] | ||
|> Enum.find(fn value -> value["name"] == "BAR" end) | ||
|
||
assert %{"name" => "NO_DESCRIPTION", "description" => nil} = | ||
data["__type"]["enumValues"] | ||
|> Enum.find(fn value -> value["name"] == "NO_DESCRIPTION" end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule AshGraphql.Test.EnumWithAshDescription do | ||
@moduledoc false | ||
use Ash.Type.Enum, | ||
values: [ | ||
fizz: "A fizz", | ||
buzz: "A buzz" | ||
] | ||
|
||
def graphql_type, do: :enum_with_ash_description | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule AshGraphql.Test.EnumWithAshGraphqlDescription do | ||
@moduledoc false | ||
use Ash.Type.Enum, | ||
values: [ | ||
foo: "This should get ignored by AshGraphQL", | ||
bar: "This too", | ||
no_description: "And also this" | ||
] | ||
|
||
def graphql_type, do: :enum_with_ash_graphql_description | ||
|
||
def graphql_describe_enum_value(:foo), do: "A foo" | ||
def graphql_describe_enum_value(:bar), do: "A bar" | ||
def graphql_describe_enum_value(_), do: nil | ||
end |