From 97ec561464e8b0f3775b6f4fe7b3e81e8b401f82 Mon Sep 17 00:00:00 2001 From: Maarten van Vliet Date: Fri, 26 Aug 2022 19:32:04 +0200 Subject: [PATCH 1/2] Ensure root objects are marked as referenced correctly Followup to https://github.com/absinthe-graphql/absinthe/pull/1176 Since every schema will have a declared or inferred declaration, this can be used to mark root objects as referenced. The MarkReferenced used to mark every :query/:subscription/:mutation object as referenced, even when it was used in the declaration. This fix ensures that root objects that are not used in the declaration are not marked as referenced and thus not rendered in the SDL. --- lib/absinthe/phase/schema/mark_referenced.ex | 13 ++++++++----- test/absinthe/schema_test.exs | 4 ---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/absinthe/phase/schema/mark_referenced.ex b/lib/absinthe/phase/schema/mark_referenced.ex index f009252796..ae1d2bfe65 100644 --- a/lib/absinthe/phase/schema/mark_referenced.ex +++ b/lib/absinthe/phase/schema/mark_referenced.ex @@ -9,13 +9,16 @@ defmodule Absinthe.Phase.Schema.MarkReferenced do %{schema_definitions: [schema]} = blueprint schema = - Map.update!(schema, :type_definitions, &mark_referenced(&1, schema.directive_definitions)) + Map.update!( + schema, + :type_definitions, + &mark_referenced(&1, schema.directive_definitions, schema.schema_declaration) + ) {:ok, %{blueprint | schema_definitions: [schema]}} end - @roots [:query, :mutation, :subscription] - defp mark_referenced(type_defs, directive_defs) do + defp mark_referenced(type_defs, directive_defs, schema_declaration) do types_by_ref = Enum.reduce(type_defs, %{}, fn type_def, acc -> acc @@ -24,8 +27,8 @@ defmodule Absinthe.Phase.Schema.MarkReferenced do end) referenced_type_ids = - @roots - |> Enum.map(&Map.get(types_by_ref, &1)) + schema_declaration.field_definitions + |> Enum.map(&Map.get(types_by_ref, &1.identifier)) |> Enum.reject(&is_nil/1) |> Enum.concat(directive_defs) |> Enum.reduce(MapSet.new(), &referenced_types(&1, types_by_ref, &2)) diff --git a/test/absinthe/schema_test.exs b/test/absinthe/schema_test.exs index ac55d072e8..542178fa13 100644 --- a/test/absinthe/schema_test.exs +++ b/test/absinthe/schema_test.exs @@ -328,10 +328,6 @@ defmodule Absinthe.SchemaTest do name: String } - type MyRootMutation { - name: String - } - type RootQueryType { name(familyName: Boolean): String } From 8dc22a5145eac2931fb0c95b0e4287268615a024 Mon Sep 17 00:00:00 2001 From: Maarten van Vliet Date: Fri, 26 Aug 2022 19:44:39 +0200 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dbb24e3cd..ea39daa1a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Bug Fix: [Fix bug in Schema.**absinthe_types**(:all) for Persistent Term](https://github.com/absinthe-graphql/absinthe/pull/1161) - Feature: [Add `import_directives` macro](https://github.com/absinthe-graphql/absinthe/pull/1158) - Feature: [Support type extensions on schema declarations](https://github.com/absinthe-graphql/absinthe/pull/1176) +- Bug Fix: [Root objects are marked as referenced correctly](https://github.com/absinthe-graphql/absinthe/pull/1186) ## 1.7.0