python - 使用 GraphQL +Ariadne + Flask 上传文件:graphql.error.graphql_error.GraphQLError: Operation data should be an JSON object
问题描述
我正在尝试使用此技术堆栈(Flask + Graphql + Ariadne)上传文件。我已经实现了其他 API,但现在对于文件上传,它返回了这个错误。
错误
Traceback (most recent call last):
File "d:\project\python-envs\graph_ql_env\lib\site-packages\ariadne\graphql.py", line 138, in graphql_sync
validate_data(data)
File "d:\project\python-envs\graph_ql_env\lib\site-packages\ariadne\graphql.py", line 354, in validate_data
raise GraphQLError("Operation data should be a JSON object")
graphql.error.graphql_error.GraphQLError: Operation data should be a JSON object
127.0.0.1 - - [Time] "POST /graphql HTTP/1.1" 400 -
项目文件。
架构.graphql
scalar Upload
type Mutation {
uploadProfileImage(id: String!, image: Upload!): Boolean!
}
突变.py
@convert_kwargs_to_snake_case
def resolve_upload_profile_image(obj, info, id, file):
主文件
mutation.set_field("uploadProfileImage", M.resolve_upload_profile_image)
type_defs = load_schema_from_path("schema.graphql")
schema = make_executable_schema(
type_defs, query, mutation, upload_scalar, snake_case_fallback_resolvers
)
@app.route("/graphql", methods=["POST"])
def graphql_server():
data = request.get_json()
success, result = graphql_sync(
schema,
data,
context_value=request,
debug=app.debug
)
status_code = 200 if success else 400
return jsonify(result), status_code
使用 Altair 进行查询
mutation ($image: Upload!) {
uploadProfileImage (id:"qwe1", image:$image)
}
解决方案
@graphql_bp.route("/", methods=["POST"])
def graphql_server():
if request.content_type.startswith("multipart/form-data"):
data = combine_multipart_data(
json.loads(request.form.get("operations")),
json.loads(request.form.get("map")),
dict(request.files)
)
else:
data = request.get_json()
success, result = graphql_sync(
schema,
data,
context_value=request,
debug=current_app.debug
)
status_code = 200 if success else 400
return jsonify(result), status_code
推荐阅读
- javascript - 使用javascript自动选择自定义选择框中的第一个选项
- php - 获取特定类别的所有帖子 - Wordpress
- javascript - 如何在ajax成功上加载带有json数据的网格数据
- javascript - 对动态 HTML 表应用多重过滤
- javascript - 如何从服务器水合 vee-validate
- botframework - 使用 MS Teams 作为通道:身份验证对话框(Microsoft.Bot.Builder.Dialogs 中的 GetTokenDialog 类)不会弹出
- vba - INSERT INTO 按钮只插入一次
- python - Python在文本文件中进行数字排序
- javascript - 异步登录谷歌地图脚本
- html - IMG Modal 仅适用于第一张图片