Skip to content

Commit 541e164

Browse files
committed
Implement query for myFavorites
1 parent 3eec9c5 commit 541e164

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

app/GraphQL/Queries/MyFavorites.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\GraphQL\Queries;
4+
5+
use Error;
6+
use Illuminate\Support\Facades\Auth;
7+
8+
final class MyFavorites
9+
{
10+
/**
11+
* @param array{} $args
12+
*/
13+
public function __invoke($_, array $args)
14+
{
15+
$quard = Auth::guard("api");
16+
if(!$quard->user()){
17+
throw new Error("Invalid credentials.");
18+
}
19+
$user = $quard->user();
20+
return $user->favorites;
21+
}
22+
}

app/Models/Favorite.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Favorite extends Model
9+
{
10+
use HasFactory;
11+
}

app/Models/User.php

+5
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ public function notifications(): HasMany
5454
{
5555
return $this->hasMany(Notification::class, "user_id");
5656
}
57+
58+
public function favorites(): HasMany
59+
{
60+
return $this->hasMany(Favorite::class, "user_id");
61+
}
5762
}

graphql/types.graphql

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type User {
99
updated_at: String!
1010
posts: [Post]! @hasMany
1111
notifications: [Notification]! @hasMany
12+
favorites: [Favorit] @hasMany
1213
}
1314

1415

@@ -57,3 +58,11 @@ type Comment {
5758
created_at: String
5859
updated_at: String
5960
}
61+
62+
type Favorite {
63+
id: ID!
64+
user: User! @belongsTo
65+
post: Post!
66+
created_at: String
67+
updated_at: String
68+
}

graphql/user.graphql

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extend type Query {
22
users: [User!]! @paginate @guard
33
user(id: ID! @eq): User @find
4+
myFavorites: [Favorite]! @guard
45
}

0 commit comments

Comments
 (0)