Mam serwer GraphQL, którego schema wygląda w taki sposób
type Query {
userProfile(userId: String!): Profile
}
type Profile {
id: ID!
email: String!
firstName: String!
lastName: String!
}
I stworzyłem sobie taki serwis
import { Injectable } from '@angular/core';
import { Query } from 'apollo-angular'
import gql from 'graphql-tag'
@Injectable()
export class GetUserProfileGQL extends Query {
document: any = gql`
query GetUserProfile($userId: String!) {
userProfile(userId: $userId) {
id
email
firstName
lastName
}
}
`;
}
Dodatkowo mam taką strukturę
export interface Profile {
id?: string;
email?: string;
firstName?: string;
lastName?: string;
}
I taką metodę setUserInfo(profile: Profile)
, którą wołam po odpytaniu /graphql
private initUserInfo(userId: string): void {
this.getUserProfileGQL.fetch({
userId
}).subscribe(res => {
this.authHolder.setUserInfo(res.data.userProfile);
});
}
I pomimo błędów kompilacji
I tego, że w VSCode się to świeci na czerwono, to wszystko działa jak powinno. Struktura otrzymanej odpowiedzi z /graphql
to
{
"data": {
"userProfile": {
"id": "123",
...
}
}
}
Jak więc przekonać kompilator, że to jest ok?
#Edit
Mogę zrobić coś takiego
this.authHolder.setUserInfo(res.data);
, przypominam jak wygląda sygnatura tej metody - setUserInfo(profile: Profile)
VSCode i kompilacja jest wtedy cicho, ale wewnatrz tej metody user.firstName === undefined
, muszę wyciągnąc user.userProfile.firstName
, które oczywiście przez kompilator zostanie uznane za błąd, ale faktycznie wyciąga dane...