Graphql-laravel: Middleware Problem

Created on 2 Sep 2019  路  2Comments  路  Source: rebing/graphql-laravel

Versions:

  • graphql-laravel Version: ^2.0
  • Laravel Version: 5.8.*
  • PHP Version: 7.3.7

Question:

Hi, I have same problem #212, #87 about middleware.
My application accept only api login (so if credentials is correct, the application give jwt token. i have not web login page.)
I want use auth:api middleware in schema, but it doesnt make anything.

Even at least i try use auth middleware because i want see working right middleware and i want be sure the problem is not related about api settings, it doesnt make anything too.

app/Http/Kernel.php
  protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'jwt.auth' => \Tymon\JWTAuth\Http\Middleware\Authenticate::class,
app/GraphQL/Queries/User/UsersQuery.php
<?php

declare(strict_types=1);

namespace App\GraphQL\Queries\User;

use App\Repositories\UserRepository;
use Closure;
use Rebing\GraphQL\Support\Facades\GraphQL;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Rebing\GraphQL\Support\Query;

class UsersQuery extends Query
{
   protected $attributes = [
        'name' => 'Users query'
    ];

   protected $userRepository;

   public function __construct(UserRepository $userRepository)
   {
       $this->userRepository = $userRepository;
   }

    public function type(): Type
    {
        return Type::listOf(GraphQL::type('User'));
    }

    public function args(): array
    {
        return [
            'id' => ['name' => 'id', 'type' => Type::string()],
        ];
    }

    public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closure $getSelectFields)
    {
        $fields = $getSelectFields();
        $select = $fields->getSelect();
        $with = $fields->getRelations();

        return $this->userRepository->with($with)->all($select);
    }
}
config/graphql.php

    'default_schema' => 'default',
    'schemas' => [
        'default' => [
            'query' => [
                'user' => \App\GraphQL\Queries\User\UserQuery::class,
                'users' => \App\GraphQL\Queries\User\UsersQuery::class,
                'unit' => UnitQuery::class,

                'queries_executed' => \Uroshcs\GraphQLQueryDebugger\GraphQL\QueriesExecutedQuery::class
            ],
            'mutation' => [
                'CreateRole' => CreateRoleMutation::class
            ],
            'middleware' => ['auth'],
            'method' => ['get', 'post'],
        ]
    ],

GET 127.0.0.1:8000/graphql (without Bearer authorization token)

query{
  users {
    name
  }
}

Response: 200 Success

"data": {
    "users": [
      {
        "name": "Foo"
      },
      {
        "name": "Bar"
      },
...

Where i am wrong?

Thank you.

question

All 2 comments

   public function __construct(UserRepository $userRepository)

Unrelated to your question, but I advise not to do this: when GraphQL has to build the type system, it has to "make" _all_ queries (and types).

At this point, it's not even guaranteed your resolve will be called, so this is waste of CPU cycles.

My advise: whilst not _elegant_, just resolve them within resolve.

I'm thinking of improving this in the feature.

Sorry, nothing related to your middleware question though.

   public function __construct(UserRepository $userRepository)

Unrelated to your question, but I advise not to do this: when GraphQL has to build the type system, it has to "make" _all_ queries (and types).

At this point, it's not even guaranteed your resolve will be called, so this is waste of CPU cycles.

My advise: whilst not _elegant_, just resolve them within resolve.

I'm thinking of improving this in the feature.

Sorry, nothing related to your middleware question though.

Thank you for your advise, i'm removing __constructmethod on my queries.

As for the solving my problem, it's really annoying.
We set queries on the config file, i think php artisan config:clear can help to me.
No. The route definitions(graphql queries) are not relating config cache 馃槃

The problem solved with php artisan route:cache

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevelacey picture stevelacey  路  8Comments

constantinosergiou picture constantinosergiou  路  3Comments

timothyvictor picture timothyvictor  路  3Comments

edgarsn picture edgarsn  路  4Comments

hemratna picture hemratna  路  3Comments