Skip to content

Commit 3f614bb

Browse files
committed
Queries
1 parent 5506221 commit 3f614bb

13 files changed

+91
-16
lines changed

app/GraphQL/Mutations/DeleteAcount.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
final class DeleteAcount
1010
{
1111
/**
12-
* @param null $_
12+
*
1313
* @param array{} $args
1414
*/
1515
public function __invoke($_, array $args)
@@ -19,7 +19,7 @@ public function __invoke($_, array $args)
1919

2020
if(!($user || Hash::check($args["password"], $user->password))){
2121
throw ValidationException::withMessages([
22-
"email" => ["the provided credentials are incorrect."]
22+
"message" => ["the provided credentials are incorrect."]
2323
]);
2424
}
2525
$user->delete();

app/GraphQL/Mutations/Login.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
final class Login
1010
{
1111
/**
12-
* @param null $_
12+
*
1313
* @param array{} $args
1414
*/
1515
public function __invoke($_, array $args)
@@ -20,7 +20,7 @@ public function __invoke($_, array $args)
2020
if(! ($user || Hash::check($args["password"], $user->password)))
2121
{
2222
throw ValidationException::withMessages([
23-
"email" => ["the provided credentials are incorrect."]
23+
"message" => ["the provided credentials are incorrect."]
2424
]);
2525
}
2626
return $user->createToken($user->username)->plainTextToken;

app/GraphQL/Queries/CountOfPosts.php

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

app/GraphQL/Queries/MyPosts.php

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

3-
namespace App\GraphQL\Mutations;
3+
namespace App\GraphQL\Queries;
44

55
use App\Models\Post;
66

77
final class PopularPosts
88
{
99
/**
10-
* @param null $_
10+
*
1111
* @param array{} $args
1212
*/
1313
public function __invoke($_, array $args)
1414
{
1515
// TODO implement the resolver
16-
$posts = Post::where("views", ">", 5000)->orWhere("likes", ">", 50)->all();
16+
$posts = Post::where("views", ">", 5000)->orWhere("likes", ">", 50)->get();
1717
return $posts;
1818
}
1919
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<?php
22

3-
namespace App\GraphQL\Mutations;
3+
namespace App\GraphQL\Queries;
44

55
use App\Models\Post;
66

77
final class Search
88
{
99
/**
10-
* @param null $_
10+
*
1111
* @param array{} $args
1212
*/
1313
public function __invoke($_, array $args)
1414
{
1515
// TODO implement the resolver
16-
$posts = Post::where("content", "like", "%" . $args["key"] . "%")->orWhere("title", "like", "%" . $args["key"] . "%")->all();
16+
$posts = Post::where("content", "like", "%" . $args["key"] . "%")->orWhere("title", "like", "%" . $args["key"] . "%")->get();
1717
return $posts;
1818
}
1919
}

app/Http/Kernel.php

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Kernel extends HttpKernel
4242
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
4343
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
4444
\Illuminate\Routing\Middleware\SubstituteBindings::class,
45+
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
4546
],
4647
];
4748

@@ -53,6 +54,7 @@ class Kernel extends HttpKernel
5354
* @var array<string, class-string|string>
5455
*/
5556
protected $middlewareAliases = [
57+
"sanctum" => \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
5658
'auth' => \App\Http\Middleware\Authenticate::class,
5759
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
5860
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,

config/auth.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
'defaults' => [
17-
'guard' => 'web',
17+
'guard' => 'api',
1818
'passwords' => 'users',
1919
],
2020

config/cors.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
|
1616
*/
1717

18-
'paths' => ['api/*', 'sanctum/csrf-cookie'],
18+
'paths' => ['api/*', 'graphql', 'sanctum/csrf-cookie'],
1919

2020
'allowed_methods' => ['*'],
2121

config/lighthouse.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
|
6262
*/
6363

64-
'guards' => ["api"],
64+
'guards' => ["sanctum", "api"],
6565

6666
/*
6767
|--------------------------------------------------------------------------

graphql/notification.graphql

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extend type Query {
2+
myNotifications: [Notification]! @guard
3+
}

graphql/post.graphql

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
extend type Query {
22
posts: [Post!]! @all
33
post(id: ID! @eq): Post @find
4-
search(key: String!): [Post]! @all
5-
popularPosts: [Post]! @all
6-
# myPosts: [Post]! @guard
4+
search(key: String!): [Post]!
5+
popularPosts: [Post]!
6+
myPosts: [Post]! @guard
77
postsByLike(likes: Int! @eq): [Post] @find
8+
countOfPosts: Int!
89
}

0 commit comments

Comments
 (0)