Skip to content

Commit

Permalink
fix(graphql): handle nullable response fields that can occur when the…
Browse files Browse the repository at this point in the history
…re are server errors (#1246)
  • Loading branch information
andrewazores authored Apr 23, 2024
1 parent 20dcce1 commit a66caeb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
22 changes: 12 additions & 10 deletions src/app/Archives/AllTargetsArchivedRecordingsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,17 @@ export const AllTargetsArchivedRecordingsTable: React.FC<AllTargetsArchivedRecor
)
.pipe(
map((v) => {
return v.data.targetNodes.map((node) => {
const target: Target = node.target;
return {
target,
targetAsObs: of(target),
archiveCount: node.target.archivedRecordings.aggregate.count,
recordings: node.target.archivedRecordings.data as ArchivedRecording[],
};
});
return v.data?.targetNodes
?.map((node) => {
const target: Target = node?.target;
return {
target,
targetAsObs: of(target),
archiveCount: node?.target?.archivedRecordings?.aggregate?.count ?? 0,
recordings: (node?.target?.archivedRecordings?.data as ArchivedRecording[]) ?? [],
};
})
.filter((v) => !!v.target);
}),
)
.subscribe({
Expand Down Expand Up @@ -258,7 +260,7 @@ export const AllTargetsArchivedRecordingsTable: React.FC<AllTargetsArchivedRecor
target: target,
targetAsObs: of(target),
archiveCount: v.data.targetNodes[0]?.target?.archivedRecordings?.aggregate?.count ?? 0,
recordings: v.data.targetNodes[0]?.target?.archivedRecordings,
recordings: v.data.targetNodes[0]?.target?.archivedRecordings ?? [],
},
];
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/Dashboard/AutomatedAnalysis/AutomatedAnalysisCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export const AutomatedAnalysisCard: DashboardCardFC<AutomatedAnalysisCardProps>
queryArchivedRecordings(target.id!)
.pipe(
first(),
map((v) => v.data.targetNodes[0].target.archivedRecordings.data as ArchivedRecording[]),
map((v) => (v.data?.targetNodes[0]?.target?.archivedRecordings?.data as ArchivedRecording[]) ?? []),
)
.subscribe({
next: (recordings) => {
Expand Down Expand Up @@ -368,7 +368,7 @@ export const AutomatedAnalysisCard: DashboardCardFC<AutomatedAnalysisCardProps>
}
}
}),
map((v) => v.data.targetNodes[0].target.activeRecordings.data[0] as Recording),
map((v) => v.data?.targetNodes[0]?.target?.activeRecordings?.data[0] as Recording),
tap((recording) => {
if (recording === null || recording === undefined) {
throw new Error(NO_RECORDINGS_MESSAGE);
Expand Down
4 changes: 2 additions & 2 deletions src/app/RecordingMetadata/BulkEditLabels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export const BulkEditLabels: React.FC<BulkEditLabelsProps> = ({
{ filter: { sourceTarget: UPLOADS_SUBDIRECTORY } },
)
.pipe(
map((v) => v.data.archivedRecordings.data as ArchivedRecording[]),
map((v) => (v.data?.archivedRecordings?.data as ArchivedRecording[]) ?? []),
first(),
)
: context.target.target().pipe(
Expand Down Expand Up @@ -213,7 +213,7 @@ export const BulkEditLabels: React.FC<BulkEditLabelsProps> = ({
{ id: target.id! },
),
),
map((v) => v.data.targetNodes[0].target.archivedRecordings.data as ArchivedRecording[]),
map((v) => (v.data?.targetNodes[0]?.target?.archivedRecordings?.data as ArchivedRecording[]) ?? []),
first(),
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/Recordings/ArchivedRecordingsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export const ArchivedRecordingsTable: React.FC<ArchivedRecordingsTableProps> = (
} else if (isUploadsTable) {
addSubscription(
queryUploadedRecordings()
.pipe(map((v) => v.data.archivedRecordings.data as ArchivedRecording[]))
.pipe(map((v) => (v?.data?.archivedRecordings?.data as ArchivedRecording[]) ?? []))
.subscribe({
next: handleRecordings,
error: handleError,
Expand All @@ -260,7 +260,7 @@ export const ArchivedRecordingsTable: React.FC<ArchivedRecordingsTableProps> = (
filter((target) => !!target),
first(),
concatMap((target: Target) => queryTargetRecordings(target.id!)),
map((v) => v.data.targetNodes[0].target.archivedRecordings.data as ArchivedRecording[]),
map((v) => (v.data?.targetNodes[0]?.target?.archivedRecordings?.data as ArchivedRecording[]) ?? []),
)
.subscribe({
next: handleRecordings,
Expand Down
20 changes: 10 additions & 10 deletions src/app/Shared/Services/Api.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ export class ApiService {
recordingName,
labels: labels.map((label) => ({ key: label.key, value: label.value })),
},
).pipe(map((v) => v.data.archivedRecordings.data as ArchivedRecording[]));
).pipe(map((v) => (v.data?.archivedRecordings?.data as ArchivedRecording[]) ?? []));
}

isProbeEnabled(): Observable<boolean> {
Expand Down Expand Up @@ -972,7 +972,7 @@ export class ApiService {
},
),
),
map((v) => v.data.targetNodes[0].target.archivedRecordings as ArchivedRecording[]),
map((v) => (v.data?.targetNodes[0]?.target?.archivedRecordings as ArchivedRecording[]) ?? []),
);
}

Expand Down Expand Up @@ -1000,7 +1000,7 @@ export class ApiService {
recordingName,
labels: labels.map((label) => ({ key: label.key, value: label.value })),
},
).pipe(map((v) => v.data.archivedRecordings.data as ArchivedRecording[]));
).pipe(map((v) => (v.data?.archivedRecordings?.data as ArchivedRecording[]) ?? []));
}

postTargetRecordingMetadata(recordingName: string, labels: KeyValue[]): Observable<ActiveRecording[]> {
Expand Down Expand Up @@ -1037,7 +1037,7 @@ export class ApiService {
},
),
),
map((v) => v.data.targetNodes[0].target.activeRecordings as ActiveRecording[]),
map((v) => (v.data?.targetNodes[0]?.target?.activeRecordings as ActiveRecording[]) ?? []),
);
}

