【问题标题】:Upload files using Flask and Graphene?使用 Flask 和 Graphene 上传文件?
【发布时间】:2018-03-02 05:05:45
【问题描述】:

尝试进行文件上传的突变。 这是我在没有 GraphQL 的情况下尝试过的,效果很好:

@app.route('/upload', methods=['POST'])
def uploadFile():
    for key in request.files:
        file = request.files[key]
        file.save(os.path.join(UPLOAD_FOLDER, file.filename))

    return 'uploaded'

虽然不确定如何在 GraphQL 中执行此操作,但我想将其包含在 my example repo 中,以便每个人都知道该怎么做。 Here's what 我在 Node.js 中用于文件上传

【问题讨论】:

    标签: python flask graphql graphene-python


    【解决方案1】:

    说,你正在上传一张照片。

    class Upload(graphene.Scalar):
        def serialize(self):
            pass
    
    class UploadPhoto(graphene.Mutation):
        class Input:
            photo = Upload()
            id = graphene.String(required=True)
    
        ok = graphene.Boolean()
    
        @staticmethod
        def mutate(root, args, context, info):
            files = request.files
            photo = files['variables.photo']
    
            return UploadPhoto(ok=True)
    
    class MyMutations(graphene.ObjectType):
        upload_photo = UploadPhoto.Field()
    
    schema = graphene.Schema(query=XXX, mutation=MyMutations, types=[Upload])
    

    礼貌:https://github.com/graphql-python/graphene-django/issues/101#issuecomment-331079482

    【讨论】:

      猜你喜欢
      • 2018-09-05
      • 2017-04-10
      • 2015-02-22
      • 2013-12-03
      • 1970-01-01
      • 2012-08-02
      • 2019-04-17
      • 1970-01-01
      • 2018-05-20
      相关资源
      最近更新 更多