title: DataLoader fetching documents before knowing their keys
body: <p>I have around 3350 documents saved in MongoDB with the following schema:</p>
  
  <pre><code>const item = new Schema({
      id: {
          type: Number,
          required: true,
          index: true
      },
      name: {
          type: String,
          required: true,
          index: true
      },
      store: {
          type: Number,
          required: true
      },
      rsbuddy: {
          type: [rsbuddy],
          default: []
      }
  })
  </code></pre>
  
  <p>The <code>rsbuddy</code> field inside this <code>item</code> usually has around 30,000 to 40,000 entries stored, so I only want to fetch these documents when they are requested. Furthermore, my root GraphQL query looks as follows (it's built with Apollo):</p>
  
  <pre><code>const RuneScapeQuery = `
      type RuneScapeQuery {
          items: [Item]
          item(id: Int): Item
          updates: [Update]
      }
  `
  </code></pre>
  
  <p>Basically, what I want to happen is that when I am batching a request for the <code>items</code> query, I want it to query all documents by default. I currently have the following code to do that:</p>
  
  <pre><code>resolvers: {
    RuneScapeQuery: {
      items: async (root, args, { loaders }) =&gt;
        fetchItems()
          .then(items =&gt; items.map(item =&gt; item.id))
          .then(ids =&gt; loaders.itemLoader.loadMany(ids)),
      item: (root, { id }, { loaders }) =&gt; loaders.itemLoader.load(id)
    }
  }
  </code></pre>
  
  <p>However, the problem with this approach is that the first round-trip to MongoDB is spent fetching the id's of all items. Secondly, the dataloader will request all data for each <em>individual</em> item instead of using the <code>find</code> function to retrieve many documents.</p>
  
  <pre><code>const fetchItem = id =&gt;
      Item.findOne({ id }, { rsbuddy: false })
  
  const itemLoader = new DataLoader(keys =&gt; {
      return Promise.all(keys.map(fetchItem))
  })
  </code></pre>
  
  <p>What is a good way to improve this structure and simplify it?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>To clarify my question, I will try to explain the question with the main problem and that is time. If you add <code>DataLoader</code>s to the equation, the items will be served from cache. However, due to the nature of always needing to fetch the live keys first, the query will remain to take 11 seconds. The first time, it will take around 30 seconds, because it needs to fetch the keys and then fetch the items one by one. However, after that, it will take 11 seconds again, because the keys need to be fetched first.</p>