title: How do you handle lists that require joined data from multiple data sources in AppSync/GraphQL?
body: <pre><code>type Employee {
      id: String!
      name: String
      lastObservedStatus: String
  }
  
  type Query {
      employees: [Employee]
  }
  </code></pre>
  <p>This is a fictional schema to illustrate my question. I have two separate data sources that return lists that need to be joined in order to populate the response. The first data source 'employee list api' is an http API I can query to get an authoritative list of employees that I can use to populate the <code>id</code> and <code>name</code> columns. For example, I get a response like this:</p>
  <pre><code>[
      {&quot;id&quot;: &quot;001&quot;, &quot;name&quot;: &quot;Harry&quot;},
      {&quot;id&quot;: &quot;002&quot;, &quot;name&quot;: &quot;Jerry&quot;},
      {&quot;id&quot;: &quot;003&quot;, &quot;name&quot;: &quot;Larry&quot;}
  ]
  </code></pre>
  <p>I have a second http API 'employee observation log' I can query to get a list of statuses together with the associated ids. The id allows me to associate the number to an entry in the employee record, and I have a record date. There may be more than one status record, but in GraphQL I want to pick only the most recent one. Example response:</p>
  <pre><code>[
      {&quot;id&quot;:&quot;002&quot;, &quot;TimeStamp&quot;:&quot;2021-07-01T12:30:00Z&quot;, &quot;status&quot;: &quot;eating&quot;},
      {&quot;id&quot;:&quot;002&quot;, &quot;TimeStamp&quot;:&quot;2021-07-01T13:10:00Z&quot;, &quot;status&quot;: &quot;staring out the window&quot;},
      {&quot;id&quot;:&quot;001&quot;, &quot;TimeStamp&quot;:&quot;2021-07-01T16:00:00Z&quot;, &quot;status&quot;: &quot;sleeping in lobby&quot;}
  ]
  </code></pre>
  <p>Now, I want the graphQL response to return something like this:</p>
  <pre><code>{
    &quot;data&quot;: {
      &quot;employees&quot;: [
        {
          &quot;id&quot;: &quot;001&quot;,
          &quot;name&quot;: &quot;Harry&quot;,
          &quot;lastObservedStatus&quot;: &quot;sleeping in lobby&quot;
        },
        {
          &quot;id&quot;: &quot;002&quot;,
          &quot;name&quot;: &quot;Jerry&quot;,
          &quot;lastObservedStatus&quot;: &quot;staring out the window&quot;
        },
        {
          &quot;id&quot;: &quot;003&quot;,
          &quot;name&quot;: &quot;Larry&quot;,
          &quot;lastObservedStatus&quot;: null
        }
      ]
    }
  }
  </code></pre>
  <p>Since 'employee list api' is the authoritative source about which employees exist, all queries to the 'employee' field should always trigger a query to that api, but the 'employee observation log' api should only be triggered if the 'lastObservedStatus' field is selected in the query.</p>
  <p>For a schema like this, where should the resolvers be registered? I've read that the best practice is to always attach resolvers at the leaf nodes, but I'm not sure how that can be done in this situation. I'm not even sure what happens if you attach a resolver on subfields of a list.</p>
  <p>I feel like the correct way to handle this is to attach a lambda resolver to the <code>employees</code> field, and in the lambda resolver check the query's selectionSetList to check whether or not the 'lastObservedStatus' field has been selected. If not, then the lambda only queries 'employee list api', but otherwise the lambda also queries 'employee observation log' and does something similar to a SQL join before returning the result. But is that the correct way to handle this?</p>