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

FragmentSpread' object has no attribute 'selection_set' with custom Middleware #1354

Open
CAPSLOCKFURY opened this issue Jul 28, 2021 · 0 comments
Labels

Comments

@CAPSLOCKFURY
Copy link

CAPSLOCKFURY commented Jul 28, 2021

Hello, I am using custom middleware to mesure complexity of graphql query, but it throws an error FragmentSpread' object has no attribute 'selection_set' with custom Middleware when using fragments.
Query example

query{
  getProfile(id:1){
	...test
  }
}

fragment test on ProfileType{
  id
}

The middleware

from graphql.backend.core import GraphQLCoreBackend

def measure_depth(selection_set, level=1):
    current_depth = level
    for field in selection_set.selections:
        if field.selection_set:
            new_depth = measure_depth(field.selection_set, level=level + 1)
            if new_depth > current_depth:
                current_depth = new_depth
            if new_depth > 3:
                raise Exception('Query is too nested')
    return current_depth


class DepthAnalysisBackend(GraphQLCoreBackend):
    def document_from_string(self, schema, document_string):
        document = super().document_from_string(schema, document_string)
        ast = document.document_ast
        for definition in ast.definitions:
            if len(definition.selection_set.selections) > 1:
                raise Exception("Query is too complex")
            measure_depth(definition.selection_set)                

        return document

How to fix this, and what Fragments use instead of selection_set

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

No branches or pull requests

1 participant