Skip to content

Commit d0f3bb2

Browse files
CopilotPhantomDave
andcommitted
Move LoginAccount from Query to Mutation in backend and frontend
Backend changes: - Moved LoginAccount method from AccountQueries to AccountMutations - Added proper validation and error handling for login credentials Frontend changes: - Updated login-account GraphQL file to use mutation instead of query - Renamed file to login-account.mutation.graphql - Updated schema.graphql to reflect loginAccount as a mutation - Regenerated TypeScript types with codegen - Updated AccountService to use .mutate() instead of .fetch() for login - Updated documentation to reflect login as a mutation Co-authored-by: PhantomDave <34485699+PhantomDave@users.noreply.github.com>
1 parent 7eb9ba3 commit d0f3bb2

6 files changed

Lines changed: 51 additions & 14 deletions

File tree

PhantomDave.BankTracking.Api/Types/Mutations/AccountMutations.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,50 @@ public async Task<AccountType> CreateAccount(
5353

5454
return AccountType.FromAccount(account);
5555
}
56+
57+
/// <summary>
58+
/// Login to an account
59+
/// </summary>
60+
public async Task<AccountType?> LoginAccount(
61+
string email,
62+
string password,
63+
[Service] AccountService accountService)
64+
{
65+
if (string.IsNullOrWhiteSpace(email))
66+
{
67+
throw new GraphQLException(
68+
ErrorBuilder.New()
69+
.SetMessage("Email is required.")
70+
.SetCode("BAD_USER_INPUT")
71+
.SetExtension("field", "email")
72+
.SetExtension("reason", "required")
73+
.Build());
74+
}
75+
76+
if (string.IsNullOrWhiteSpace(password))
77+
{
78+
throw new GraphQLException(
79+
ErrorBuilder.New()
80+
.SetMessage("Password is required.")
81+
.SetCode("BAD_USER_INPUT")
82+
.SetExtension("field", "password")
83+
.SetExtension("reason", "required")
84+
.Build());
85+
}
86+
87+
var account = await accountService.LoginAccountAsync(email, password);
88+
if (account is null)
89+
{
90+
throw new GraphQLException(
91+
ErrorBuilder.New()
92+
.SetMessage("Invalid email or password.")
93+
.SetCode("AUTHENTICATION_FAILED")
94+
.SetExtension("field", "email")
95+
.SetExtension("reason", "invalid_credentials")
96+
.Build());
97+
}
98+
99+
return AccountType.FromAccount(account);
100+
}
56101
}
57102

PhantomDave.BankTracking.Api/Types/Queries/AccountQueries.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,5 @@ public async Task<IEnumerable<AccountType>> GetAccounts(
2929
var account = await accountService.GetAccountByEmail(email);
3030
return account != null ? AccountType.FromAccount(account) : null;
3131
}
32-
33-
public async Task<AccountType?> LoginAccount(
34-
string email,
35-
string password,
36-
[Service] AccountService accountService)
37-
{
38-
var account = await accountService.LoginAccountAsync(email, password);
39-
return account != null ? AccountType.FromAccount(account) : null;
40-
}
4132
}
4233

frontend/GRAPHQL_REFACTORING.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ this.apollo.mutate({
2525
### After
2626
```typescript
2727
// Generated typed GQL services
28-
this.getAccountByEmailGQL.watch({ variables: { email } }).valueChanges
28+
this.getAccountByEmailGQL.fetch({ variables: { email } })
2929
this.createAccountGQL.mutate({ variables: { email, password } })
30+
this.loginAccountGQL.mutate({ variables: { email, password } })
3031
```
3132

3233
## Generated GQL Classes
@@ -36,10 +37,10 @@ The following injectable GQL classes are now available in `src/generated/graphql
3637
### Queries
3738
- **GetAccountByEmailGQL** - Fetch account by email
3839
- **GetAccountsGQL** - Fetch all accounts
39-
- **LoginAccountGQL** - Login with email and password
4040

4141
### Mutations
4242
- **CreateAccountGQL** - Create a new account
43+
- **LoginAccountGQL** - Login with email and password (changed from Query to Mutation for proper authentication semantics)
4344

4445
## Usage
4546

frontend/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ scalar DateTime
2525

2626
type Mutation {
2727
createAccount(email: String!, password: String!): AccountType!
28+
loginAccount(email: String!, password: String!): AccountType
2829
}
2930

3031
type Query {
3132
accountByEmail(email: String!): AccountType
3233
accounts: [AccountType!]!
33-
loginAccount(email: String!, password: String!): AccountType
3434
}

frontend/src/graphql/login-account.query.graphql renamed to frontend/src/graphql/login-account.mutation.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
query LoginAccount($email: String!, $password: String!) {
1+
mutation LoginAccount($email: String!, $password: String!) {
22
loginAccount(email: $email, password: $password) {
33
id
44
email

frontend/src/models/account/account-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class AccountService {
9999

100100
try {
101101
const result = await firstValueFrom(
102-
this.loginAccountGQL.fetch({ variables: { email, password } })
102+
this.loginAccountGQL.mutate({ variables: { email, password } })
103103
);
104104

105105
if (result?.data?.loginAccount) {

0 commit comments

Comments
 (0)