Merge pull request #19768 from mozilla/FXA-12756

fix(settings): Handle null Apollo cache reads safely
This commit is contained in:
Valerie Pomerleau 2025-12-17 12:03:44 -08:00 committed by GitHub
commit eafa9e7297
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 39 deletions

View File

@ -388,13 +388,17 @@ export class Account implements AccountData {
}
}
private get data() {
const { account } = this.apolloClient.cache.readQuery<{
account: AccountData;
}>({
private get data(): AccountData {
// readQuery is cache-only by default
const result = this.apolloClient.readQuery<{ account: AccountData }>({
query: GET_ACCOUNT,
})!;
return account;
});
if (!result?.account) {
throw new Error('Account data not loaded from Apollo cache');
}
return result.account;
}
get loading() {
@ -541,32 +545,12 @@ export class Account implements AccountData {
);
}
async getSecurityEvents() {
async getSecurityEvents(): Promise<SecurityEvent[]> {
const { data } = await this.apolloClient.query({
fetchPolicy: 'network-only',
query: GET_SECURITY_EVENTS,
});
const { account } = data;
return account.securityEvents;
}
async getProfileInfo() {
try {
const { data } = await this.apolloClient.query({
fetchPolicy: 'network-only',
query: GET_PROFILE_INFO,
});
const { account } = data;
return account as ProfileInfo;
} catch (err) {
const errno = (err as ApolloError).graphQLErrors[0].extensions?.errno as
| number
| undefined;
if (errno && AuthUiErrorNos[errno]) {
throw AuthUiErrorNos[errno];
}
throw AuthUiErrors.UNEXPECTED_ERROR;
}
return data?.account?.securityEvents ?? [];
}
async getRecoveryKeyBundle(

View File

@ -7,7 +7,7 @@ import React from 'react';
import config from '../../lib/config';
import firefox, { FirefoxCommand } from '../../lib/channels/firefox';
import { createApolloClient } from '../../lib/gql';
import { Account, GET_PROFILE_INFO } from '../Account';
import { GET_PROFILE_INFO } from '../Account';
import { AlertBarInfo } from '../AlertBarInfo';
export const INITIAL_SETTINGS_QUERY = gql`
@ -95,17 +95,25 @@ export function initializeSettingsContext() {
const alertBarInfo = new AlertBarInfo();
const apolloClient = createApolloClient(config.servers.gql.url);
const GET_UID_QUERY = gql`
query GetUid {
account {
uid
}
}
`;
const isForCurrentUser = (event: Event) => {
const { account } = apolloClient.cache.readQuery<{ account: Account }>({
query: gql`
query GetUid {
account {
uid
}
}
`,
})!;
return account.uid === (event as CustomEvent).detail.uid;
const data = apolloClient.readQuery<{ account: { uid: string } }>({
query: GET_UID_QUERY,
});
if (!data?.account?.uid) {
return false;
}
const currentUid = data.account.uid;
const eventUid = (event as CustomEvent).detail?.uid;
return currentUid != null && currentUid === eventUid;
};
firefox.addEventListener(FirefoxCommand.ProfileChanged, (event) => {