Skip to content

Example for handling file on server #4

@sandervanhooft

Description

@sandervanhooft

@HriBB Do you perhaps have an example how to handle the file on the graphql server?

If I forward it to my REST endpoint, it doesn't seem to be recognised as a file.

My resolver:

...
uploadUserAvatar(root: Function, args: Object) {
      console.log(args.files) // see output below
      Viewer.uploadAvatar(args.token, args.files[0])
      return Viewer.fromToken(args.token)
}
...

Viewer connection:

uploadAvatar(token: string, avatar: Object){
    const options = {
      method: 'POST',
      uri: this.buildRestURL('settings/profile/uploadavatar'),
      headers: {
        'Authorization': `Bearer ${token}`,
      },
      body: {
        avatar,
      },
      json: true,
    }
    return rp(options)
      .then((res) => {
        console.log("\nuploadAvatar SUCCESS:", res)
        return {
          ...res.data,
          token
        }
      })
      .catch((err) => console.log("\nuploadAvatar ERROR:", err))

Console.log(args.files) from resolver:

[ { fieldname: 'files',
       originalname: 'elephant.jpeg',
       encoding: '7bit',
       mimetype: 'image/jpeg',
       destination: '../tmp/',
       filename: '5b4af2fa02e1cfde3995873a2d7ea959',
       path: '../tmp/5b4af2fa02e1cfde3995873a2d7ea959',
       size: 3536 } ] }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions