Feature request: adding path to estimators args(for calculating actual query complexity)
eduhenke opened this issue · 0 comments
eduhenke commented
I want to have both the expected and actual query complexity/cost, like in the shopify api: https://shopify.dev/api/usage/rate-limits#requested-and-actual-cost.
The expected query cost is pretty easy with this library, an example using an ApolloServer plugin:
{
requestDidStart: async () => ({
async didResolveOperation({ request, document }) {
const complexity = getComplexity({
schema,
operationName: request.operationName,
query: document,
variables: request.variables,
estimators: [
directiveEstimator({ name: 'complexity' }),
simpleEstimator({ defaultComplexity: 1 }),
],
});
logger.info('Expected query complexity points:', { complexity });
}
})
}
However, I'm having trouble calculating the actual query cost, here is my attempt so far:
{
requestDidStart: async () => ({
async willSendResponse({ request, document, response }) {
// uses response objects to determine the actual operation complexity
const responseEstimator: ComplexityEstimator = (options) => {
// there is no "options.path"
// const object = _.get(response, options.path);
// i can then use object.length(or something else) to get the number of returned items
// return object.length ?? 1;
return 1;
};
const complexity = getComplexity({
schema,
operationName: request.operationName,
query: document!,
variables: request.variables,
estimators: [responseEstimator],
});
logger.info('Actual query complexity points:', { complexity });
},
})
}
It seems for this implementation to work I'd need access to the path of the field, like in https://github.com/graphql/graphql-js/blob/main/src/type/definition.ts#L896 or the fourth field on the resolvers(info.path
), so I can map the field in which the complexity is being calculated to the actual response