簡體   English   中英

如何使用Relay Modern突變進行文件上傳?

[英]How to do file uploads with Relay Modern mutations?

我正在使用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: ... ?
  },
);

接力現代還支持嗎? 如果是這樣,這樣做的方式是什么? 謝謝。

您必須在commitMutationconfig對象中提供文件作為對象uploadables文件,然后在網絡層中實現實際上載,因為對服務器的提取請求必須是多部分表單而不是application / json。

有關完整示例,請參閱https://github.com/facebook/relay/issues/1844#issuecomment-316893590

發現這個問題,因為我自己也有同樣的問題。

不知道完整的答案還沒有,但我開始通過繼電器讀取源和基於包/繼電器運行時/突變/ commitRelayModernMutation.js它看起來像你可以通過uploadables你的突變。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM