AE
Ash Elixir•3y ago
moxley

AshGraphql Manage Relationships with multiple actions

Hello, I'm using the AshGraphql managed_relationships DSL to allow a GraphQL mutation to update relationship data on a resource. I works fine with the :create action, but on adding the similar code for the :update action, it gives errors. Here are the relevant parts of the resource module:
defmodule GF.WebSite do
actions do
# Add a set of simple actions. You'll customize these later.
defaults [:read, :destroy]

create :create do
argument :components, {:array, :map}
change manage_relationship(:components, type: :direct_control)
end

create :update do
argument :components, {:array, :map}
change manage_relationship(:components, type: :direct_control)
end
end

graphql do
type :web_site

mutations do
create :create_web_site, :create
update :update_web_site, :update
end

managed_relationships do
managed_relationship :create, :components
managed_relationship :update, :components
end
end
defmodule GF.WebSite do
actions do
# Add a set of simple actions. You'll customize these later.
defaults [:read, :destroy]

create :create do
argument :components, {:array, :map}
change manage_relationship(:components, type: :direct_control)
end

create :update do
argument :components, {:array, :map}
change manage_relationship(:components, type: :direct_control)
end
end

graphql do
type :web_site

mutations do
create :create_web_site, :create
update :update_web_site, :update
end

managed_relationships do
managed_relationship :create, :components
managed_relationship :update, :components
end
end
With the code above, there is a compiler error, Type name "CreateWebSiteComponentsInput" is not unique.. What do I need to change to make this work? I read the documentation about Graphql managed relationships. It's not all clicking yet for me. https://ash-hq.org/docs/dsl/ash-resource#graphql-managed_relationships
Ash HQ
Ash.Resource
View the documentation for Ash.Resource on Ash HQ.
7 Replies
ZachDaniel
ZachDaniel•3y ago
🤔 Something seems strange there I'm still not sure exactly why this would cause your issue, but create :update do is probably not what you want I don't think that will fix it though. Or if it does, then its doing something wrong I think ah, crap
defp default_managed_type_name(resource, action, argument) do
String.to_atom(
to_string(action.type) <>
"_" <>
to_string(AshGraphql.Resource.Info.type(resource)) <>
"_" <> to_string(argument.name) <> "_input"
)
end
defp default_managed_type_name(resource, action, argument) do
String.to_atom(
to_string(action.type) <>
"_" <>
to_string(AshGraphql.Resource.Info.type(resource)) <>
"_" <> to_string(argument.name) <> "_input"
)
end
Thats not how we should be doing that 😢 It should be:
defp default_managed_type_name(resource, action, argument) do
String.to_atom(
to_string(action.name) <>
"_" <> to_string(argument.name) <> "_input"
)
end
defp default_managed_type_name(resource, action, argument) do
String.to_atom(
to_string(action.name) <>
"_" <> to_string(argument.name) <> "_input"
)
end
So fixing your action type will actually fix the issue
moxley
moxleyOP•3y ago
Fixing my action type? What would that look like?
ZachDaniel
ZachDaniel•3y ago
well, you have create :update which I assume you mean to be an update action i.e update :update
moxley
moxleyOP•3y ago
Oh!
ZachDaniel
ZachDaniel•3y ago
So that will "fix" it, but really we ought to make this whole thing better. Probably the only thing I can do is add a new configuration and warn about it at compile time if it isn't set.
moxley
moxleyOP•3y ago
Okay, got it. Yeah, I'm still getting used to setting up actions. Thanks!
ZachDaniel
ZachDaniel•3y ago
I'm going to release a new version of ash_graphql soon that warns about these type names and chooses a better name if you supply the proper config Okay so I've released a new version of ash_graphql 0.23.0 that fixes these generated type names, and shows a warning guiding users in that direction You may want to upgrade before you get too far in

Did you find this page helpful?