Skip to content

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

Closed
@purpleD17

Description

@purpleD17

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.

Activity

purpleD17

purpleD17 commented on Oct 24, 2019

@purpleD17
Author

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

ivome commented on Oct 25, 2019

@ivome
Collaborator

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.

purpleD17

purpleD17 commented on Oct 25, 2019

@purpleD17
Author

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

ivome commented on Apr 17, 2020

@ivome
Collaborator

This is now available in v0.5.0

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @purpleD17@ivome

        Issue actions

          Is there an easy way to calculate complexity on just the operationName called? · Issue #22 · slicknode/graphql-query-complexity