Skip to content

Commit e5ea822

Browse files
committed
feat: raise at compile time when related resource's domain is missing from schema
1 parent ded9fab commit e5ea822

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

lib/ash_graphql.ex

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ defmodule AshGraphql do
167167

168168
Enum.each(ash_resources, &Code.ensure_compiled!/1)
169169

170+
AshGraphql.validate_domains_for_relationships!(
171+
ash_resources,
172+
Enum.map(domains, &elem(&1, 0))
173+
)
174+
170175
schema = __MODULE__
171176
schema_env = __ENV__
172177

@@ -1259,4 +1264,33 @@ defmodule AshGraphql do
12591264
response_metadata: {:my_extension_key, {MyApp.MetadataHandler, :build, []}}
12601265
"""
12611266
end
1267+
1268+
@doc false
1269+
def validate_domains_for_relationships!(ash_resources, all_domains) do
1270+
for resource <- ash_resources,
1271+
AshGraphql.Resource in Spark.extensions(resource),
1272+
relationship <- Ash.Resource.Info.public_relationships(resource),
1273+
AshGraphql.Resource in Spark.extensions(relationship.destination),
1274+
relationship.destination not in ash_resources do
1275+
destination_domain = Ash.Resource.Info.domain(relationship.destination)
1276+
1277+
domain_hint =
1278+
if destination_domain do
1279+
"Add #{inspect(destination_domain)} to your schema's domain list:\n\n" <>
1280+
" use AshGraphql, domains: #{inspect(all_domains ++ [destination_domain])}"
1281+
else
1282+
"Make sure the domain for #{inspect(relationship.destination)} is included in your schema's domain list."
1283+
end
1284+
1285+
raise """
1286+
#{inspect(resource)} has a relationship `#{relationship.name}` to #{inspect(relationship.destination)}, \
1287+
but #{inspect(relationship.destination)}'s domain is not included in this schema.
1288+
1289+
#{domain_hint}
1290+
"""
1291+
|> IO.inspect()
1292+
end
1293+
1294+
:ok
1295+
end
12621296
end
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# SPDX-FileCopyrightText: 2020 ash_graphql contributors <https://github.com/ash-project/ash_graphql/graphs/contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule AshGraphql.MissingRelatedDomainTest do
6+
use ExUnit.Case, async: false
7+
8+
# Define ResourceB and DomainB as valid, fully-compiled modules outside of the test.
9+
# The test scenario is: both domains are valid, but the schema only registers DomainA.
10+
11+
alias AshGraphql.MissingRelatedDomainTest.{
12+
RelatedDomain,
13+
SourceDomain,
14+
RelatedResource,
15+
SourceResource
16+
}
17+
18+
defmodule RelatedResource do
19+
use Ash.Resource,
20+
domain: RelatedDomain,
21+
extensions: [AshGraphql.Resource]
22+
23+
graphql do
24+
type :missing_test_related_resource
25+
end
26+
27+
actions do
28+
default_accept(:*)
29+
defaults([:read])
30+
end
31+
32+
attributes do
33+
uuid_primary_key(:id)
34+
end
35+
end
36+
37+
defmodule RelatedDomain do
38+
use Ash.Domain, extensions: [AshGraphql.Domain]
39+
40+
resources do
41+
resource(RelatedResource)
42+
end
43+
end
44+
45+
defmodule SourceResource do
46+
use Ash.Resource,
47+
domain: SourceDomain,
48+
extensions: [AshGraphql.Resource]
49+
50+
graphql do
51+
type :missing_test_source_resource
52+
53+
queries do
54+
list :list_source_resources, :read
55+
end
56+
end
57+
58+
actions do
59+
default_accept(:*)
60+
defaults([:read])
61+
end
62+
63+
attributes do
64+
uuid_primary_key(:id)
65+
end
66+
67+
relationships do
68+
belongs_to(:related, RelatedResource, public?: true)
69+
end
70+
end
71+
72+
defmodule SourceDomain do
73+
use Ash.Domain, extensions: [AshGraphql.Domain]
74+
75+
resources do
76+
resource(SourceResource)
77+
end
78+
end
79+
80+
test "raises at compile time when a related resource's domain is not in the schema" do
81+
assert_raise RuntimeError, ~r/RelatedDomain/, fn ->
82+
defmodule TestSchemaMissingDomain do
83+
use Absinthe.Schema
84+
85+
# Only SourceDomain is registered — RelatedDomain is intentionally omitted
86+
@domains [SourceDomain]
87+
use AshGraphql, domains: @domains
88+
end
89+
end
90+
end
91+
end

0 commit comments

Comments
 (0)