0

This is my receiver in the server.

 
  @Mutation(() => String)
  async register(
    @Arg("picture", () => GraphQLUpload) { createReadStream, filename }: any
  ): Promise<string> {
    console.log(__dirname);
    try {
      const promise = await new Promise(async (resolve, reject) =>
        createReadStream()
          .pipe(createWriteStream(__dirname + `/../../../images/${filename}`))
          .on("finish", () => resolve(true))
          .on("error", () => reject(false))
      );
    } catch (err) {
      console.log(err);
    }
    return "";
  }

This is my raw register.graphql which is later compiled using graphql-codegen.

mutation Register($picture: Upload!) {
  register(picture:$picture)
}


I am stuck on the angular part. So far I have this.

  uploadClicked($event: any) {
    let file = $event.target.files[0];
    console.log(file);
    this.registerGQL.mutate({ picture: { file } }).subscribe((res) => {
      console.log(res);
    });
  }

This is my error when I try to submit it. enter image description here

S. Karki
  • 419
  • 3
  • 14
  • FromData is used for 'manual' querying (using axios/fetch) - apollo client processes (should) it internally, simply try to pass a `file` as `picture` variable – xadm Sep 29 '21 at 21:33
  • @xadm I'm still getting the same errors --- ```let file = $event.target.files[0]; console.log(file); this.registerGQL.mutate({ picture: file }).subscribe((res) => { console.log(res); }); ``` – S. Karki Sep 30 '21 at 04:48
  • API tested separately (postman/insomnia)? is network request properly formatted (graphql upload specs)? – xadm Sep 30 '21 at 06:29
  • @xadm Yes the API works perfectly with postman. Its just the angular part I'm confused in. – S. Karki Sep 30 '21 at 07:05
  • 1
    network request? I don't know A. ... follow some angular upload tutorial for required config ... adapt until it works (request) according to specs – xadm Sep 30 '21 at 07:11
  • @xadm Am I supposed to do ```this.registerGQL.mutate({ picture: { file } })``` or ```this.registerGQL.mutate({ picture: file })```? – S. Karki Sep 30 '21 at 07:28
  • 1
    you already tested both! rather 2nd one ... still search for proper client config/requirements .. EOT – xadm Sep 30 '21 at 07:33

0 Answers0