-1

I tried to setup successfully graphQL with Laravel 8 using Lighthouse 5.3.0

I am having issue when I pass auth token in header with graphiQL

Here is my query schema

     type Query {
          user(id: ID! @eq): User  @guard(with: ["api"]) @find
          users: [User!]!  @paginate(type: "paginator" model: "App\\Models\\User")
          product(id: ID! @eq): Product @find
          all_products: [Product!]! @paginate
          user_products(user_id: Int! @eq): [Product!]! @paginate
        }

and running query below

query fetchUserObj($user_id:ID!){
  user(id:$user_id){
      id
      name
      email
  }
}

{
  "user_id" : 1
}

with bearer token,

{
  "error": {
    "message": "Call to undefined method App\\Models\\User::withAccessToken()",
    "exception": "BadMethodCallException",
    "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Traits\\ForwardsCalls.php",
    "line": 50,
    "trace": [
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Traits\\ForwardsCalls.php",
        "line": 36,
        "function": "throwBadMethodCallException",
        "class": "Illuminate\\Database\\Eloquent\\Model",
        "type": "::"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php",
        "line": 1890,
        "function": "forwardCallTo",
        "class": "Illuminate\\Database\\Eloquent\\Model",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\passport\\src\\Guards\\TokenGuard.php",
        "line": 182,
        "function": "__call",
        "class": "Illuminate\\Database\\Eloquent\\Model",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\passport\\src\\Guards\\TokenGuard.php",
        "line": 110,
        "function": "authenticateViaBearerToken",
        "class": "Laravel\\Passport\\Guards\\TokenGuard",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\passport\\src\\PassportServiceProvider.php",
        "line": 308,
        "function": "user",
        "class": "Laravel\\Passport\\Guards\\TokenGuard",
        "type": "->"
      },
      {
        "function": "Laravel\\Passport\\{closure}",
        "class": "Laravel\\Passport\\PassportServiceProvider",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\RequestGuard.php",
        "line": 58,
        "function": "call_user_func"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\GuardHelpers.php",
        "line": 60,
        "function": "user",
        "class": "Illuminate\\Auth\\RequestGuard",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\nuwave\\lighthouse\\src\\Support\\Http\\Middleware\\AttemptAuthentication.php",
        "line": 45,
        "function": "check",
        "class": "Illuminate\\Auth\\RequestGuard",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\nuwave\\lighthouse\\src\\Support\\Http\\Middleware\\AttemptAuthentication.php",
        "line": 30,
        "function": "attemptAuthentication",
        "class": "Nuwave\\Lighthouse\\Support\\Http\\Middleware\\AttemptAuthentication",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Nuwave\\Lighthouse\\Support\\Http\\Middleware\\AttemptAuthentication",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\nuwave\\lighthouse\\src\\Support\\Http\\Middleware\\AcceptJson.php",
        "line": 27,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Nuwave\\Lighthouse\\Support\\Http\\Middleware\\AcceptJson",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 103,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
        "line": 695,
        "function": "then",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
        "line": 670,
        "function": "runRouteWithinStack",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
        "line": 636,
        "function": "runRoute",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
        "line": 625,
        "function": "dispatchToRoute",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
        "line": 166,
        "function": "dispatch",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 128,
        "function": "Illuminate\\Foundation\\Http\\{closure}",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php",
        "line": 21,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php",
        "line": 21,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php",
        "line": 27,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance.php",
        "line": 86,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\fruitcake\\laravel-cors\\src\\HandleCors.php",
        "line": 57,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Fruitcake\\Cors\\HandleCors",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\fideloper\\proxy\\src\\TrustProxies.php",
        "line": 57,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Fideloper\\Proxy\\TrustProxies",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
        "line": 103,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
        "line": 141,
        "function": "then",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
        "line": 110,
        "function": "sendRequestThroughRouter",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\public\\index.php",
        "line": 52,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
      },
      {
        "file": "E:\\wamp\\www\\laravel-graphql\\server.php",
        "line": 21,
        "function": "require_once"
      }
    ]
  }
}

How can I pass bearer token in request.

Nimantha
  • 6,405
  • 6
  • 28
  • 69

1 Answers1

0

Apply the Laravel\Passport\HasApiTokens trait on your User model as explained in the https://laravel.com/docs/8.x/passport#installation documentation

Daniël de Wit
  • 2,206
  • 1
  • 16
  • 13