Skip to content

Commit

Permalink
Merge types and referenced_types from custom schema prototypes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffutter committed Aug 5, 2024
1 parent 2e320b1 commit a8b208e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/absinthe/phase/schema/compile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,30 @@ defmodule Absinthe.Phase.Schema.Compile do
{type_def.identifier, type_def.name}
end)

type_list =
case prototype_schema do
Absinthe.Schema.Prototype ->
type_list

prototype_schema ->
Map.merge(type_list, prototype_schema.__absinthe_types__())
end

referenced_types =
for type_def <- schema.type_definitions,
type_def.__private__[:__absinthe_referenced__],
into: %{},
do: {type_def.identifier, type_def.name}

referenced_types =
case prototype_schema do
Absinthe.Schema.Prototype ->
referenced_types

prototype_schema ->
Map.merge(referenced_types, prototype_schema.__absinthe_types__(:referenced))
end

directive_list =
Map.new(schema.directive_artifacts, fn type_def ->
{type_def.identifier, type_def.name}
Expand Down
12 changes: 12 additions & 0 deletions lib/absinthe/schema/persistent_term.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,25 @@ if Code.ensure_loaded?(:persistent_term) do
|> get()
|> Map.fetch!(:__absinthe_types__)
|> Map.fetch!(:referenced)
|> __maybe_merge_types_from_prototype(schema_mod, :referenced)
end

def __absinthe_types__(schema_mod, group) do
schema_mod
|> get()
|> Map.fetch!(:__absinthe_types__)
|> Map.fetch!(group)
|> __maybe_merge_types_from_prototype(schema_mod, group)
end

defp __maybe_merge_types_from_prototype(types, schema_mod, group) do
prototype_schema_mod = schema_mod.__absinthe_prototype_schema__()

if prototype_schema_mod == Absinthe.Schema.Prototype do
types
else
Map.merge(types, prototype_schema_mod.__absinthe_types__(group))
end
end

def __absinthe_directives__(schema_mod) do
Expand Down
33 changes: 32 additions & 1 deletion test/absinthe/introspection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ defmodule Absinthe.IntrospectionTest do
serialize &Utils.serialize/1
end

scalar :_underscore_normal_string, name: "_UnderscoreNormalString" do
parse &Utils.parse/1
serialize &Utils.serialize/1
end

enum :color_channel do
description "The selected color channel"
value :red, as: :r, description: "Color Red"
Expand All @@ -122,6 +127,7 @@ defmodule Absinthe.IntrospectionTest do
arg :complex, :complex
arg :normal_string, :normal_string
arg :color_channel, :color_channel
arg :_underscore_normal_string, :_underscore_normal_string

on [:field]
end
Expand All @@ -142,6 +148,9 @@ defmodule Absinthe.IntrospectionTest do
"""
query IntrospectionQuery {
__schema {
types {
name
}
directives {
name
args {
Expand All @@ -166,7 +175,8 @@ defmodule Absinthe.IntrospectionTest do
"directives" => [
%{"name" => "complexDirective", "args" => complex_directive_args}
| _
]
],
"types" => types
}
}
}} = result
Expand All @@ -184,6 +194,8 @@ defmodule Absinthe.IntrospectionTest do
}
)

assert Enum.member?(types, %{"name" => "Complex"})

assert Enum.member?(
complex_directive_args,
%{
Expand All @@ -197,6 +209,8 @@ defmodule Absinthe.IntrospectionTest do
}
)

assert Enum.member?(types, %{"name" => "NormalString"})

assert Enum.member?(
complex_directive_args,
%{
Expand All @@ -209,6 +223,23 @@ defmodule Absinthe.IntrospectionTest do
"name" => "colorChannel"
}
)

assert Enum.member?(types, %{"name" => "ColorChannel"})

assert Enum.member?(
complex_directive_args,
%{
"type" => %{
"kind" => "SCALAR",
"name" => "_UnderscoreNormalString"
},
"defaultValue" => nil,
"description" => nil,
"name" => "_underscoreNormalString"
}
)

assert Enum.member?(types, %{"name" => "_UnderscoreNormalString"})
end
end

Expand Down

0 comments on commit a8b208e

Please sign in to comment.