Expand Down Expand Up @@ -1180,7 +1180,7 @@ export class ApiService {
).pipe(
first(),
map((body) =>
body.data.environmentNodes[0].descendantTargets.reduce(
(body.data?.environmentNodes[0]?.descendantTargets ?? []).reduce(
(acc: number, curr) => acc + curr.target.activeRecordings.aggregate.count,
0,
),
Expand Down Expand Up @@ -1212,11 +1212,11 @@ export class ApiService {
true,
).pipe(
map((resp) => {
const nodes = resp.data.targetNodes;
const nodes = resp.data?.targetNodes ?? [];
if (nodes.length === 0) {
return false;
}
const count = nodes[0].target.activeRecordings.aggregate.count;
const count = nodes[0]?.target?.activeRecordings?.aggregate?.count ?? 0;
return count > 0;
}),
catchError((_) => of(false)),
Expand Down Expand Up @@ -1292,11 +1292,11 @@ export class ApiService {
{ id: target.id! },
).pipe(
map((resp) => {
const nodes = resp.data.targetNodes;
const nodes = resp.data?.targetNodes ?? [];
if (!nodes || nodes.length === 0) {
return {};
}
return nodes[0]?.target.mbeanMetrics;
return nodes[0]?.target?.mbeanMetrics ?? {};
}),
catchError((_) => of({})),
);
Expand Down Expand Up @@ -1329,7 +1329,7 @@ export class ApiService {
{ id: target.id! },
true,
true,
).pipe(map((v) => v.data.targetNodes[0].target.archivedRecordings.data as ArchivedRecording[]));
).pipe(map((v) => (v.data?.targetNodes[0]?.target?.archivedRecordings?.data as ArchivedRecording[]) ?? []));
}

getTargetActiveRecordings(target: TargetStub): Observable<ActiveRecording[]> {
Expand Down
2 changes: 1 addition & 1 deletion src/app/Topology/Entity/EntityDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ const MBeanDetails: React.FC<{
{ id: targetId },
)
.pipe(
map((resp) => resp.data.targetNodes[0].target.mbeanMetrics || {}),
map((resp) => resp.data?.targetNodes[0]?.target?.mbeanMetrics ?? {}),
catchError((_) => of({})),
)
.subscribe(setMbeanMetrics),
Expand Down

0 comments on commit a66caeb

Please sign in to comment.