From e433b57eda1e45e959f7a166d727493e8a80b14f Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Wed, 2 Dec 2020 00:55:25 -0500 Subject: [PATCH] fix: resolve testing compilation errors --- lib/ash_graphql.ex | 68 +++++++++++++++++----------------- test/create_test.exs | 9 ++++- test/support/api.ex | 2 + test/support/resources/post.ex | 2 + test/support/schema.ex | 4 +- test/test_helper.exs | 2 + 6 files changed, 51 insertions(+), 36 deletions(-) diff --git a/lib/ash_graphql.ex b/lib/ash_graphql.ex index 8024164..1481e3e 100644 --- a/lib/ash_graphql.ex +++ b/lib/ash_graphql.ex @@ -12,6 +12,9 @@ defmodule AshGraphql do api |> List.wrap() |> Kernel.++(List.wrap(apis)) + + apis = + apis |> Enum.map(&{&1, false}) |> List.update_at(0, fn {api, _} -> {api, true} end) @@ -20,6 +23,8 @@ defmodule AshGraphql do @moduledoc false alias Absinthe.{Blueprint, Phase, Pipeline} + Code.ensure_compiled(api) + def pipeline(pipeline) do Pipeline.insert_before( pipeline, @@ -31,45 +36,40 @@ defmodule AshGraphql do def run(blueprint, _opts) do api = unquote(api) - case Code.ensure_compiled(api) do - {:module, _} -> - blueprint_with_queries = - api - |> AshGraphql.Api.queries(__MODULE__) - |> Enum.reduce(blueprint, fn query, blueprint -> - Absinthe.Blueprint.add_field(blueprint, "RootQueryType", query) - end) + Code.ensure_compiled(api) - blueprint_with_mutations = - api - |> AshGraphql.Api.mutations(__MODULE__) - |> Enum.reduce(blueprint_with_queries, fn mutation, blueprint -> - Absinthe.Blueprint.add_field(blueprint, "RootMutationType", mutation) - end) + blueprint_with_queries = + api + |> AshGraphql.Api.queries(__MODULE__) + |> Enum.reduce(blueprint, fn query, blueprint -> + Absinthe.Blueprint.add_field(blueprint, "RootQueryType", query) + end) - type_definitions = - if unquote(first?) do - AshGraphql.Api.global_type_definitions(__MODULE__) ++ - AshGraphql.Api.type_definitions(api, __MODULE__) - else - AshGraphql.Api.type_definitions(api, __MODULE__) - end + blueprint_with_mutations = + api + |> AshGraphql.Api.mutations(__MODULE__) + |> Enum.reduce(blueprint_with_queries, fn mutation, blueprint -> + Absinthe.Blueprint.add_field(blueprint, "RootMutationType", mutation) + end) - new_defs = - List.update_at(blueprint_with_mutations.schema_definitions, 0, fn schema_def -> - %{ - schema_def - | imports: [{Absinthe.Type.Custom, []} | List.wrap(schema_def.imports)], - type_definitions: schema_def.type_definitions ++ type_definitions - } - end) + type_definitions = + if unquote(first?) do + AshGraphql.Api.global_type_definitions(__MODULE__) ++ + AshGraphql.Api.type_definitions(api, __MODULE__) + else + AshGraphql.Api.type_definitions(api, __MODULE__) + end - {:ok, %{blueprint_with_mutations | schema_definitions: new_defs}} + new_defs = + List.update_at(blueprint_with_mutations.schema_definitions, 0, fn schema_def -> + %{ + schema_def + | imports: [{Absinthe.Type.Custom, []} | List.wrap(schema_def.imports)], + type_definitions: schema_def.type_definitions ++ type_definitions + } + end) - {:error, _} -> - # Something else will fail here, so we don't need to - {:ok, blueprint} - end + {:ok, %{blueprint_with_mutations | schema_definitions: new_defs}} end end diff --git a/test/create_test.exs b/test/create_test.exs index 56afb45..f4fc5ef 100644 --- a/test/create_test.exs +++ b/test/create_test.exs @@ -1,5 +1,12 @@ defmodule AshGraphql.CreateTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false + + setup do + on_exit(fn -> + nil + # ETS.Set.delete(ETS.Set.wrap_existing!(AshGraphql.Test.Post)) + end) + end test "a create with arguments works" do resp = diff --git a/test/support/api.ex b/test/support/api.ex index e6b6ba3..63f85c0 100644 --- a/test/support/api.ex +++ b/test/support/api.ex @@ -1,4 +1,6 @@ defmodule AshGraphql.Test.Api do + @moduledoc false + use Ash.Api resources do diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index ed38820..313ab20 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -1,4 +1,6 @@ defmodule AshGraphql.Test.Post do + @moduledoc false + use Ash.Resource, data_layer: Ash.DataLayer.Ets, extensions: [AshGraphql.Resource] diff --git a/test/support/schema.ex b/test/support/schema.ex index 3d7126c..c9c50e0 100644 --- a/test/support/schema.ex +++ b/test/support/schema.ex @@ -1,4 +1,6 @@ defmodule AshGraphql.Test.Schema do + @moduledoc false + use Absinthe.Schema @apis [AshGraphql.Test.Api] @@ -15,7 +17,7 @@ defmodule AshGraphql.Test.Schema do AshGraphql.add_context(ctx, @apis) end - def plugins() do + def plugins do [Absinthe.Middleware.Dataloader | Absinthe.Plugin.defaults()] end end diff --git a/test/test_helper.exs b/test/test_helper.exs index 869559e..054dbef 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1 +1,3 @@ ExUnit.start() + +Code.ensure_compiled(AshGraphql.Test.Api)