Skip to content

Commit

Permalink
fix: Null locations (#17)
Browse files Browse the repository at this point in the history
* fix: Handle null/deactivated locations

* fix: Handle null/deactivated locations in UI
  • Loading branch information
pettermachado authored Nov 12, 2024
1 parent c8e32ee commit 32ddbf6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/components/columns/zone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const columns: ColumnDef<ZoneRow>[] = [
{
id: "location",
header: "Location",
accessorFn: ({ zone }) => zone?.location.name,
accessorFn: ({ zone }) => zone?.location?.name,
meta: {
filter: "select",
},
Expand Down
2 changes: 1 addition & 1 deletion app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export type Account = {
export type Zone = {
id: string;
name: string;
location: Location;
location: Location | null;
account: { id: string };
};

Expand Down
22 changes: 17 additions & 5 deletions lib/soundtrack-api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ import { getLogger } from "../logger/index.js";
const logger = getLogger("lib/soundtrack-api/client");
const semaphore = new Semaphore(3);

type GraphQLErrorPathSegment = string | number;
type GraphQLErrorType = {
message: string;
path: GraphQLErrorPathSegment[] | null | undefined;
extensions: { code: string } | null | undefined;
};
type ErrorType = GraphQLErrorType;

type QueryResponse<T> = {
data: T;
errors?: unknown[];
errors?: ErrorType[];
};

type TokenType = "Bearer" | "Basic";
Expand Down Expand Up @@ -140,12 +148,16 @@ async function request<T, A>(
const { data, errors } = (await res.json()) as QueryResponse<T>;

if (errors && opts.errorPolicy !== "all") {
errors.forEach((error, i) => {
const msg = `(${i + 1}/${errors.length}) ${inspect(error)}`;
logger.error(`GraphQL request returned error: ${msg}`);
});
logGraphQLErrors(errors);
throw new Error("GraphQL request retured errors");
}

return { data, errors };
}

export function logGraphQLErrors(errors: GraphQLErrorType[]) {
errors.forEach((error, i) => {
const msg = `(${i + 1}/${errors.length}) ${inspect(error)}`;
logger.error(`GraphQL request returned error: ${msg}`);
});
}
22 changes: 20 additions & 2 deletions lib/soundtrack-api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { runMutation, RunOptions, runQuery } from "./client.js";
import {
logGraphQLErrors,
runMutation,
RunOptions,
runQuery,
} from "./client.js";
import { Semaphore } from "@shopify/semaphore";
import {
Account,
Expand Down Expand Up @@ -185,8 +190,21 @@ export class Api {
const res = await runQuery<AccountZonesQuery, AccountZonesQueryArgs>(
accountZonesQuery,
{ id: accountId, cursor },
await this.runOptions(),
await this.runOptions({ errorPolicy: "all" }),
);
if (res.errors && res.errors.length > 0) {
logGraphQLErrors(res.errors);
const onlyMissingLocationErrors = res.errors.every((error) => {
const code = error.extensions?.code;
const lastPath = error.path?.[error.path.length - 1];
return code === "NOT_FOUND" && lastPath === "location";
});
if (onlyMissingLocationErrors) {
logger.warn("Request returned zones without locations");
} else {
throw new Error("GraphQL request returned errors");
}
}
const zoneFn = toZoneFn(accountId);
const zones: Zone[] = acc.concat(
res.data.account.soundZones.edges.map(({ node }) => zoneFn(node)),
Expand Down
4 changes: 2 additions & 2 deletions lib/soundtrack-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export type Location = {
export type AccountZone = {
id: string;
name: string;
location: Location;
location: Location | null;
};

export type Zone = {
id: string;
name: string;
account: { id: string };
location: Location;
location: Location | null;
};

export type AccountLibrary = {
Expand Down

0 comments on commit 32ddbf6

Please sign in to comment.