im trying to handle the response from my graphQL backend with apollo angular.
When I send a mutation with the mutate() method, it respond a type MutationResult from apollo, which contains a data property which is undefined | null, so when i try to access it with my interface so i can parse the success or errors of the response, and lets say is a mutation thats handling registration or login operations... it will need to handle the JWT token and refreshToken, but im unable to store the values because the upper object isnt defined...
What am I doing wrong?
api.service.ts
registerUser(identification:String,email:String,username:String,
firstname:String, lastName:String,p1:String,p2:String
):Observable<MutationResult<RegisterMutationResult>>{
console.log("En Servicios:\n parametros ingresados:",identification,email,username,firstname,lastName,p1,p2)
return this.apollo.mutate<RegisterMutationResult>(
{
mutation:REGISTER,
variables:{
documento:identification,
email:email,
username:username,
name:firstname,
lastname:lastName,
p1:p1,
p2:p2
},
optimisticResponse:{
data: new RegisterMutationResultData(
new RegisterOperation(
true,
null,
null,
null
)
)
}
}
)
}
Login.component.ts
this.api.registerUser(
this.registerForm.value.documento,
this.registerForm.value.email,
this.registerForm.value.username,
this.registerForm.value.nombre,
this.registerForm.value.apellido,
this.registerForm.value.password1,
this.registerForm.value.password2,
)
.subscribe(
{
next: val=>{
console.warn('In registerUser next:')
// console.log(val.data)
this.aux_reg= new RegisterMutationResult(
val.data!.data
)
console.log(this.aux_reg)
},
error: e=>{
console.log('Error:',e)
},
complete: ()=>{
console.log('Register mutation complete')
}
}
)
I tried to add the TS2.0 non-null assertion operator as per this response from the apollo github
console.log(val.data) returns a value, but whenever i try to access it to read the token or the success variable. It returns undefined...
is there a better way to handle this operation? what am I missing?
BTW: I'd come from a background im C/C++, python/django and im kinda new to angular, js, Ts and nextjs overall