Search code examples
laravel-authenticationlaravel-lighthouse

Laravel lighthouse 4.16 get current user without using @middleware which is deprecated


I have the following mutation

type Mutation {
  # ...

  createArticle(title: String!, content: String!): Article 
    @field(resolver: "ArticleMutator@create")
    
@middleware(checks: ["auth:api"]) // here is the problem related to relation of a user and an article
}

Using the @middleware directive can't be used anymore in Laravel Lighthouse 4.16

So I want to know which method can I use to retrieve the currently authenticated user and create a relation to an article at the same time?

Update full project here https://github.com/wolfiton/laravel-graphql

Also idon't know why I can't get the user.id from the context. I couldn't find anything related to this problem in the docs.

Here are the 2 files that are related to my problem

https://github.com/wolfiton/laravel-graphql/blob/master/graphql/schema.graphql

https://github.com/wolfiton/laravel-graphql/blob/master/app/GraphQL/Mutations/ArticleMutator.php

Error when using Matt Davenport suggestion @guard

type Mutation @guard(with: ["api"]) {
    createArticle(title: String!, content: String!): Article
        @field(resolver: "ArticleMutator")
}
{
  "errors": [
    {
      "debugMessage": "Call to a member function articles() on null",
      "message": "Internal server error",
      "extensions": {
        "category": "internal"
      },
      "locations": [
        {
          "line": 6,
          "column": 3
        }
      ],
      "path": [
        "createArticle"
      ],

Thanks in advance


Solution

  • The @middleware directive has been deprecated in favor of @guard, so I would do something like this:

    type Mutation @guard(with: ["api"]) {
      createArticle(title: String!, content: String!): Article @field(resolver: "ArticleMutator@create")
    }