Skip to content

@can directive throws error for basic gate ability #2212

@6XGate

Description

@6XGate

Describe the bug

When running a query or mutation that does not return an Eloquent model with a basic gate ability for the @can directive, it throws a DefinitionException stating Failed to find class ${graphQlType} in namespaces [...${modelNamspaces}] for directive @can..

Expected behavior/Solution

It should not fail without a model, but rather fallback to using any gates defined with Gate::define or similar methods.

Steps to reproduce

  1. Create a mutation or query using a custom resolver. That resolver should not return an Eloquent model.
  2. Define a basic gate in app/Providers/AuthServiceProvider.php using Gate::define. For example, it could be called read.
  3. Add a @can(ability: 'read') directive to the operation in step 1.
  4. Call the operation from a client.

Output/Logs

Click to expand

Entry truncated and tagged for readability.

[${timestamp}] local.ERROR: FFailed to find class ${graphQlType} in namespaces [...${modelNamspaces}] for directive @can. {...${context}, "exception":"[object] (Nuwave\\Lighthouse\\Exceptions\\DefinitionException(code: 0): ${message} at /usr/share/nginx/html/vendor/nuwave/lighthouse/src/Schema/Directives/BaseDirective.php:197)
[stacktrace]
...${stacktrace}

Lighthouse Version

5.61.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions