Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Null locations #17

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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