Skip to content

Commit

Permalink
feat(apps/gql): add Mutation.removePanoPost resolver
Browse files Browse the repository at this point in the history
Closes #582
  • Loading branch information
usirin committed Aug 4, 2023
1 parent 7fbd430 commit c7fa2b2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
9 changes: 7 additions & 2 deletions apps/gql/features/pano/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ function createPanoPostActions({ prisma }: Clients) {
});
};

const update = (userID: string, args: UpdatePanoPostArgs) => {
const update = (id: string, args: UpdatePanoPostArgs) => {
return prisma.post.update({
where: { id: userID },
where: { id },
data: {
title: args.title,
url: args.url,
Expand All @@ -45,8 +45,13 @@ function createPanoPostActions({ prisma }: Clients) {
});
};

const remove = (id: string) => {
return prisma.post.delete({ where: { id } });
};

return {
create,
update,
remove,
};
}
18 changes: 18 additions & 0 deletions apps/gql/schema/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export const resolvers = {

CreatePanoPostPayload: {}, // union
UpdatePanoPostPayload: {}, // union
RemovePanoPostPayload: {}, // union
UserError: {}, // interface

InvalidInput: errorFieldsResolver,
Expand Down Expand Up @@ -271,5 +272,22 @@ export const resolvers = {
const updated = await actions.pano.post.update(id.value, input);
return transformPanoPost(await loaders.pano.post.byID.clear(updated.id).load(updated.id));
},
removePanoPost: async (_, { input }, { actions, loaders, pasaport: { session } }) => {
if (!session?.user?.id) {
return NotAuthorized();
}

const id = parse(input.id);
if (id.type !== "PanoPost") {
return InvalidInput("wrong id");
}

const post = await loaders.pano.post.byID.load(id.value);
if (post.userID !== session.user.id) {
return NotAuthorized();
}

return transformPanoPost(await actions.pano.post.remove(id.value));
},
},
} satisfies Resolvers;
7 changes: 7 additions & 0 deletions apps/gql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ type PanoCommentEdge {
type Mutation {
createPanoPost(input: CreatePanoPostInput!): CreatePanoPostPayload
updatePanoPost(input: UpdatePanoPostInput!): UpdatePanoPostPayload
removePanoPost(input: RemovePanoPostInput!): RemovePanoPostPayload
}

input CreatePanoPostInput {
Expand All @@ -177,3 +178,9 @@ input UpdatePanoPostInput {
}

union UpdatePanoPostPayload = PanoPost | NotAuthorized | InvalidInput

input RemovePanoPostInput {
id: ID!
}

union RemovePanoPostPayload = PanoPost | NotAuthorized | InvalidInput
39 changes: 39 additions & 0 deletions apps/gql/schema/types.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ export type InvalidInput = UserError & {
export type Mutation = {
__typename?: "Mutation";
createPanoPost: Maybe<CreatePanoPostPayload>;
removePanoPost: Maybe<RemovePanoPostPayload>;
updatePanoPost: Maybe<UpdatePanoPostPayload>;
};

export type MutationCreatePanoPostArgs = {
input: CreatePanoPostInput;
};

export type MutationRemovePanoPostArgs = {
input: RemovePanoPostInput;
};

export type MutationUpdatePanoPostArgs = {
input: UpdatePanoPostInput;
};
Expand Down Expand Up @@ -184,6 +189,12 @@ export type QueryUserArgs = {
username: InputMaybe<Scalars["String"]["input"]>;
};

export type RemovePanoPostInput = {
id: Scalars["ID"]["input"];
};

export type RemovePanoPostPayload = InvalidInput | NotAuthorized | PanoPost;

export type SozlukQuery = {
__typename?: "SozlukQuery";
term: Maybe<SozlukTerm>;
Expand Down Expand Up @@ -355,6 +366,10 @@ export type ResolversUnionTypes<RefType extends Record<string, unknown>> = Resol
| (InvalidInput & { __typename: "InvalidInput" })
| (NotAuthorized & { __typename: "NotAuthorized" })
| (PanoPost & { __typename: "PanoPost" });
RemovePanoPostPayload:
| (InvalidInput & { __typename: "InvalidInput" })
| (NotAuthorized & { __typename: "NotAuthorized" })
| (PanoPost & { __typename: "PanoPost" });
UpdatePanoPostPayload:
| (InvalidInput & { __typename: "InvalidInput" })
| (NotAuthorized & { __typename: "NotAuthorized" })
Expand Down Expand Up @@ -400,6 +415,10 @@ export type ResolversTypes = ResolversObject<{
PanoPostFilter: PanoPostFilter;
PanoQuery: ResolverTypeWrapper<PanoQuery>;
Query: ResolverTypeWrapper<{}>;
RemovePanoPostInput: RemovePanoPostInput;
RemovePanoPostPayload: ResolverTypeWrapper<
ResolversUnionTypes<ResolversTypes>["RemovePanoPostPayload"]
>;
SozlukQuery: ResolverTypeWrapper<SozlukQuery>;
SozlukTerm: ResolverTypeWrapper<SozlukTerm>;
SozlukTermBody: ResolverTypeWrapper<SozlukTermBody>;
Expand Down Expand Up @@ -438,6 +457,8 @@ export type ResolversParentTypes = ResolversObject<{
PanoPostEdge: PanoPostEdge;
PanoQuery: PanoQuery;
Query: {};
RemovePanoPostInput: RemovePanoPostInput;
RemovePanoPostPayload: ResolversUnionTypes<ResolversParentTypes>["RemovePanoPostPayload"];
SozlukQuery: SozlukQuery;
SozlukTerm: SozlukTerm;
SozlukTermBody: SozlukTermBody;
Expand Down Expand Up @@ -496,6 +517,12 @@ export type MutationResolvers<
ContextType,
RequireFields<MutationCreatePanoPostArgs, "input">
>;
removePanoPost: Resolver<
Maybe<ResolversTypes["RemovePanoPostPayload"]>,
ParentType,
ContextType,
RequireFields<MutationRemovePanoPostArgs, "input">
>;
updatePanoPost: Resolver<
Maybe<ResolversTypes["UpdatePanoPostPayload"]>,
ParentType,
Expand Down Expand Up @@ -656,6 +683,17 @@ export type QueryResolvers<
viewer: Resolver<Maybe<ResolversTypes["Viewer"]>, ParentType, ContextType>;
}>;

export type RemovePanoPostPayloadResolvers<
ContextType = KampusGQLContext,
ParentType extends ResolversParentTypes["RemovePanoPostPayload"] = ResolversParentTypes["RemovePanoPostPayload"]
> = ResolversObject<{
__resolveType?: TypeResolveFn<
"InvalidInput" | "NotAuthorized" | "PanoPost",
ParentType,
ContextType
>;
}>;

export type SozlukQueryResolvers<
ContextType = KampusGQLContext,
ParentType extends ResolversParentTypes["SozlukQuery"] = ResolversParentTypes["SozlukQuery"]
Expand Down Expand Up @@ -775,6 +813,7 @@ export type Resolvers<ContextType = KampusGQLContext> = ResolversObject<{
PanoPostEdge: PanoPostEdgeResolvers<ContextType>;
PanoQuery: PanoQueryResolvers<ContextType>;
Query: QueryResolvers<ContextType>;
RemovePanoPostPayload: RemovePanoPostPayloadResolvers<ContextType>;
SozlukQuery: SozlukQueryResolvers<ContextType>;
SozlukTerm: SozlukTermResolvers<ContextType>;
SozlukTermBody: SozlukTermBodyResolvers<ContextType>;
Expand Down

0 comments on commit c7fa2b2

Please sign in to comment.