Search code examples
react-nativerelayjs

getFiles() in react native with relayjs mutations


I need to download multiple files from react native app throw relayjs mutation. Files array looks like:

[ {uri: 'file:///path/to/file.jpg', mime: 'image/jpg'}, {uri: 'file:///path/to/file2.jpg', mime: 'image/jpg'}, ]

I found getFiles() func for mutations, but I don't understand what format of the data it should return?


Solution

  • This is how I've solved it:

    I used this package https://github.com/jeanpan/react-native-camera-roll-picker to get the image from the camera roll:

    You can use it to test, add this to your render:

    <CameraRollPicker
              callback={this.getSelectedImage}
              imagesPerRow={4}
              imageMargin={1}
              maximum={1}
            />
    

    This is my implementation of getSelectedImage, I'm just sending one image, but you can send multiples images, files is an object where the value is an image.

    getSelectedImage = (images) => {
        const image = images[0];
    
        const files = {
          [image.filename]: {
            uri: image.uri,
            name: image.filename,
          },
        };
    
        const mutation = new UploadUserProfileMutation({
          files,
        });
    
        this.props.relay.commitUpdate(mutation, callbacks);
      };
    

    in your mutation add the method getFiles:

    export default class UploadUserProfileMutation extends Relay.Mutation {
    ...
      getFiles() {
        return this.props.files;
      }
    ...
    }
    

    You should complete the mutation as a normal mutation