首页 > 解决方案 > 如何在 graphql-flask 中应用订阅?

问题描述

我是 GraphQL 的新手,并且在 Flask 中使用订阅。但得到错误。以下是我创建的文件。在 models.py 文件中,我有“订单”表。我无法理解在哪个文件中显示错误。如果有人知道答案,请告诉我。

应用程序.py

app = Flask(__name__)
sockets = Sockets(app)
pubsub = RedisPubsub()
CORS(app)

subscription_mgr = SubscriptionManager(schema, pubsub)

@sockets.route('/socket')
def socket_channel(websocket):
    subscription_server = SubscriptionServer(subscription_mgr, websocket)
    subscription_server.handle()
    return []    

@app.teardown_appcontext
def shutdown_session1(exception=None):
    db_session_ordermanag.remove()


if __name__ == "__main__":
    from geventwebsocket import WebSocketServer

    server = WebSocketServer(('', 5000), app)
    server.serve_forever()

订阅.py

class Subscription(graphene.ObjectType):
    orders = graphene_sqlalchemy.SQLAlchemyConnectionField(
        Order, active=graphene.Boolean())

    def resolve_orders(self, args, context, info):
        with app.app_context():
        query = Order.get_query(context)
        return query.filter_by(id=info.root_value.get('id'))

突变.py

class CreateOrder(graphene.ClientIDMutation):
    class Input:
        #id = graphene.Int()
        revision = graphene.Int()
        name = graphene.String()
        statusid = graphene.Int()

    ok = graphene.Boolean()
    order = graphene.Field(lambda: Order)

    @classmethod
    def mutate_and_get_payload(cls, args, context, info):
        _input = args.copy()
        del _input['clientMutationId']
        new_order = OrderModel(**_input)
        db_session_pg.add(new_order)
        db_session_pg.commit()
        ok = True
        if pubsub.subscriptions:
            pubsub.publish('orders', new_order.as_dict())
        return CreateOrder(ok=ok, order=new_order)

错误:

  File "C:\Users\mpal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\graphql\execution\executor.py", line 36, in <module>
    from .middleware import MiddlewareManager
  File "C:\Users\mpal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\graphql\execution\middleware.py", line 73, in <module>
    @promisify
  File "C:\Users\mpal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\promise\promise.py", line 444, in promisify
    raise TypeError("Object is not a Promise like object.")
TypeError: Object is not a Promise like object.

如何解决此错误?

标签: pythongraphql

解决方案


推荐阅读