Skip to content

thoughts on implementing Query Complexity Analysis? #18

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

Closed
roippi opened this issue Jul 16, 2016 · 2 comments
Closed

thoughts on implementing Query Complexity Analysis? #18

roippi opened this issue Jul 16, 2016 · 2 comments

Comments

@roippi
Copy link
Contributor

roippi commented Jul 16, 2016

This is a feature that's popping up in a few other framework implementations, and I think it's pretty killer - it lets you do a reasonable job at statically detecting pathologic queries.

There are some more complex ideas floating out there in the community, namely graphql/graphql-js#304. This could be used both for complexity/cost analysis in addition to query resolution optimization, though it seems more useful for optimization. Probably overkill here.

Has anyone already thought of implementing this here, or should I take a crack at it?

@viniychuk
Copy link
Member

Hi @roippi , I started to work on it but the first priority is to get the docs updated. We can definitely discuss your ideas (let's say in gitter) so I can have as much input as possible.

@roippi
Copy link
Contributor Author

roippi commented Jul 20, 2016

Thanks. I have a working POC here: master...roippi:complexity_analysis_wip

You can implement a 'cost' function on fields, e.g.:

cost: function($args, $config, $childScore) {
  return 10 + $args['count'] * $childScore;
}

For a query:

{
  things (count:5) {
    someField
    someExpensiveSubQuery {
      someOtherField
    }
  }
}

And things would have access to the combined calculated score of all its subfields/subqueries.

Usage is pretty easy:

$processor = new Processor(new $mySchema());
$processor->setMaxComplexity(42);
// process normally, if complexity is exceeded it will return an error instead of trying to resolve the query

I'll hop into gitter to discuss. This is still a WIP so I'm open to feedback on the API, implementation, or test coverage 😄 .

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

No branches or pull requests

2 participants