nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.36k stars 437 forks source link

@can don't work with simple gate #1501

Open MrJmpl3 opened 4 years ago

MrJmpl3 commented 4 years ago

Describe the bug

Don't work with simple permission

Expected behavior/Solution

I think that is because the directive execute $gate->check($permission, [$model, $others]), that make error because the directive try to find the $model in the Schema to get the type of Model Class.

I only need $gate->check($permission) without params.

Maybe related: https://github.com/nuwave/lighthouse/issues/1293

Steps to reproduce

Query:

{
  departments
}

Schema:

extend type Query {
    departments: [String!]! @can(ability: "list ubigeo")
}

Output/Logs

{
  "errors": [
    {
      "debugMessage": "Type 'String' on 'departments' can not be found in the schema.'",
      "message": "Internal server error",
      "extensions": {
        "category": "schema"
      },
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "departments"
      ],
      "trace": [
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/nuwave/lighthouse/src/Schema/Directives/CanDirective.php",
          "line": 159,
          "call": "Nuwave\\Lighthouse\\Schema\\Directives\\BaseDirective::getModelClass()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/nuwave/lighthouse/src/Schema/Directives/CanDirective.php",
          "line": 94,
          "call": "Nuwave\\Lighthouse\\Schema\\Directives\\CanDirective::modelsToCheck()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/nuwave/lighthouse/src/Schema/Factories/FieldFactory.php",
          "line": 166,
          "call": "Nuwave\\Lighthouse\\Schema\\Directives\\CanDirective::Nuwave\\Lighthouse\\Schema\\Directives\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 632,
          "call": "Nuwave\\Lighthouse\\Schema\\Factories\\FieldFactory::Nuwave\\Lighthouse\\Schema\\Factories\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 555,
          "call": "GraphQL\\Executor\\ReferenceExecutor::resolveOrError()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 1247,
          "call": "GraphQL\\Executor\\ReferenceExecutor::resolveField()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 257,
          "call": "GraphQL\\Executor\\ReferenceExecutor::executeFields()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 208,
          "call": "GraphQL\\Executor\\ReferenceExecutor::executeOperation()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/webonyx/graphql-php/src/Executor/Executor.php",
          "line": 155,
          "call": "GraphQL\\Executor\\ReferenceExecutor::doExecute()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/webonyx/graphql-php/src/GraphQL.php",
          "line": 158,
          "call": "GraphQL\\Executor\\Executor::promiseToExecute()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/webonyx/graphql-php/src/GraphQL.php",
          "line": 90,
          "call": "GraphQL\\GraphQL::promiseToExecute()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/nuwave/lighthouse/src/GraphQL.php",
          "line": 156,
          "call": "GraphQL\\GraphQL::executeQuery()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/nuwave/lighthouse/src/GraphQL.php",
          "line": 99,
          "call": "Nuwave\\Lighthouse\\GraphQL::executeQuery()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/nuwave/lighthouse/src/Support/Http/Controllers/GraphQLController.php",
          "line": 59,
          "call": "Nuwave\\Lighthouse\\GraphQL::executeRequest()"
        },
        {
          "call": "Nuwave\\Lighthouse\\Support\\Http\\Controllers\\GraphQLController::query()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
          "line": 54,
          "function": "call_user_func_array()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
          "line": 45,
          "call": "Illuminate\\Routing\\Controller::callAction()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
          "line": 239,
          "call": "Illuminate\\Routing\\ControllerDispatcher::dispatch()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
          "line": 196,
          "call": "Illuminate\\Routing\\Route::runController()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
          "line": 685,
          "call": "Illuminate\\Routing\\Route::run()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 128,
          "call": "Illuminate\\Routing\\Router::Illuminate\\Routing\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/nuwave/lighthouse/src/Support/Http/Middleware/AttemptAuthentication.php",
          "line": 32,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Nuwave\\Lighthouse\\Support\\Http\\Middleware\\AttemptAuthentication::handle()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/nuwave/lighthouse/src/Support/Http/Middleware/AcceptJson.php",
          "line": 29,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Nuwave\\Lighthouse\\Support\\Http\\Middleware\\AcceptJson::handle()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 103,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
          "line": 687,
          "call": "Illuminate\\Pipeline\\Pipeline::then()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
          "line": 662,
          "call": "Illuminate\\Routing\\Router::runRouteWithinStack()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
          "line": 628,
          "call": "Illuminate\\Routing\\Router::runRoute()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
          "line": 617,
          "call": "Illuminate\\Routing\\Router::dispatchToRoute()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
          "line": 165,
          "call": "Illuminate\\Routing\\Router::dispatch()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 128,
          "call": "Illuminate\\Foundation\\Http\\Kernel::Illuminate\\Foundation\\Http\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/app/Http/Middleware/Localization.php",
          "line": 27,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "App\\Http\\Middleware\\Localization::handle()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
          "line": 21,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest::handle()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
          "line": 27,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize::handle()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
          "line": 63,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode::handle()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/fruitcake/laravel-cors/src/HandleCors.php",
          "line": 57,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Fruitcake\\Cors\\HandleCors::handle()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/fideloper/proxy/src/TrustProxies.php",
          "line": 57,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Fideloper\\Proxy\\TrustProxies::handle()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 103,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
          "line": 140,
          "call": "Illuminate\\Pipeline\\Pipeline::then()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
          "line": 109,
          "call": "Illuminate\\Foundation\\Http\\Kernel::sendRequestThroughRouter()"
        },
        {
          "file": "/home/vagrant/ocasani-laravel-api-recave-see-cliente/public/index.php",
          "line": 53,
          "call": "Illuminate\\Foundation\\Http\\Kernel::handle()"
        }
      ]
    }
  ]
}

Lighthouse Version

v4.16.0

spawnia commented 4 years ago

@can expects a model to check the ability against. Since you do not provide one explictly, Lighthouse guesses that your model is called String and looks for such a class.

MrJmpl3 commented 4 years ago

@can expects a model to check the ability against. Since you do not provide one explictly, Lighthouse guesses that your model is called String and looks for such a class.

In other words, @can, isn't a laravel can middleware, is more like a policies. I think you should rename @can to @policy, and make another @gate and this work with simple gates