Skip to content

Commit

Permalink
refactor out a defp filter_deprecated/2 to reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
jgautsch committed Jan 5, 2024
1 parent 53dd591 commit 2323cbe
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions lib/absinthe/type/built_ins/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
args =
source.args
|> Map.values()
|> Enum.filter(fn %{deprecation: is_deprecated} ->
!is_deprecated || show_deprecated
end)
|> filter_deprecated(show_deprecated)
|> Enum.sort_by(& &1.identifier)

{:ok, args}
Expand Down Expand Up @@ -117,18 +115,9 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
when str in [Absinthe.Type.Object, Absinthe.Type.Interface] ->
result =
fields
|> Enum.flat_map(fn {_, %{deprecation: is_deprecated} = field} ->
cond do
Absinthe.Type.introspection?(field) ->
[]

!is_deprecated || show_deprecated ->
[field]

true ->
[]
end
end)
|> Map.values()
|> filter_deprecated(show_deprecated)
|> Enum.filter(&(!Absinthe.Type.introspection?(&1)))
|> Enum.sort_by(& &1.identifier)

{:ok, result}
Expand Down Expand Up @@ -184,9 +173,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
result =
values
|> Map.values()
|> Enum.filter(fn %{deprecation: is_deprecated} ->
!is_deprecated || show_deprecated
end)
|> filter_deprecated(show_deprecated)
|> Enum.sort_by(& &1.value)

{:ok, result}
Expand All @@ -209,9 +196,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
input_fields =
fields
|> Map.values()
|> Enum.filter(fn %{deprecation: is_deprecated} ->
!is_deprecated || show_deprecated
end)
|> filter_deprecated(show_deprecated)
|> Enum.sort_by(& &1.identifier)

{:ok, input_fields}
Expand Down Expand Up @@ -252,9 +237,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
args =
args
|> Map.values()
|> Enum.filter(fn %{deprecation: is_deprecated} ->
!is_deprecated || show_deprecated
end)
|> filter_deprecated(show_deprecated)
|> Enum.sort_by(& &1.identifier)

{:ok, args}
Expand Down Expand Up @@ -409,4 +392,10 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
inspect(Absinthe.Type.Scalar.serialize(sc, value))
end
end

defp filter_deprecated(values, show_deprecated) do
Enum.filter(values, fn %{deprecation: is_deprecated} ->
!is_deprecated || show_deprecated
end)
end
end

0 comments on commit 2323cbe

Please sign in to comment.