Search code examples
graphqlapolloapollostack

Apollo GraphQL: Can One Query Support Lookup Different Lookup Fields?


I've got a working query that looks like this:

const GETONEASSOCIATE_QUERY = gql`
query getOneAssociate($_id: String!) {
  getOneAssociate(_id: $_id) {
    _id
    first_name
    last_name
    city
    state
    userIDinRelatedTable
  }
} `;

Now I'd like to be able to look up an associate by userIDinRelatedTable. Do I have to write a whole new graphQL query, or, is there a way to set up a query so that I can specify what fields to be used for the lookup -- something like:

enter code herequery getOneAssociate($_args: args) {

Thanks in advance to all for any thoughts/advice/info!


Solution

  • I'm guessing in your scheme you have a userIDinRelatedTable field on Associate which resolves to RelatedTable.

    So to get the fields returned in your query you can

    const GETONEASSOCIATE_QUERY = gql`
      query getOneAssociate($_id: String!) {
        getOneAssociate(_id: $_id) {
        _id
        first_name
        last_name
        city
        state
        userIDinRelatedTable {
          id
          field1
          field2
        }
      }
    } `;