Complex relationship

Actually I’m making an custom api to practice Phoenix, so actually I’m having a problem with a complex relationship with Ecto, if someone can help me to solve it please n.n

    "producers": ["23\/Bandai_Visual", "Bandai Visual"],
    "licensors": [["102\/FUNimation_Entertainment", "FUNimation Entertainment"], ["233\/Bandai_Entertainment", "Bandai Entertainment"]],
    "studios": ["14\/Sunrise", "Sunrise"],
	"related": {
		"Adaptation": [["Cowboy Bebop", "\/manga\/173\/Cowboy_Bebop"], ["Shooting Star Bebop: Cowboy Bebop", "\/manga\/174\/Shooting_Star_Bebop__Cowboy_Bebop"]],
		"Side story": [["Cowboy Bebop: Tengoku no Tobira", "\/anime\/5\/Cowboy_Bebop__Tengoku_no_Tobira"], ["Cowboy Bebop: Ein no Natsuyasumi", "\/anime\/17205\/Cowboy_Bebop__Ein_no_Natsuyasumi"]],
		"Summary": ["Cowboy Bebop: Yose Atsume Blues", "\/anime\/4037\/Cowboy_Bebop__Yose_Atsume_Blues"]

I wanted to return this json on a REST/GraphQl request, but my problem its on “related”, “producer”, “licensors” and “studios” field.

for “producers”, “licensors” and “studios” every return value its related to the same table “anime_producers” (the id 23, 102, 233 etc… are this table)

for “related” its the most complext of all, it have 12 possible categories, and every category can have a manga or anime or both

I can figure out the “producers”, “licensors” and “studios” with a table for many_to_many (correct me if I’m wrong), but related I can’t figure out what is the solution for this one


Can you elaborate on what you mean by this? This will be handled very differently depending on whether you’re doing GraphQL or REST.

Sure, I have 2 endpoints in this api one for REST requests like (Rest request, this return all the file)

And one for Graphql (Graphql request, here can select what you need)

Ok, well what do you have so far for loading this data via GraphQL?

Yes @benwilson512 , the graphql and rest

Sorry, I was vague. I’m trying to get information about what your existing schema looks like, along with the relevant database models.

Your question is a bit vague, I can’t tell if you’re asking an Ecto question or a GraphQL / Absinthe question. We don’t have any information on what your "producers", "licensors" and "studios" tables look like, so I’m not sure how we’re gonna help you with the many to many.

Oh I’m so sorry @benwilson512 , I can explain with more details with this I think (I solve the “producers”, “licensors” and “studios” problem in this way):

The table “meta_producer” just contain the name of the producer (also the url, I forgot to add in the schema, same with genre table), the relation code in “anime” schema is this:

many_to_many :genres, Genre, join_through: "anime_genres", join_keys: [anime_id: :mal_id, genre_id: :mal_id]
many_to_many :licensors, Producer, join_through: "anime_licensors", join_keys: [anime_id: :mal_id, producer_id: :mal_id]
many_to_many :producers, Producer, join_through: "anime_producers", join_keys: [anime_id: :mal_id, producer_id: :mal_id]
many_to_many :studios, Producer, join_through: "anime_studios", join_keys: [anime_id: :mal_id, producer_id: :mal_id]


  1. All tables have a id called “mal_id” that works as an normal “id” but without autoincrement, and as a reference for relationships
  2. I just want to show the data with the api, I don’t need mutations, delete etc…

So my question on the GraphQl side its:

  1. How to show/configure a many to many relation in absinthe?

My Ecto question is:

  1. How to achieve the relation to build the “related” field (see the json in the first post) in Ecto? The problems here are:
  • Exist 12 types of categories (example: “Adaptation”, “Side story” etc…)
  • Every category can have “animes_anime” (see the image to guide) table reference, or a “manga_mangas” table reference (This is not seen in the image, but is simmilar to “anime_anime”), this two have the primary key named “mal_id” (in resume can reference 2 diferent tables, and get just the name and url from this table)

I hope its explained correctly, tell me if have any question ^^’

The Graphql question its solved ^^

1 Like

As far as doing this from REST is concerned then, you can just run a GraphQL query in your controller action:

def show(conn, params) do
  doc = """
  graphql query here
  case, MyApp.Web.Schema, variables: params) do
    # handle errors or ok result here
1 Like

Oh this is much better!!! so many thanks!