graphql-python / graphql-python/flask-graphql
[MIDDLEWARE] middleware executed many times
- Linguagem predominante
- Python
- Estrelas
- 1.3k
- Forks
- 139
- Métricas de merge de PRs
- Nenhum PR com merge em 30d
Descrição
Hi there, I'm using flask-graphql in a project and got into a question/issue I don't have many clues. I'm using middlewares; this is the minimum example:
```
from flask import Flask, request
from flask_graphql import GraphQLView
from graphene import Field, ObjectType, Schema, String, relay
class Hello(ObjectType):
message = String()
class Query(ObjectType):
hello = Field(Hello)
def resolve_hello(self, info):
return Hello(message="Hi there")
class DummyMiddleware(object):
def resolve(self, next, root, info, **kwargs):
print("DummyMiddleware")
return next(root, info, **kwargs)
dummy_middleware = DummyMiddleware()
schema = Schema(query=Query)
app = Flask(__name__)
app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql',
schema=schema,
graphiql=True,
middleware=[dummy_middleware]))
if __name__ == "__main__":
app.run(host='0.0.0.0', threaded=True)
```
(The problem happens no matter the version; the problem also happens when launching the wsgi app with gunicorn).
If I query "hello()", the "DummyMiddleware" will appear twice. If I query a list of elements, the middleware is executed lots of time, twice per element it seems.
For a query this is _just_ a problem of performance, but in a Logout() mutation what happens is:
- 1st time - the authenticated request is valid, so "logout()" deletes the token
- 2nd time - the authenticated request is not valid (because the token has been deleted), so it returns an error
Is there anything wrong in my approach to middlewares? I'm not sure which part is responsible (flask, graphql, my configuration...). Any hint would be appreciated.
Guia de contribuição
Nenhum guia de contribuição indexado para este repositório
Direção de pesquisa
Comece pela configuração de GraphQLView.as_view e DummyMiddleware.resolve mostrada na issue e, em seguida, reproduza o comportamento com a query hello, uma query de lista e a mutation logout. Compare as contagens de invocação do middleware e determine o comportamento esperado de uma única execução antes de identificar qual componente é responsável.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- flask, graphql, python
- Domínio
- api, backend
- Tipo de issue
- Bug
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Status de atividade
- Estagnada
- Clareza
- Precisa de esclarecimento
- Facilidade para iniciantes
- 25/100