graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

Reduce introspection complexity programmatically

Open ThePlenkov opened this issue 11 months ago • 3 comments

Use case

I'm trying to introspect Gitlab API schema with default introspection query .

Unfortunately Giltab API fails with the error: Query has complexity of 220, which exceeds max complexity of 217

I noticed that in a default query we use a static construction like which takes up to 10 nested levels:

fragment TypeRef on __Type {
      kind
      name
      ofType {
        kind
        name
        ofType {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
                ofType {
                  kind
                  name
                  ofType {
                    kind
                    name
                    ofType {
                      kind
                      name
                      ofType {
                        kind
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

However in case of Gitlab API I'm ok to intentionally reduce complexity.

So if I use postman and just remove one nested level out then introspection works and I can test it in postman.

Of course - I can store this schema in a local file and then work it just by loading. But problem is - there are tons of different tools ( especially from The Guild ) which relies on this module and introspect using this default query.

I don't see here conflicts with a spec if we some explicitly tell what is our limit for the max depth.

Possible solution

If we introduce one more option depthLimit in IntrospectionOptions, Then we can just build mentioned above segment programmtically in a loop.

It may default to 10 as it is working now.

What do you think about this idea?

Thank you!

ThePlenkov avatar Jan 10 '25 11:01 ThePlenkov

Typically the most nested these types would be is non-nullable list-of non-nullable list-of non-nullable thing; i.e. 5 "ofTypes". The default query allows for deeper than this, but honestly it's very rare to see a doubly-nested list, let alone triply nested.

You'll be happy to see this request has already been implemented in https://github.com/graphql/graphql-js/pull/4317; so consider adding a :+1: reaction to the issue description there.

benjie avatar Jan 10 '25 13:01 benjie

@benjie indeed - I also started my own implementation ( which is kind of same with recursion )

Should I park it or is ok to submit one more version for review?

ThePlenkov avatar Jan 10 '25 13:01 ThePlenkov

well , nevermind I checked the code - it's just doing exactly same. Let's stay with same PR indeed

ThePlenkov avatar Jan 10 '25 14:01 ThePlenkov