improvement: only define managed_relationship mutations when necessary

This commit is contained in:
Zach Daniel 2024-06-18 08:58:15 -04:00
parent 5892cf5c1d
commit 0292ef1db8
2 changed files with 52 additions and 37 deletions

View file

@ -154,7 +154,7 @@ defmodule AshGraphql do
all_domains,
unquote(resources),
action_middleware,
__MODULE__,
unquote(schema),
unquote(relay_ids?)
)
@ -184,13 +184,33 @@ defmodule AshGraphql do
all_domains,
unquote(resources),
action_middleware,
__MODULE__,
unquote(schema),
unquote(relay_ids?)
)
|> Enum.reduce(blueprint_with_queries, fn mutation, blueprint ->
Absinthe.Blueprint.add_field(blueprint, "RootMutationType", mutation)
end)
managed_relationship_types =
AshGraphql.Resource.managed_relationship_definitions(
Process.get(:managed_relationship_requirements, []),
unquote(schema)
)
|> Enum.reject(fn type ->
existing_types =
case blueprint_with_mutations do
%{schema_definitions: [%{type_definitions: type_definitions}]} ->
type_definitions
_ ->
[]
end
Enum.any?(existing_types, fn existing_type ->
existing_type.name == type.name
end)
end)
domains = unquote(Enum.map(domains, &elem(&1, 0)))
type_definitions =
@ -262,7 +282,9 @@ defmodule AshGraphql do
List.update_at(blueprint_with_mutations.schema_definitions, 0, fn schema_def ->
%{
schema_def
| type_definitions: schema_def.type_definitions ++ type_definitions
| type_definitions:
schema_def.type_definitions ++
type_definitions ++ managed_relationship_types
}
end)

View file

@ -1221,6 +1221,15 @@ defmodule AshGraphql.Resource do
managed = AshGraphql.Resource.Info.managed_relationship(resource, action, argument)
if managed do
current = Process.get(:managed_relationship_requirements, [])
Process.put(
:managed_relationship_requirements,
[
{resource, managed, action, argument} | current
]
)
type =
if managed.type_name do
managed.type_name
@ -1779,46 +1788,30 @@ defmodule AshGraphql.Resource do
List.wrap(filter_input(resource, schema)) ++
filter_field_types(resource, schema) ++
List.wrap(page_type_definitions(resource, schema)) ++
enum_definitions(resource, schema, __ENV__) ++
managed_relationship_definitions(resource, schema)
enum_definitions(resource, schema, __ENV__)
end
def no_graphql_types(resource, schema) do
enum_definitions(resource, schema, __ENV__) ++
managed_relationship_definitions(resource, schema)
enum_definitions(resource, schema, __ENV__)
end
defp managed_relationship_definitions(resource, schema) do
resource
|> Ash.Resource.Info.actions()
|> Enum.flat_map(fn action ->
action.arguments
|> Enum.flat_map(fn argument ->
case AshGraphql.Resource.Info.managed_relationship(resource, action, argument) do
nil ->
[]
def managed_relationship_definitions(used, schema) do
Enum.map(used, fn {resource, managed_relationship, action, argument} ->
opts =
find_manage_change(argument, action, resource) ||
raise """
There is no corresponding `change manage_change(...)` for the given argument and action
combination.
"""
managed_relationship ->
[{managed_relationship, argument, action}]
end
end)
|> Enum.map(fn {managed_relationship, argument, action} ->
opts =
find_manage_change(argument, action, resource) ||
raise """
There is no corresponding `change manage_change(...)` for the given argument and action
combination.
"""
managed_relationship_input(
resource,
action,
opts,
argument,
managed_relationship,
schema
)
end)
managed_relationship_input(
resource,
action,
opts,
argument,
managed_relationship,
schema
)
end)
end