Skip to content

Commit d46e5a4

Browse files
committed
Apply errors-plugin to CreateNote mutation
1 parent de33f93 commit d46e5a4

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

graphql/post.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { unreachable } from "@std/assert";
99
import { assertNever } from "@std/assert/unstable-never";
1010
import { Account } from "./account.ts";
1111
import { Actor } from "./actor.ts";
12-
import { builder, Node } from "./builder.ts";
12+
import { builder, Node, type ValuesOfEnumType } from "./builder.ts";
1313
import { Reactable } from "./reactable.ts";
1414

1515
const PostVisibility = builder.enumType("PostVisibility", {
@@ -22,6 +22,33 @@ const PostVisibility = builder.enumType("PostVisibility", {
2222
] as const,
2323
});
2424

25+
const CreateNoteErrorKind = builder.enumType("CreateNoteErrorKind", {
26+
values: [
27+
"NOT_AUTHENTICATED",
28+
"REPLY_TARGET_NOT_FOUND",
29+
"QUOTED_POST_NOT_FOUND",
30+
"NOTE_CREATION_FAILED",
31+
] as const,
32+
});
33+
34+
class CreateNoteError extends Error {
35+
public constructor(
36+
public readonly kind: ValuesOfEnumType<typeof CreateNoteErrorKind>,
37+
) {
38+
super(`Create note error - ${kind}`);
39+
}
40+
}
41+
42+
builder.objectType(CreateNoteError, {
43+
name: "CreateNoteError",
44+
fields: (t) => ({
45+
createNoteErrorKind: t.field({
46+
type: CreateNoteErrorKind,
47+
resolve: (error) => error.kind,
48+
}),
49+
}),
50+
});
51+
2552
export const Post = builder.drizzleInterface("postTable", {
2653
variant: "Post",
2754
interfaces: [Reactable, Node],
@@ -458,10 +485,16 @@ builder.relayMutationField(
458485
}),
459486
},
460487
{
488+
errors: {
489+
types: [CreateNoteError],
490+
result: {
491+
name: "CreateNoteSuccess",
492+
},
493+
},
461494
async resolve(_root, args, ctx) {
462495
const session = await ctx.session;
463496
if (session == null) {
464-
throw new Error("Not authenticated.");
497+
throw new CreateNoteError("NOT_AUTHENTICATED");
465498
}
466499
const { visibility, content, language, replyTargetId, quotedPostId } =
467500
args.input;
@@ -472,7 +505,7 @@ builder.relayMutationField(
472505
where: { id: replyTargetId.id },
473506
});
474507
if (replyTarget == null) {
475-
throw new Error("Reply target not found.");
508+
throw new CreateNoteError("REPLY_TARGET_NOT_FOUND");
476509
}
477510
}
478511
let quotedPost: schema.Post & { actor: schema.Actor } | undefined;
@@ -482,7 +515,7 @@ builder.relayMutationField(
482515
where: { id: quotedPostId.id },
483516
});
484517
if (quotedPost == null) {
485-
throw new Error("Quoted post not found.");
518+
throw new CreateNoteError("QUOTED_POST_NOT_FOUND");
486519
}
487520
}
488521
return await withTransaction(ctx.fedCtx, async (context) => {
@@ -511,7 +544,7 @@ builder.relayMutationField(
511544
{ replyTarget, quotedPost },
512545
);
513546
if (note == null) {
514-
throw new Error("Failed to create note.");
547+
throw new CreateNoteError("NOTE_CREATION_FAILED");
515548
}
516549
return note;
517550
});

graphql/schema.graphql

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ type ArticleContent implements Node {
270270
url: URL!
271271
}
272272

273+
type CreateNoteError {
274+
createNoteErrorKind: CreateNoteErrorKind!
275+
}
276+
277+
enum CreateNoteErrorKind {
278+
NOTE_CREATION_FAILED
279+
NOT_AUTHENTICATED
280+
QUOTED_POST_NOT_FOUND
281+
REPLY_TARGET_NOT_FOUND
282+
}
283+
273284
input CreateNoteInput {
274285
clientMutationId: ID
275286
content: Markdown!
@@ -284,6 +295,10 @@ type CreateNotePayload {
284295
note: Note!
285296
}
286297

298+
type CreateNoteSuccess {
299+
data: CreateNotePayload!
300+
}
301+
287302
type CustomEmoji implements Node {
288303
id: ID!
289304
imageUrl: String!
@@ -421,7 +436,7 @@ type Mutation {
421436
"""The signup token."""
422437
token: UUID!
423438
): SignupResult!
424-
createNote(input: CreateNoteInput!): CreateNotePayload!
439+
createNote(input: CreateNoteInput!): MutationCreateNoteResult!
425440
loginByEmail(
426441
"""The email of the account to sign in."""
427442
email: String!
@@ -455,6 +470,8 @@ type Mutation {
455470
updateAccount(input: UpdateAccountInput!): UpdateAccountPayload!
456471
}
457472

473+
union MutationCreateNoteResult = CreateNoteError | CreateNoteSuccess
474+
458475
union MutationLoginByUsernameResult = LoginError | LoginSuccess
459476

460477
interface Node {

0 commit comments

Comments
 (0)