使用Flask和Graphene上传文件?

时间:2017-09-21 01:10:50

标签: python flask graphql graphene-python

尝试进行文件上传的变异。 这是我在没有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中的文件上传

1 个答案:

答案 0 :(得分:0)

说,您正在上传照片。

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