absinthe-graphql / absinthe_ecto

DEPRECATED: Use dataloader
MIT License
130 stars 36 forks source link

Add the ability to reference child associations #1

Closed avitex closed 7 years ago

avitex commented 7 years ago

For database structures that aren't so clear cut, or at least what I've currently experienced, the ability to specify a path to the association would be pretty useful.

If the child of the parent the association depends on does not exist or is not transversable, it will return nil.

I did consider adding the ability to load associations along the path that it meets aren't loaded, however, I didn't implement this as I think it would potentially be bad practice, as it treads on absinthe/graphql concerns as the one handling the loading of the tree of associations.

I also considered, rather than providing a path, allowing the user to pass an anonymous function to do the resolving themselves, but they would then need to handle the absence of a child themselves.

If you think different, I'll happily implement it! :)

# Association specified directly on the parent (ie. parent.author)
field :author, :user, resolve: assoc(MyApp.Repo, :author)

# Association specified on a child of the parent (ie. parent.user.author)
field :author, :user, resolve: assoc(MyApp.Repo, [:user, :author])

For an example, consider this type of structure:

Yes, I spelt entities wrong :P

benwilson512 commented 7 years ago

This is an interesting proposal. Do note that with the merge of #5 this can be handled by having an ecto association on your schema that specifies the through assocations themselves.

tlvenn commented 7 years ago

Wouldnt the recursive walk_assoc create some cascading batches ? It seems like it would be better to leverage Ecto.assoc/2 to handle dynamically loading through associations unless behind the scene Ecto will actually generate the same amount of queries.

bruce commented 7 years ago

Closing this as stale (and possibly superseded by #11).