Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: adding path to estimators args(for calculating actual query complexity) #82

Open
eduhenke opened this issue Oct 11, 2022 · 0 comments

Comments

@eduhenke
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant