Skip to content

Commit

Permalink
fix: fix primary key delimiter verificatoin step
Browse files Browse the repository at this point in the history
docs: improve docs on unions
  • Loading branch information
zachdaniel committed Sep 28, 2023
1 parent c6db0fc commit ca2e534
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
6 changes: 4 additions & 2 deletions documentation/how_to/use-unions-with-graphql.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -45,4 +47,4 @@ Which, in this case, would yield:

```
type Armor = Plate | ChainMail | {custom: {value: String}}
```
```
4 changes: 2 additions & 2 deletions lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
35 changes: 0 additions & 35 deletions lib/resource/transformers/require_pkey_delimiter.ex

This file was deleted.

37 changes: 37 additions & 0 deletions lib/resource/verifiers/require_pkey_delimiter.ex
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions lib/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit ca2e534

Please sign in to comment.