Skip to content

Commit 5915930

Browse files
authored
createBranch + getBranchDetails to follow project conventions (#7403)
1 parent dc88632 commit 5915930

16 files changed

+193
-129
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { gql } from "@apollo/client";
2+
3+
import type {
4+
Branch_CreateMutation,
5+
Branch_CreateMutationVariables,
6+
} from "@/shared/api/graphql/generated/graphql";
7+
import graphqlClient from "@/shared/api/graphql/graphqlClientApollo";
8+
9+
export const BRANCH_CREATE = gql`
10+
mutation BRANCH_CREATE($name: String!, $description: String, $sync_with_git: Boolean) {
11+
BranchCreate(data: { name: $name, description: $description, sync_with_git: $sync_with_git }) {
12+
object {
13+
id
14+
name
15+
description
16+
origin_branch
17+
branched_from
18+
created_at
19+
sync_with_git
20+
is_default
21+
}
22+
}
23+
}
24+
`;
25+
26+
export interface CreateBranchFromApiParams {
27+
name: string;
28+
description?: string | null;
29+
sync_with_git?: boolean;
30+
}
31+
32+
export function createBranchFromApi(params: CreateBranchFromApiParams) {
33+
return graphqlClient.mutate<Branch_CreateMutation, Branch_CreateMutationVariables>({
34+
mutation: BRANCH_CREATE,
35+
variables: params,
36+
});
37+
}

frontend/app/src/entities/branches/api/createBranch.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { gql } from "@apollo/client";
2+
3+
import type {
4+
Get_Branch_DetailsQuery,
5+
Get_Branch_DetailsQueryVariables,
6+
} from "@/shared/api/graphql/generated/graphql";
7+
import graphqlClient from "@/shared/api/graphql/graphqlClientApollo";
8+
import type { BranchContextParams } from "@/shared/api/types";
9+
10+
export const getBranchDetailsQuery = gql`
11+
query GET_BRANCH_DETAILS($branchName: String!) {
12+
Branch(name: $branchName) {
13+
id
14+
name
15+
description
16+
origin_branch
17+
branched_from
18+
created_at
19+
sync_with_git
20+
is_default
21+
}
22+
}
23+
`;
24+
25+
export interface GetBranchDetailsFromApiParams extends BranchContextParams {}
26+
27+
export function getBranchDetailsFromApi({ branchName }: GetBranchDetailsFromApiParams) {
28+
return graphqlClient.query<Get_Branch_DetailsQuery, Get_Branch_DetailsQueryVariables>({
29+
query: getBranchDetailsQuery,
30+
variables: { branchName },
31+
});
32+
}

frontend/app/src/entities/branches/api/getBranchDetails.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const branchesQueryKeys = {
2+
all: ["branches"] as const,
3+
details: ({ branchName }: { branchName: string }) => [...branchesQueryKeys.all, branchName],
4+
} as const;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useMutation } from "@tanstack/react-query";
2+
3+
import { queryClient } from "@/shared/api/rest/client";
4+
5+
import { createBranch } from "@/entities/branches/domain/create-branch";
6+
import { getBranchesQueryOptions } from "@/entities/branches/domain/get-branches.query";
7+
8+
export function useCreateBranchMutation() {
9+
return useMutation({
10+
mutationFn: createBranch,
11+
onSuccess: async (branchCreated) => {
12+
if (!branchCreated) return;
13+
14+
const { queryKey } = getBranchesQueryOptions();
15+
queryClient.setQueryData(queryKey, (oldBranches) => [...(oldBranches ?? []), branchCreated]);
16+
queryClient.invalidateQueries({ queryKey });
17+
},
18+
});
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { Branch } from "@/shared/api/graphql/generated/graphql";
2+
3+
import {
4+
type CreateBranchFromApiParams,
5+
createBranchFromApi,
6+
} from "@/entities/branches/api/create-branch-from-api";
7+
8+
export type CreateBranchParams = CreateBranchFromApiParams;
9+
export type CreateBranch = (params: CreateBranchParams) => Promise<Branch | null>;
10+
11+
export const createBranch: CreateBranch = async (params) => {
12+
const { data, errors } = await createBranchFromApi(params);
13+
14+
if (errors) {
15+
throw new Error(errors.map((e) => e.message).join("; "));
16+
}
17+
18+
return data?.BranchCreate?.object ?? null;
19+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { queryOptions, useQuery } from "@tanstack/react-query";
2+
3+
import { branchesQueryKeys } from "@/entities/branches/domain/branch.query-keys";
4+
import {
5+
type GetBranchDetailsParams,
6+
getBranchDetails,
7+
} from "@/entities/branches/domain/get-branch-details";
8+
9+
export function getBranchDetailsQueryOptions(params: GetBranchDetailsParams) {
10+
return queryOptions({
11+
queryKey: branchesQueryKeys.details(params),
12+
queryFn: () => getBranchDetails(params),
13+
});
14+
}
15+
16+
export function useGetBranchDetails(params: GetBranchDetailsParams) {
17+
return useQuery(getBranchDetailsQueryOptions(params));
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Branch } from "@/shared/api/graphql/generated/graphql";
2+
3+
import {
4+
type GetBranchDetailsFromApiParams,
5+
getBranchDetailsFromApi,
6+
} from "@/entities/branches/api/get-branch-details-from-api";
7+
8+
export type GetBranchDetailsParams = GetBranchDetailsFromApiParams;
9+
10+
export type GetBranchDetails = (params: GetBranchDetailsParams) => Promise<Branch>;
11+
12+
export const getBranchDetails: GetBranchDetails = async (params) => {
13+
const { data, errors } = await getBranchDetailsFromApi(params);
14+
15+
if (errors) {
16+
throw new Error(errors.map((e) => e.message).join("; "));
17+
}
18+
19+
const branch = data?.Branch?.[0];
20+
21+
if (!branch) throw new Error(`Branch ${params.branchName} not found`);
22+
23+
return branch;
24+
};

frontend/app/src/entities/branches/domain/get-branches.query.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { queryOptions, useQuery } from "@tanstack/react-query";
22

3+
import { branchesQueryKeys } from "@/entities/branches/domain/branch.query-keys";
4+
35
import { getBranches } from "./get-branches";
46

57
export function getBranchesQueryOptions() {
68
return queryOptions({
7-
queryKey: ["branches"],
9+
queryKey: branchesQueryKeys.all,
810
queryFn: getBranches,
911
});
1012
}

0 commit comments

Comments
 (0)