graphql-python / graphql-python/graphene

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

Ouverte
#1,354 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
🐛 bug
Langage dominant
Python
Étoiles
8.2k
Forks
818
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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
```python
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

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

The failure occurs in the custom DepthAnalysisBackend.document_from_string traversal shown in the report; start by inspecting the AST nodes in document.document_ast, including FragmentSpread and the fragment definition. Confirm the supplied fragment query completes without the attribute error and that the depth check still behaves as intended.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
graphql, python
Domaine
api, backend
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
42/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.