我正在使用react-relay/compat 1.1.0,我需要编写一个具有上传文件能力的突变。在中继经典中,您可以使用getFiles()来支持文件在突变中的上传:
class AddImageMutation extends Relay.Mutation {
getMutation() {
return Relay.QL`mutation{ introduceImage }`;
}
getFiles() {
return {
file: this.props.file,
};
}
...
}但是还没有找到在继电器现代文档中上传文件的任何功能:
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: ... ?
},
);这在现代继电器中还支持吗?如果是这样的话,怎么做呢?谢谢。
发布于 2017-11-02 08:37:06
您必须在uploadables的config对象中以对象commitMutation的形式提供文件,然后在网络层中实现实际的上传,因为对服务器的获取请求必须是一个多部分的表单,而不是application/json。
有关完整的示例,请参见https://github.com/facebook/relay/issues/1844#issuecomment-316893590。
发布于 2017-07-30 19:49:40
我发现了这个问题,因为我自己也有一个问题。
还不确定完整的答案,但我开始阅读中继源代码,基于packages/relay-runtime/mutations/commitRelayModernMutation.js,看起来您可以将uploadables传递给您的突变。
https://stackoverflow.com/questions/45288655
复制相似问题