-1

我尝试使用 Lighthouse 5.3.0 使用 Laravel 8 成功设置 graphQL

当我使用 graphiQL 在标头中传递身份验证令牌时遇到问题

这是我的查询架构

     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
        }

并在下面运行查询

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

{
  "user_id" : 1
}

使用不记名令牌,

{
  "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"
      }
    ]
  }
}

如何在请求中传递不记名令牌。

4

1 回答 1

0

如https://laravel.com/docs/8.x/passport#installation文档中所述,将Laravel\Passport\HasApiTokens特征应用于您的User模型

于 2021-03-29T06:40:04.183 回答