AshGraphql: Updating a managed has_many relationship
I'm getting
"In field \"id\": Unknown field."
in the GraphQL response error. It's referring to the id
provided in the has_many related record .
Here are the relevant parts of my WebSite resource:
I tried playing with the options both for change manage_relationship()
and managed_relationship()
. I got different errors. I'm not quite understanding the documentation yet.34 Replies
Have you had a look at the generated types for those
managed_relationship
fields?
like in the graphql playground?The options for
manage_relationship
are discussed here
There are a lot of considerations to be made on that front. What exactly should happen with the values provided in the components
key?
You likely want lookup_with_primary_key?: true
in your managed_relationship to cause it to add the :id
field if you're doing something that should lookup/update existing records. What exactly ought to happen with the input you provide to components? What if something is left out of the list? Should it be removed?The options for manage_relationship are discussed hereYep, I read that. There's a lot of read. I'm having difficulty understanding it.
There are a lot of considerations to be made on that front. What exactly should happen with the values provided in the components key?The list of values in the
components
key are the attributes for each component under a WebSite. These should be used as the attributes to insert or update each component with. The id
field in each item is the component's ID. It should be used to look up the component record in the database for updating. If no ID is given, a new record should be inserted. If the WebSite has a component whose ID is not in the list of component items, that record should be deleted. This is the behavior I'm used to working with when using Ecto's cast_assoc for has_many relationships.
When adding lookup_with_primary_key?: true
to the managed_relationship
call, there's a compiler error: ** (Protocol.UndefinedError) protocol Enumerable not implemented for %Absinthe.Blueprint.Schema.FieldDefinition{name: "id", identifier: :id...
you're having a time of it
Got it, so you want
type: :direct_control
whats the stack trace for that error?I'm using
:direct_control
Yeah, I was meaning you wouldn't need the additional options
Do you mean these options:
yes
Is that stacktrace from the latest ash_graphql release?
No. I'll update it and report back.
awesome, thanks 😄
I just upgraded to ash_graphql 0.23.0, ash 2.6.26, and ash_postgres 1.3.17. So far I'm getting this:
🤔 do you have any resources that have the
AshGraphql.Resource
extension but not a type :type
configuration?Um, I'm not sure. I don't know what a
AshGraphql.Resource
extension is.when you add it to your resource you do
extensions: [AshGraphql.Resource]
in the resource
So we're looking for resources where you've done that but not defined a typeOh wait. I commented out a bunch of code, and that's probably what caused that. One moment...
I'm adding a check for that now
(we should be ignoring resources that don't have a configured type
Okay, I'm seeing the new warnings you were mentioning before. I'll address those first.
You should just be able to add the config, don't need to worry about step 1 🙂
Okay, got through all of that, and no more warnings. The GraphQL response is the same:
This time, I can add the
lookup_with_primary_key?: true
option to managed_relationship()
, and there's no compiler error, but the GraphQL response is the same.
"In field \"id\": Unknown field."
Okay, so we're compiling, its just not including the primary key field which you naturally need for the "or update" component of all of this.
lemme take a look
Thanks!
fun fact, looks like we just aren't adding primary key for updates
lemme confirm/try something out
Oh! Hmm.
oh, nvm, just getting my brain back in this code
okay, do me a favor and try out the main branch?
Yep, one moment...
Sorry, I got lost watching the Erlang Movie.
😆 no worries lol
It works!
🥳
I'm adding an automated test as well for this case in the future
I'll cut a release once the test is in
Sweet! I'll add some more tests on my end to verify all the cases for managing the association.
The three cases are covered in my tests: items can be added, updated, or deleted.
Just wait til you get to nested managed relationships 😆
okay, made some small tweaks, added some tests and released it.
LMK if the update works for you, when you get a chance 🙂
I updated to the new patch version, and my tests are still passing. It's looking good.
Thanks for bearing with me on that 😅
Thanks for being quick to respond!