High complexity when getting linked items of BoardRelation Value

Hi all,

I was wondering if there is any plan to increase or even eliminate the complexity check.

I need to get the subitems of a connected board but apparently the query is too complex. Right now I’m doing it with two calls, but ideally I would want to make just one, here it is FYR:

query {
      items (ids: [4520136270]) {
        id
        column_values (ids: ["conectar_tableros4"]) {
          value
          id
          ...on BoardRelationValue {
            display_value
            linked_items {
              id
            column_values (ids: ["estado","correo_electr_nico","date"]) {
              id
              value
              text
            }
            subitems {
              name
              column_values(ids: ["numbers","n_meros","estado1","estado","n_meros8"]){
                id
                text
                value
              }
            }
          }
        }
      }
    }
  }

And this is the answer I’m getting:

{
  "errors": [
    {
      "message": "Query has complexity of 14024043, which exceeds max complexity of 5000000",
      "extensions": {
        "code": "maxComplexityExceeded",
        "complexity": 14024043,
        "maxComplexity": 5000000
      }
    }
  ],
  "account_id": XXXXXXXX
}

Any help would be really appreciated.

Thanks,
Carlos

Hey @carlosdelgado – moved your question into a new thread because I think it’s rich enough to be a standalone topic.

Removing complexity

I’m afraid we don’t have any plans to remove the complexity cost.

We need to rate limit our API to ensure our platform is stable and performs well for all our customers.

We have plans to make the calculation more accurate, but nothing specific to share at the moment.

Optimizing the query

I would try to optimize the query a bit. Complexity is hard to calculate on its own, so I usually just do trial and error with a few different strategies:

  • Adding the complexity field to the top of the query, so I can check the cost of the current query
  • Check which fields are the highest cost by removing them and checking the complexity
  • Move deeply nested data into a more shallower part of the query

Let’s go through them one by one:

Adding the complexity field:

This is simple, just add complexity { query } to the top:

query {
  complexity {
    query 
  }
  items {...}
}

Check which is the highest cost field:

I removed some fields and found that the nested subitems call is what causes your query to be so heavy.

The query without the subitems field costs only 14043 points:

query noSubitems {
  complexity {
    query
  }
  items(ids: [4520136270]) {
    id
    column_values(ids: ["conectar_tableros4"]) {
      value
      id
      ... on BoardRelationValue {
        display_value
        linked_items {
          id
          column_values(ids: ["estado", "correo_electr_nico", "date"]) {
            id
            value
            text
          }
        }
      }
    }
  }
}

This cost is so high because subitems are two extra round trips on the server (one to get the linked board, and another to get the subitem board). Plus, there could be 1 subitem or 10k of them. The complexity takes all these costs in mind.

Reducing nesting using linked_items on item level

You can also reduce nesting in the query here, because each item has a linked_items field. It requires you to supply the connected board ID and connected column.

This query only has complexity of 14045:

query newQuery {
  complexity {
    query
  }
  items(ids: [4520136270]) {
    id
    linked_items(link_to_item_column_id: "conectar_tableros4", linked_board_id:12345) {
      id
      column_values(ids: ["estado", "correo_electr_nico", "date"]) {
        id
        value
        text
      }
      subitems {
        name
        column_values(ids: ["numbers", "n_meros", "estado1", "estado", "n_meros8"]) {
          id
          text
          value
        }
      }
    }
  }
}

Final thoughts

I know you want to make only 1 API call, but this is too much data to get in one call. You can do two things:

  • Get the connected board ID and then get the data through the linked_items object on items, not on the BoardRelationValue
  • Get subitems in another API call

Let me know what you think :crystal_ball:

1 Like