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 9e9b160 commit 065c7d4
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 45 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
72 changes: 36 additions & 36 deletions services/gateway/src/schema/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,40 +68,40 @@ export default new SchemaBuilder<{
nullable: false,
},
},
tracing: {
default:
process.env['NODE_ENV'] === 'development'
? true
: (config) => isRootField(config),
wrap: (resolver, _options, config) => (source, args, context, info) =>
runFunction(
() => resolver(source, args, context, info),
(error, duration) => {
const bindings = {
graphql: {
kind: config.kind,
parentType: config.parentType,
args: JSON.stringify(
Object.fromEntries(
Object.entries(args).map(([key, value]) => [
key,
key === 'password' ? '********' : value,
]),
),
),
},
duration,
};

if (error) {
moduleLogger.error(
bindings,
error instanceof Error ? error.message : `${error}`,
);
} else {
moduleLogger.info(bindings);
}
},
),
},
// tracing: {
// default:
// process.env['NODE_ENV'] === 'development'
// ? true
// : (config) => isRootField(config),
// wrap: (resolver, _options, config) => (source, args, context, info) =>
// runFunction(
// () => resolver(source, args, context, info),
// (error, duration) => {
// const bindings = {
// graphql: {
// kind: config.kind,
// parentType: config.parentType,
// args: JSON.stringify(
// Object.fromEntries(
// Object.entries(args).map(([key, value]) => [
// key,
// key === 'password' ? '********' : value,
// ]),
// ),
// ),
// },
// duration,
// };
//
// if (error) {
// moduleLogger.error(
// bindings,
// error instanceof Error ? error.message : `${error}`,
// );
// } else {
// moduleLogger.info(bindings);
// }
// },
// ),
// },
});
20 changes: 19 additions & 1 deletion services/gateway/src/schema/types/query.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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';
import logger from '../../util/logger';

builder.queryType();

Expand Down Expand Up @@ -58,14 +60,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 065c7d4

Please sign in to comment.