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

Automatic query complexity #610

Closed
oprypkhantc opened this issue Jul 28, 2023 · 1 comment · Fixed by #612
Closed

Automatic query complexity #610

oprypkhantc opened this issue Jul 28, 2023 · 1 comment · Fixed by #612

Comments

@oprypkhantc
Copy link
Contributor

Hey.

There was a similar issue here, see last comment: #306 . What's important here is the following: it'd be nice to have graphqlite automatically compute query complexity using pre-defined settings and an attribute. Some other GraphQL implementations (for other languages) have been doing this for a while and it's pretty neat.

For example, here's a guide from HotChocolate (.NET graphql implementation): https://chillicream.com/docs/hotchocolate/v13/security/operation-complexity

The way they do it is pretty simple: they count every non-object and non-list field as 1, and every object or list or anything with a custom resolver as 5, and allow overriding that through an annotation:

type Query {
  books(take: Int = 10): [Book] @cost(complexity: 5, multipliers:[take])
}

type Book {
  title
  author: Author @cost(complexity: 5)
}

type Author {
  name
}

Given this example, a query like this:

query {
  books {
    title
  }
}

costs 10 (take parameter) * (5 (each Book in books cost) + 1 (title of each Book)) = 10 * (5 + 1) = 60

While a query like that:

query {
  books {
    title
    author {
      name
    }
  }
}

costs 10 * (5 + 1 + 5) = 110.

The implementation consists of a new attribute #[Cost(int $complexity = 1, ?int $defaultMultiplier = null, array $multipliers = [])] and a ComplexityFieldMiddleware. Again, thanks to webonyx/graphql-php supporting query complexity out of the box, it's pretty trivial, yet quite nice.

I've already implemented this in our package and would like to backport this to graphqlite, if there's any interest in it here.

@oojacoboo
Copy link
Collaborator

@oprypkhantc Let's do it!

We'll need some good documentation on security and calculating ideal values. How does a multiplier work, and why would you want multiple multipliers - what benefits are there for that, etc.

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.

2 participants