title: many-to-many relationship in GraphQL
body: <p>Let's say I have two Arrays of Objects:</p>
  
  <pre><code>let movies = [
      { id: '1', title: 'Erin Brockovich'},
      { id: '2', title: 'A Good Year'},
      { id: '3', title: 'A Beautiful Mind'},
      { id: '4', title: 'Gladiator'}
  ];
  
  let actors = [
      { id: 'a', name: 'Julia Roberts'},
      { id: 'b', name: 'Albert Finney'},
      { id: 'c', name: 'Russell Crowe'}
  ];
  </code></pre>
  
  <p>I want to create many-to-many relationship betweet them. For the begining in Vanilla JavaScript , eventually in GraphQL schema.</p>
  
  <p>In JavaScript I did something like this:</p>
  
  <pre><code>let movies = [
      { id: '1', title: 'Erin Brockovich',  actorId: ['a', 'b'] },
      { id: '2', title: 'A Good Year',      actorId: ['b', 'c'] },
      { id: '3', title: 'A Beautiful Mind', actorId: ['c'] },
      { id: '4', title: 'Gladiator',        actorId: ['c'] }
  ];
  let actors = [
      { id: 'a', name: 'Julia Roberts', movieId: ['1'] },
      { id: 'b', name: 'Albert Finney', movieId: ['1', '2'] },
      { id: 'c', name: 'Russell Crowe', movieId: ['2', '3', '4'] }
  ];
  let actorIds = [];
  let movieIds = [];
  for (let m = 0; m &lt; movies.length; m ++) {
      for (let i = 0; i &lt; movies[m].actorId.length; i ++) {
          actorIds.push(movies[m].actorId[i]);
      }
  }
  for (let a = 0; a &lt; actors.length; a ++) {
      for (let i = 0; i &lt; actors[a].movieId.length; i ++) {
          movieIds.push(actors[a].movieId[i]);
      }
  }
  for (let a = 0; a &lt; actors.length; a ++) {
      for (let i = 0; i &lt; actorIds.length; i ++) {
          if ((actors[a].id == 'c') &amp;&amp; (actors[a].id == actorIds[i])) {
              for (let j = 0; j &lt; movies.length; j ++) {
                  if (movies[j].id == movieIds[i]) {
                      console.log(movies[j].title);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>When I run preceding code in Node, Terminal will return</p>
  
  <pre><code>A Good Year
  A Beautiful Mind
  Gladiator
  </code></pre>
  
  <p>and this is exactly what I want.</p>
  
  <p>Unfortunately I've get lost in GraphQL schema. What I have so far—inside of <code>fields</code> function of course—is this:</p>
  
  <pre><code>in_which_movies: {
      type: new GraphQLList(FilmType),
      resolve(parent, args) {
  
          let actorIds = [];
          let movieIds = [];
  
          for (let m = 0; m &lt; movies.length; m ++) {
              for (let i = 0; i &lt; movies[m].actorId.length; i ++) {
                  actorIds.push(movies[m].actorId[i]);
              }
          }
          for (let a = 0; a &lt; actors.length; a ++) {
              for (let i = 0; i &lt; actors[a].movieId.length; i ++) {
                  movieIds.push(actors[a].movieId[i]);
              }
          }
          for (var a = 0; a &lt; actors.length; a ++) {
              for (var i = 0; i &lt; actorIds.length; i ++) {
                  if ((actors[a].id == parent.id) &amp;&amp; (actors[a].id == actorIds[i])) {
                      for (var j = 0; j &lt; movies.length; j ++) {
                          if (movies[j].id == movieIds[i]) {
                              console.log(movies[j].title);
                          }
                      }
                  }
              }
          }
          return movies[j].title;
      }
  }
  </code></pre>
  
  <p>When I run the following query in GraphiQL... </p>
  
  <pre><code>{
      actor(id: "c") {
          name
          in_which_movies {
              title
          }
      }
  }
  </code></pre>
  
  <p>...I have this response:</p>
  
  <pre><code>{
    "errors": [
      {
        "message": "Cannot read property 'title' of undefined",
        "locations": [
          {
            "line": 4,
            "column": 3
          }
        ],
        "path": [
          "actor",
          "in_which_movies"
        ]
      }
    ],
    "data": {
      "actor": {
        "name": "Russell Crowe",
        "in_which_movies": null
      }
    }
  }
  </code></pre>
  
  <p>...which is strange for me, cuz Terminal is responding as I expected</p>
  
  <pre><code>A Good Year
  A Beautiful Mind
  Gladiator
  </code></pre>
  
  <p>I guess all code I've wrote so far is useless and I need some fresh guidelines how to write many-to-many relationship in GraphQL properly.</p>