Skip to content

Instantly share code, notes, and snippets.

@romseguy
Last active July 12, 2017 22:34
Show Gist options
  • Save romseguy/bac7a3d7b159b13d6a0a2210127b226b to your computer and use it in GitHub Desktop.
Save romseguy/bac7a3d7b159b13d6a0a2210127b226b to your computer and use it in GitHub Desktop.
ecto question
# User can have many Place
# Place can have many User
# User has a role for a Place he belongs to
# 1) ecto schemas
schema "accounts_users" do
# fields
many_to_many :map_places, Place, join_through: UserPlace
end
schema "map_places" do
# fields
many_to_many :accounts_users, User, join_through: UserPlace
end
schema "users_places" do
field :user_id, :id
field :place_id, :id
field :role_id, :id
end
# 2) graphql
# 2.1) query I'm running
query {
places {
id
users {
userId
roleId
}
}
}
# 2.2) absinthe types
object :map_place do
field :id, :id
field :users, list_of(:user_place), resolve: assoc(:users_places)
end
object :user_place do
field :user_id, :id
field :place_id, :id
field :role_id, :id
end
# 2.3) absinthe query
query do
field :places, type: list_of(:map_place) do
resolve &Resolvers.all/2
end
end
# resolver
def all(_args, _info) do
{:ok, Map.list_places()}
end
# phoenix Map context
def list_places do
Repo.all(Place)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment