Skip to content

Commit

Permalink
Prevent errors from listmonk from crashing the app
Browse files Browse the repository at this point in the history
  • Loading branch information
knpwrs committed Dec 20, 2024
1 parent acf64b8 commit 83c2164
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 9 deletions.
14 changes: 13 additions & 1 deletion apps/web-next/src/__generated__/graphql-types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions apps/web-next/src/routes/(root)/(watch).tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ const getHomepageData = query(async function () {
${UploadCardFields}
query HomepageData($loggedIn: Boolean!) {
newsletterListIds
newsletterListIds {
... on QueryNewsletterListIdsSuccess {
__typename
data
}
... on Error {
__typename
message
}
}
subscriptionUploads: mySubscriptionUploadRecords(first: 8)
@include(if: $loggedIn) {
pageInfo {
Expand Down Expand Up @@ -80,6 +89,16 @@ export default function WatchRoute() {
const data = createAsync(() => getHomepageData());
const user = useUser();

const newsletterListIds = () => {
const res = data()?.newsletterListIds;

if (res?.__typename === 'QueryNewsletterListIdsSuccess') {
return res.data;
}

return [];
};

return (
<>
<Og
Expand Down Expand Up @@ -116,7 +135,7 @@ export default function WatchRoute() {
<UploadGrid edges={data()?.trendingUploads?.edges ?? []} />
<SeeMoreLink to="trending" />
<Show when={!user()?.subscribedToNewsletter}>
<Newsletter listIds={data()?.newsletterListIds ?? []} />
<Newsletter listIds={newsletterListIds()} />
</Show>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion apps/web-next/src/routes/(root)/__generated__/(watch).d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion apps/web/src/__generated__/graphql-types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions apps/web/src/routes/(root)/(watch).tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ export function routeData() {
${UploadCardFields}
query HomepageData($loggedIn: Boolean!) {
newsletterListIds
newsletterListIds {
... on QueryNewsletterListIdsSuccess {
__typename
data
}
... on Error {
__typename
message
}
}
subscriptionUploads: mySubscriptionUploadRecords(first: 5)
@include(if: $loggedIn) {
pageInfo {
Expand Down Expand Up @@ -84,6 +93,16 @@ export default function WatchRoute() {
const data = useRouteData<typeof routeData>();
const user = useUser();

const newsletterListIds = () => {
const res = data()?.newsletterListIds;

if (res?.__typename === 'QueryNewsletterListIdsSuccess') {
return res.data;
}

return [];
};

return (
<>
<Og
Expand Down Expand Up @@ -120,7 +139,7 @@ export default function WatchRoute() {
<UploadGrid edges={data()?.trendingUploads?.edges ?? []} />
<SeeMoreLink to="trending" />
<Show when={!user()?.subscribedToNewsletter}>
<Newsletter listIds={data()?.newsletterListIds ?? []} />
<Newsletter listIds={newsletterListIds()} />
</Show>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/routes/(root)/__generated__/(watch).d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified services/gateway/bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions services/gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"subtitle": "^4.2.1",
"tiny-invariant": "^1.3.3",
"uuid": "^11.0.3",
"valibot": "^1.0.0-beta.9",
"wait-on": "^8.0.1",
"xss": "^1.0.15",
"zod": "^3.24.1"
Expand Down
19 changes: 18 additions & 1 deletion services/gateway/src/schema/types/query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ExpiryMap from 'expiry-map';
import pMem from 'p-memoize';
import envariant from '@knpwrs/envariant';
import * as v from 'valibot';
import prisma from '../../util/prisma';
import builder from '../builder';

Expand Down Expand Up @@ -58,14 +59,30 @@ builder.queryField('stats', (t) =>
}),
);

const ListmonkResultSchema = v.object({
data: v.object({
results: v.array(v.object({ uuid: v.pipe(v.string(), v.uuid()) })),
}),
});

builder.objectType(Error, {
name: 'Error',
fields: (t) => ({
message: t.exposeString('message'),
}),
});

builder.queryField('newsletterListIds', (t) =>
t.field({
type: ['String'],
errors: {
types: [Error],
},
resolve: async () => {
const res = await fetch(
envariant('LISTMONK_INTERNAL_URL') + '/api/lists?tag=default',
);
const json = await res.json();
const json = v.parse(ListmonkResultSchema, await res.json());
return json.data.results.map(
(l: { uuid: string }) => l.uuid,
) as Array<string>;
Expand Down

0 comments on commit 83c2164

Please sign in to comment.