github: Add rate limit cost estimation for GraphQL
Created by: ryanslade
GraphQL queries are rate limited based on "cost", not number of requests. The details of how this works are here: https://developer.github.com/v4/guides/resource-limitations/#rate-limit
It is possible to estimate this cost before making a query, as explained here: https://developer.github.com/v4/guides/resource-limitations/#calculating-a-rate-limit-score-before-running-the-call
This change implements this estimation by walking the query tree looking for instances of "first" and "last" arguments. When found, the cost for that node is the cost of all it's parent nodes multiplied together.
The algorithm was tested by manually requesting the cost of various queries using the GitHub GraphQL explorer and comparing the output to what our estimator produces.
They were all correct apart from the "complex query" test which was off by 1, 20 vs 21. However, their docs mention that the estimate is "roughly" equivalent so I think the error is acceptable.
This change is not used anywhere yet but it will allow us to greatly simplify our internal rate limit code as it allows us to move the rate limiting closer to the point where we make queries.