Skip to content

Commit bade821

Browse files
CopilotPhantomDave
andcommitted
Add documentation for GraphQL refactoring
Co-authored-by: PhantomDave <34485699+PhantomDave@users.noreply.github.com>
1 parent 9d5759c commit bade821

1 file changed

Lines changed: 142 additions & 0 deletions

File tree

frontend/GRAPHQL_REFACTORING.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Account GraphQL Operations Refactoring
2+
3+
This document explains the refactoring of account-related GraphQL operations to use Apollo Angular's typed GQL service pattern.
4+
5+
## Overview
6+
7+
All account operations have been migrated from using raw GraphQL queries/mutations with Apollo client to using generated, type-safe Apollo Angular GQL services.
8+
9+
## What Changed
10+
11+
### Before
12+
```typescript
13+
// Direct Apollo usage
14+
this.apollo.query({
15+
query: GET_ACCOUNT_BY_EMAIL,
16+
variables: { email }
17+
})
18+
19+
this.apollo.mutate({
20+
mutation: CREATE_ACCOUNT,
21+
variables: { email, password }
22+
})
23+
```
24+
25+
### After
26+
```typescript
27+
// Generated typed GQL services
28+
this.getAccountByEmailGQL.watch({ variables: { email } }).valueChanges
29+
this.createAccountGQL.mutate({ variables: { email, password } })
30+
```
31+
32+
## Generated GQL Classes
33+
34+
The following injectable GQL classes are now available in `src/generated/graphql.ts`:
35+
36+
### Queries
37+
- **GetAccountByEmailGQL** - Fetch account by email
38+
- **GetAccountsGQL** - Fetch all accounts
39+
- **LoginAccountGQL** - Login with email and password
40+
41+
### Mutations
42+
- **CreateAccountGQL** - Create a new account
43+
44+
## Usage
45+
46+
### Using AccountService (Recommended)
47+
The AccountService provides high-level methods that wrap the GQL classes with error handling and state management:
48+
49+
```typescript
50+
import { AccountService } from './models/account/account-service';
51+
52+
export class MyComponent {
53+
private readonly accountService = inject(AccountService);
54+
55+
async login() {
56+
const account = await this.accountService.loginAccount(email, password);
57+
}
58+
59+
async register() {
60+
const result = await this.accountService.createAccount(email, password);
61+
}
62+
}
63+
```
64+
65+
### Using GQL Classes Directly (Advanced)
66+
For more control, you can inject the GQL classes directly:
67+
68+
```typescript
69+
import { LoginAccountGQL, CreateAccountGQL } from './generated/graphql';
70+
import { firstValueFrom } from 'rxjs';
71+
72+
export class MyComponent {
73+
private readonly loginGQL = inject(LoginAccountGQL);
74+
private readonly createAccountGQL = inject(CreateAccountGQL);
75+
76+
async login(email: string, password: string) {
77+
const result = await firstValueFrom(
78+
this.loginGQL.watch({ variables: { email, password } }).valueChanges
79+
);
80+
return result.data?.loginAccount;
81+
}
82+
83+
async register(email: string, password: string) {
84+
const result = await firstValueFrom(
85+
this.createAccountGQL.mutate({ variables: { email, password } })
86+
);
87+
return result.data?.createAccount;
88+
}
89+
}
90+
```
91+
92+
## Adding New Operations
93+
94+
To add a new account operation:
95+
96+
1. Create a `.graphql` file in `src/graphql/`:
97+
```graphql
98+
# src/graphql/update-account.mutation.graphql
99+
mutation UpdateAccount($id: Int!, $email: String!) {
100+
updateAccount(id: $id, email: $email) {
101+
id
102+
email
103+
createdAt
104+
updatedAt
105+
}
106+
}
107+
```
108+
109+
2. Run code generation:
110+
```bash
111+
npm run codegen
112+
```
113+
114+
3. The new `UpdateAccountGQL` class will be generated and can be injected:
115+
```typescript
116+
import { UpdateAccountGQL } from './generated/graphql';
117+
118+
export class AccountService {
119+
private readonly updateAccountGQL = inject(UpdateAccountGQL);
120+
121+
async updateAccount(id: number, email: string) {
122+
const result = await firstValueFrom(
123+
this.updateAccountGQL.mutate({ variables: { id, email } })
124+
);
125+
return result.data?.updateAccount;
126+
}
127+
}
128+
```
129+
130+
## Benefits
131+
132+
1. **Type Safety**: Full TypeScript type checking for all GraphQL operations
133+
2. **Auto-completion**: IDE provides suggestions for available fields and variables
134+
3. **Consistency**: All operations follow the same pattern
135+
4. **Maintainability**: Easier to add, modify, and remove operations
136+
5. **Code Generation**: No manual type definitions needed
137+
138+
## Migration Notes
139+
140+
- Old `account.queries.ts` and `account.mutations.ts` files have been removed
141+
- The `Account` interface was updated to match generated types (`updatedAt?: string | null`)
142+
- All existing components using AccountService continue to work without changes

0 commit comments

Comments
 (0)