Reactjs 如何使用graphql和graphene上传文件
我正在尝试使用graphql和graphene上传一个文件 在前端,我有获取请求,如下所示:Reactjs 如何使用graphql和graphene上传文件,reactjs,graphql,graphene-django,Reactjs,Graphql,Graphene Django,我正在尝试使用graphql和graphene上传一个文件 在前端,我有获取请求,如下所示: fetch( '/graphql', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({query: MUTATION, variables: {file: fi
fetch(
'/graphql', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({query: MUTATION, variables: {file: file}})
})
.then(res => res.json())
.then(res => {
debugger;
})
.catch(error => {
debugger;
});
const MUTATION = `mutation UploadMutation($file: Upload!) {
uploadMutation(file: $file) {
ok
url
}
}`;
class UploadMutation(graphene.Mutation):
class Arguments:
file = Upload(required=True)
ok = graphene.Boolean()
url = graphene.String()
def mutate(root, info, file, **kwargs):
url = handle_upload_file(file)
ok = True
return UploadMutation(ok=ok, url=url)
lastModified: 1569487573753
lastModifiedDate: Thu Sep 26 2019 10:46:13 GMT+0200 (Central European Summer Time) {}
name: "1.png"
path: "1.png"
size: 138373
type: "image/png"
webkitRelativePath: ""
突变情况如下:
fetch(
'/graphql', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({query: MUTATION, variables: {file: file}})
})
.then(res => res.json())
.then(res => {
debugger;
})
.catch(error => {
debugger;
});
const MUTATION = `mutation UploadMutation($file: Upload!) {
uploadMutation(file: $file) {
ok
url
}
}`;
class UploadMutation(graphene.Mutation):
class Arguments:
file = Upload(required=True)
ok = graphene.Boolean()
url = graphene.String()
def mutate(root, info, file, **kwargs):
url = handle_upload_file(file)
ok = True
return UploadMutation(ok=ok, url=url)
lastModified: 1569487573753
lastModifiedDate: Thu Sep 26 2019 10:46:13 GMT+0200 (Central European Summer Time) {}
name: "1.png"
path: "1.png"
size: 138373
type: "image/png"
webkitRelativePath: ""
在后端方面,我有如下变化:
fetch(
'/graphql', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({query: MUTATION, variables: {file: file}})
})
.then(res => res.json())
.then(res => {
debugger;
})
.catch(error => {
debugger;
});
const MUTATION = `mutation UploadMutation($file: Upload!) {
uploadMutation(file: $file) {
ok
url
}
}`;
class UploadMutation(graphene.Mutation):
class Arguments:
file = Upload(required=True)
ok = graphene.Boolean()
url = graphene.String()
def mutate(root, info, file, **kwargs):
url = handle_upload_file(file)
ok = True
return UploadMutation(ok=ok, url=url)
lastModified: 1569487573753
lastModifiedDate: Thu Sep 26 2019 10:46:13 GMT+0200 (Central European Summer Time) {}
name: "1.png"
path: "1.png"
size: 138373
type: "image/png"
webkitRelativePath: ""
上传文件如下:
fetch(
'/graphql', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({query: MUTATION, variables: {file: file}})
})
.then(res => res.json())
.then(res => {
debugger;
})
.catch(error => {
debugger;
});
const MUTATION = `mutation UploadMutation($file: Upload!) {
uploadMutation(file: $file) {
ok
url
}
}`;
class UploadMutation(graphene.Mutation):
class Arguments:
file = Upload(required=True)
ok = graphene.Boolean()
url = graphene.String()
def mutate(root, info, file, **kwargs):
url = handle_upload_file(file)
ok = True
return UploadMutation(ok=ok, url=url)
lastModified: 1569487573753
lastModifiedDate: Thu Sep 26 2019 10:46:13 GMT+0200 (Central European Summer Time) {}
name: "1.png"
path: "1.png"
size: 138373
type: "image/png"
webkitRelativePath: ""
但是在mutate函数中,我得到:
{'path': '1.png'}
知道我做错了什么吗?你没有在搜索。。。解释了很多次。。。上传需求multipart@xadm好啊在这种情况下怎么做?一如既往。。。搜索邮递员示例。。。检查/准备后端。。。使用reactto wide问题重新创建邮递员请求。。。。我应该搜索副本并建议关闭它吗?或者写下一篇教程;作为回答?只需遵循提示,使用早期的答案(许多答案使用react和python),谷歌。。。。回到更详细(但更接近标准解决方案)的问题。。。或者等别人我知道你在找什么。。。。react+graphql+upload+python是一个非常流行的问题,很多问题都有帮助。。。搜索是关键。。。我只是不知道你在跟踪哪个
“graphql上传graphene django”
谷歌的结果?单独的问题,它将不再是“特例”