Different subquery input by item ID

Still really new to Apollo and I didn’t see any examples like this in the docs, but forgive me if I missed something.

If I have a schema that can be queried as follows

query ExampleQuery($ids: [ID!]!, $subQueryInput: SubQueryInput!) {
  items(ids: $ids) {
    results {
      item {
         subQuery(input: $subQueryInput) {
           result
         }
      }
    }
  }
}

Is it possible to have a different value for $subQueryInput for each ID in $ids? Or do I need to query each ID separately? Assuming I can’t make changes to the server in the timeframe I’m being held to

Hi @sawmill! The value of the $subQueryInput argument in this example will be the same for each element in the items list. I believe you are correct that, given the stipulated server constraints, a separate query will be needed for each $ids / $subQueryInput combination.

Thanks for confirming my suspicions @JeffAuriemma !

Followup: are there any docs of what’s possible if I could make changes to the server?

@sawmill a caveat: I don’t know how you’re querying for the data and the min/max/median number of items for which you’re querying, which IMO is material to the strategy you’d want to use. But one option is to use a different data structure as a parameter:

type ItemIdWithSubQueryInput {
  id: ID!
  subQueryInput: SubQueryInput!
}

query ExampleQuery($itemIdsWithSubQueryInputs: [ItemIdWithSubQueryInput!]!) {
  items(itemIdsWithSubQueryInputs: $itemIdsWithSubQueryInputs) {
    results {
      item {
         subQuery {
           result
         }
      }
    }
  }
}

In your server code, the items resolver would instantiate an Item resolver for each ItemIdWithSubQueryInput element in the argument list. For each element, the nested Item#subQuery resolver would be passed the SubQueryInput value as an argument.

This is just one potential approach. I don’t particularly like the tight coupling but at the same time, I don’t see a way around it.