我正在使用react-relay/compat 1.1.0
,我需要编写一个能够上传文件的突变。
在 Relay Classic 中,您可以使用 getFiles()
来支持突变中的文件上传:
class AddImageMutation extends Relay.Mutation {
getMutation() {
return Relay.QL`mutation{ introduceImage }`;
}
getFiles() {
return {
file: this.props.file,
};
}
...
}
但是在 Relay Modern 中没有发现任何上传文件的功能痕迹文档:
const {commitMutation} = require('react-relay');
commitMutation(
environment: Environment,
config: {
mutation: GraphQLTaggedNode,
variables: Variables,
onCompleted?: ?(response: ?Object) => void,
onError?: ?(error: Error) => void,
optimisticResponse?: ?() => Object,
optimisticUpdater?: ?(store: RecordSourceSelectorProxy) => void,
updater?: ?(store: RecordSourceSelectorProxy) => void,
configs?: Array<RelayMutationConfig>,
// files: ... ?
},
);
现代中继是否支持这一点?如果是这样,该怎么做?谢谢。
最佳答案
您必须在 commitMutation
的 config
对象中将文件作为对象 uploadables
提供,然后在网络层中实现实际上传,因为对服务器的获取请求必须是多部分形式,而不是 application/json。
参见https://github.com/facebook/relay/issues/1844#issuecomment-316893590获取完整示例。
关于graphql - 如何使用 Relay Modern 突变进行文件上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45288655/