Created
November 5, 2016 11:34
-
-
Save MartinElvar/fc49103230479cd22eaa1b7b75cd3099 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# User has many storages. | |
# | |
# Give me a user, along with the storages it owns. | |
# q={user(id: 131) {name, storages{title} }} | |
defmodule Users.Types do | |
use Absinthe.Schema.Notation | |
object :user do | |
field :id, :id | |
field :first_name, :string | |
field :last_name, :string | |
field :email, :string | |
field :phone, :string | |
field :storages, :storage do | |
resolve fn user, _, _ -> | |
batch({ApiEndpoint.Schema.Helpers, :by_id, Storages.Storage}, {:user_id, user.id}, fn batch_result -> | |
# Find the storages for the user, in the batch. | |
storages = Enum.filter(batch_result, fn storage -> storage.user_id == user.id end) | |
# It seems i may not return a list of storages here? | |
{:ok, batch_result} | |
end) | |
end | |
end | |
end | |
end | |
defmodule ApiEndpoint.Schema.Helpers do | |
def by_id(model, acc) do | |
Enum.reduce(acc, model, fn {foreign_key, id}, query -> | |
query | |
|> where([m], field(m, ^foreign_key) == ^id) | |
end) | |
|> Storages.Repo.all # But there are no database in the Endpoint! Solve plox? | |
end | |
end | |
# > expected a map, got: [%Storages.Storage{price_annually.......... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment