【问题标题】:Apollo-Upload-Client doesn't return Id like it statesApollo-Upload-Client 不会像它所说的那样返回 Id
【发布时间】:2018-02-18 23:22:30
【问题描述】:

我正在使用apollo-upload-client,所以调用看起来像这样:

const UPLOAD_IMAGE_MUTATION = gql`
mutation AddImageFileMutation ($file: Upload!, $albumId: String!) {
    uploadImage(file: $file, albumId: $albumId){
        id
}}`

您可以看到它应该返回id,但我发现情况并非如此。

我的update: 调用显示{data} 包含{data: {uploadImage: null}}(请参阅下面的代码以供参考)。有没有办法让它返回ID?

更新突变(使用 Dropzone 做参考)....

_onDrop = async (acceptedFiles, rejectedFiles) => {
        acceptedFiles.map((imageFile, index) => {
            const uploadImage = async () => {
                const index = Math.random()
                this.state.loadingBars.push({index: index, preview: imageFile.preview, percentage: 15})
                await this.props.uploadImageMutation({
                    variables: {file: imageFile, albumId: this.state.albumId},
                    update: (store, {data: {uploadImage}}) => {
                        //uploadImage returning null. {data} equals {data: {uploadImage: null}}

                        //stuff
                    }
                })
            };
            uploadImage()
        })
    }

【问题讨论】:

  • 如果uploadImage 为空,听起来你的突变的解析器要么没有返回任何东西,要么返回的对象不是预期的形状。查看您的解析器代码可能会有所帮助。
  • 你是对的,看更新

标签: reactjs graphql apollo


【解决方案1】:

解析代码如下:

uploadImage:...
     resolve:...
          const function1()
          const function2()
          const function3()
          function1() //calls function2() and function3()

我需要添加 return function1() 而不是直接调用它

Schema.js 文件uploadImage 以供参考,如果它可以帮助任何不使用graphql-tools 的人

uploadImage: {
            type: ImageFileType,
            args: {
                albumId: {type: GraphQLString },
                file: {type: ImageScalarType}
            },
            resolve(parentValue, data){
                const storeFS = ({ stream, filename }) => {
                    const id = new ObjectID
                    console.log('added image with ID ', id)
                    const path = `./images/${id}-${filename}`
                    return new Promise((resolve, reject) =>
                        stream
                            .on('error', error => {
                                if (stream.truncated)
                                // Delete the truncated file
                                    fs.unlinkSync(path)
                                reject(error)
                            })
                            .on('end', () => resolve({ id, path }))
                            .pipe(fs.createWriteStream(path))
                    )
                }

                const storeDB = (file, albumId) => {
                    const newImage = new IMAGE({//
                        _id: file.id,
                        name: file.filename,
                        fileType: file.mimetype,
                        url: file.path,
                        albumId: albumId
                    })
                    return new Promise((resolve, reject) => {
                        //Save image to db
                        newImage.save(function (err, callback) {
                            if (err) reject(err)
                            else {
                                resolve(newImage)
                                return new Promise((resolve, reject) => {
                                    //Save image Id to album
                                    ALBUM.findOneAndUpdate(
                                        {"_id": albumId},
                                        {"$addToSet": {images: [callback._id]}},
                                        {"new": true}
                                    ).exec((err, res) => {
                                        if (err) reject(err)
                                        else resolve(res)
                                    })
                                })
                            }
                        })
                    })
                }
                const processUpload = async data => {
                    const { stream, filename, mimetype, encoding } = await data.file //need 0 for array from dropzone
                    const albumId = data.albumId
                    const { id, path } = await storeFS({ stream, filename })
                    return storeDB({ id, filename, mimetype, encoding, path }, albumId)
                }
                return processUpload(data)
            }
        }

【讨论】:

    猜你喜欢
    • 2013-11-13
    • 2019-04-20
    • 2018-07-20
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2014-08-19
    相关资源
    最近更新 更多