title: In Apollo GraphQL, how do I setup a resolver to not loop values?
body: <p>I'm using Sequelize in Node.js with Apollo-Server and Express.js.</p>
  
  <p>When making queries that go deeper and deeper, GraphQL is looping my models and doing a separate query by ID on each of those.</p>
  
  <p>For example, if I get <code>user(userId) &gt; playthroughs &gt; scores</code>, this will do a lookup for that user (no problem), then a lookup for all the playthroughs with that <code>userId</code> (still no a big deal), but then to get the scores, it loops each <code>playthroughId</code> and does a completely separate query on each. This is ridiculously inefficient and causes my queries to take way longer than they should.</p>
  
  <p>Instead of looping:</p>
  
  <pre><code>SELECT scoreValue
  FROM scores
  WHERE playthroughId = id
  </code></pre>
  
  <p>I'd really like to grab the array myself and do that loop like this:</p>
  
  <pre><code>SELECT scoreValue
  FROM scores
  WHERE playthroughId IN (...ids)
  </code></pre>
  
  <p>This also happened when I used the reference GraphQL from Facebook last year, so I don't think it's specific to Apollo's implementation.</p>
  
  <p>I'd like to know how I can tweak these queries so they're not taking such a performance hit.</p>
  
  <p>Example resolvers:</p>
  
  <pre><code>const resolvers = {
      Query: {
          user: (_, values) =&gt; User.findOne(formatQuery(values))
              .then(getDataValues),
      },
  
      Playthrough: {
          score: ({ playthroughId }) =&gt; Score.findOne(formatQuery({ playthroughId }))
              .then(getDataValues),
      },
  
      User: {
          playthroughs: ({ userId }, { take }) =&gt; Playthrough.findAll(formatQuery({ userId, take, order: 'playthroughId DESC' }))
              .then(getAllDataValues),
      },
  }
  </code></pre>