slicknode/graphql-query-complexity

Is there an easy way to calculate complexity on just the operationName called?

purpleD17 opened this issue · 4 comments

Right now I've plugged in the basic queryComplexity validationRule but noticed that i'm getting many logs of complexity queries. It turns out that clients are sometimes sending me POST objects with entire schemas full of queries and only calling one via operationName. I am therefore getting many scores logging, eventually i get NaNs logged then eventually it's erroring out complaining that my variables are missing in my query. For example

"{\"errors\":[{\"message\":\"Argument \\\"site\\\" of required type \\\"Site!\\\" was provided the variable \\\"$site\\\" which was not provided a runtime value.

When I take out the extra querys in the incoming POST, I actually get a correct logged query complexity score and no error.

So one option I have working is to manually call getComplexity which takes a query- here i can filter out all queries except for the one matching operationName. However, this is pretty hard to do if there is a query with a ... fragment.

I wonder if this is a feature we can have built into the module from the get go? Only score what the user is actually calling based on operationName ? thanks.

Wow, i actually found someone who solved this online:

https://github.com/MichalLytek/type-graphql/blob/v0.17.5/examples/query-complexity/index.ts#L31

Though it could still be built in to this module.

ivome commented

Thanks for bringing this up @purpleD17
I'd happily add this to the library. Would you mind creating a PR for this? I think we could just add an optional option to pass the operationName to the complexity rule and then add the logic from the example that you found.

Yeah I can create a pr but I'm new to this stuff so it's something I'll have to work on a bit when I have some free time. I'm going to https://summit.graphql.com/ so it's something I can try to work on when i'm there - good place to get in person help if I need it.

ivome commented

This is now available in v0.5.0