Skip to content

Commit

Permalink
Fix schema description returning description of __schema
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenvanvliet committed Jun 29, 2022
1 parent 8ab3617 commit a97a327
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/absinthe/phase/schema/compile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ defmodule Absinthe.Phase.Schema.Compile do
unquote(Macro.escape(prototype_schema))
end

def __absinthe_schema_declaration__() do
unquote(Macro.escape(schema.schema_declaration))
end

unquote_splicing(metadata)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/absinthe/phase/schema/populate_persistent_term.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ if Code.ensure_loaded?(:persistent_term) do
__absinthe_prototype_schema__: prototype_schema,
__absinthe_type__: types_map,
__absinthe_directive__: directives_map,
__absinthe_reference__: metadata
__absinthe_reference__: metadata,
__absinthe_schema_declaration__: schema.schema_declaration
}

schema_name = opts[:schema] || raise "no schema name provided"
Expand Down
8 changes: 8 additions & 0 deletions lib/absinthe/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ defmodule Absinthe.Schema do
@schema_provider.__absinthe_interface_implementors__(__MODULE__)
end

def __absinthe_schema_declaration__() do
@schema_provider.__absinthe_schema_declaration__(__MODULE__)
end

def __absinthe_prototype_schema__() do
@prototype_schema
end
Expand Down Expand Up @@ -547,6 +551,10 @@ defmodule Absinthe.Schema do
end
end

def schema_declaration(schema) do
schema.__absinthe_schema_declaration__()
end

@doc """
Get all concrete types for union, interface, or object
"""
Expand Down
4 changes: 4 additions & 0 deletions lib/absinthe/schema/compiled.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ defmodule Absinthe.Schema.Compiled do
def __absinthe_interface_implementors__(schema_mod) do
Module.concat([schema_mod, Compiled]).__absinthe_interface_implementors__
end

def __absinthe_schema_declaration__(schema_mod) do
Module.concat([schema_mod, Compiled]).__absinthe_schema_declaration__
end
end
7 changes: 7 additions & 0 deletions lib/absinthe/schema/persistent_term.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ if Code.ensure_loaded?(:persistent_term) do
|> Map.fetch!(:__absinthe_interface_implementors__)
end

def __absinthe_schema_declaration__(schema_mod) do
schema_mod
|> get()
|> Map.fetch!(:__absinthe_schema_declaration__)
end

@dialyzer {:nowarn_function, [get: 1]}
defp get(schema) do
:persistent_term.get({__MODULE__, schema})
Expand All @@ -109,5 +115,6 @@ else
def __absinthe_directives__(_), do: raise(@error)
def __absinthe_interface_implementors__(_), do: raise(@error)
def __absinthe_prototype_schema__(), do: raise(@error)
def __absinthe_schema_declaration__(_), do: raise(@error)
end
end
2 changes: 1 addition & 1 deletion lib/absinthe/type/built_ins/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do

field :description, :string do
resolve(fn _, %{schema: schema} ->
{:ok, Absinthe.Schema.lookup_type(schema, :__schema).description}
{:ok, Absinthe.Schema.schema_declaration(schema).description}
end)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule Elixir.Absinthe.Integration.Execution.Introspection.FullTest do
result = Absinthe.Schema.introspect(Absinthe.Fixtures.ContactSchema)
{:ok, %{data: %{"__schema" => schema}}} = result

assert schema["description"] == "Represents a schema"
assert schema["queryType"]
assert schema["mutationType"]
assert schema["subscriptionType"]
Expand Down
1 change: 0 additions & 1 deletion test/absinthe/schema/sdl_render_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ defmodule Absinthe.Schema.SdlRenderTest do
test "Render SDL from blueprint defined with macros" do
assert Absinthe.Schema.to_sdl(MacroTestSchema) ==
"""
"Represents a schema"
schema {
query: RootQueryType
}
Expand Down
3 changes: 1 addition & 2 deletions test/absinthe/schema/type_system_directive_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ defmodule Absinthe.Schema.TypeSystemDirectiveTest do
end

@macro_schema_sdl """
"Represents a schema"
schema {
schema @feature(name: ":schema") {
query: RootQueryType
}
Expand Down
2 changes: 1 addition & 1 deletion test/absinthe/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ defmodule Absinthe.SchemaTest do
describe "to_sdl/1" do
test "return schema sdl" do
assert Schema.to_sdl(SourceSchema) == """
\"Represents a schema\"\nschema {\n query: RootQueryType\n}\n\ntype Foo {\n name: String\n}\n\n\"can describe query\"\ntype RootQueryType {\n foo: Foo\n}
schema {\n query: RootQueryType\n}\n\ntype Foo {\n name: String\n}\n\n\"can describe query\"\ntype RootQueryType {\n foo: Foo\n}
"""
end
end
Expand Down

0 comments on commit a97a327

Please sign in to comment.