From ca2e5342acd808935c87fe15831995f4806544ad Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Thu, 28 Sep 2023 17:31:27 -0400 Subject: [PATCH] fix: fix primary key delimiter verificatoin step docs: improve docs on unions --- .../how_to/use-unions-with-graphql.md | 6 ++- lib/resource/resource.ex | 4 +- .../transformers/require_pkey_delimiter.ex | 35 ------------------ .../verifiers/require_pkey_delimiter.ex | 37 +++++++++++++++++++ lib/type.ex | 6 +++ 5 files changed, 49 insertions(+), 39 deletions(-) delete mode 100644 lib/resource/transformers/require_pkey_delimiter.ex create mode 100644 lib/resource/verifiers/require_pkey_delimiter.ex diff --git a/documentation/how_to/use-unions-with-graphql.md b/documentation/how_to/use-unions-with-graphql.md index 9b5c933c..69ec1696 100644 --- a/documentation/how_to/use-unions-with-graphql.md +++ b/documentation/how_to/use-unions-with-graphql.md @@ -1,6 +1,6 @@ # Use Unions with GraphQL -By default, if a union is used in your resource in line, it will get a nice type generated for it based on the +By default, if a union is used in your resource in line, it will get a nice type generated for it based on the resource/key name. Often, you'll want to define a union using `Ash.Type.NewType`. For example: ```elixir @@ -21,6 +21,8 @@ defmodule MyApp.Armor do ] ] + use AshGraphql.Type + # Add this to define the union in ash_graphql def graphql_type(_), do: :armor end @@ -45,4 +47,4 @@ Which, in this case, would yield: ``` type Armor = Plate | ChainMail | {custom: {value: String}} -``` \ No newline at end of file +``` diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index e75e20fd..b2fd34c0 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -360,14 +360,14 @@ defmodule AshGraphql.Resource do } @transformers [ - AshGraphql.Resource.Transformers.RequirePkeyDelimiter, AshGraphql.Resource.Transformers.RequireKeysetForRelayQueries, AshGraphql.Resource.Transformers.ValidateActions, AshGraphql.Resource.Transformers.ValidateCompatibleNames ] @verifiers [ - AshGraphql.Resource.Verifiers.VerifyQueryMetadata + AshGraphql.Resource.Verifiers.VerifyQueryMetadata, + AshGraphql.Resource.Verifiers.RequirePkeyDelimiter ] @sections [@graphql] diff --git a/lib/resource/transformers/require_pkey_delimiter.ex b/lib/resource/transformers/require_pkey_delimiter.ex deleted file mode 100644 index 5531b348..00000000 --- a/lib/resource/transformers/require_pkey_delimiter.ex +++ /dev/null @@ -1,35 +0,0 @@ -defmodule AshGraphql.Resource.Transformers.RequirePkeyDelimiter do - # Ensures that the resource has a primary key called `id` - @moduledoc false - - use Spark.Dsl.Transformer - - alias Spark.Dsl.Transformer - - def transform(dsl) do - if Transformer.get_persisted(dsl, :embedded?) do - {:ok, dsl} - else - primary_key = - dsl - |> Transformer.get_entities([:attributes]) - |> Enum.filter(& &1.primary_key?) - - case primary_key do - [_single] -> - {:ok, dsl} - - [_ | _] -> - if Transformer.get_persisted(dsl, :primary_key) do - {:ok, dsl} - else - {:error, - "AshGraphql requires a `primary_key_delimiter` to be set for composite primary keys."} - end - end - end - end - - def after?(Ash.Resource.Transformers.BelongsToAttribute), do: true - def after?(_), do: false -end diff --git a/lib/resource/verifiers/require_pkey_delimiter.ex b/lib/resource/verifiers/require_pkey_delimiter.ex new file mode 100644 index 00000000..94685e52 --- /dev/null +++ b/lib/resource/verifiers/require_pkey_delimiter.ex @@ -0,0 +1,37 @@ +defmodule AshGraphql.Resource.Verifiers.RequirePkeyDelimiter do + # Ensures that the resource has a primary key called `id` + @moduledoc false + + use Spark.Dsl.Verifier + + alias Spark.Dsl.Verifier + + def verify(dsl) do + if Verifier.get_persisted(dsl, :embedded?) do + :ok + else + primary_key = + dsl + |> Verifier.get_entities([:attributes]) + |> Enum.filter(& &1.primary_key?) + + case primary_key do + [_single] -> + :ok + + [_ | _] -> + if Verifier.get_persisted(dsl, :primary_key) do + :ok + else + module = Verifier.get_persisted(dsl, :module) + + raise Spark.Error.DslError, + module: module, + path: [:graphql, :primary_key_delimiter], + message: + "AshGraphql requires a `primary_key_delimiter` to be set for composite primary keys." + end + end + end + end +end diff --git a/lib/type.ex b/lib/type.ex index 42a3452e..4d1db595 100644 --- a/lib/type.ex +++ b/lib/type.ex @@ -3,6 +3,12 @@ defmodule AshGraphql.Type do Callbacks used to enrich types with GraphQL-specific metadata. """ + defmacro __using__(_) do + quote do + @behaviour AshGraphql.Type + end + end + @doc """ Sets the name of the graphql type for a given Ash type. For example: `:weekday`.