Can we group queries/resolvers in Graphql and then add Middleware?

Viewed 131

I want to ask about graphql.

  1. In graphql, can we group query/resolver and then give middleware like laravel? If it's Laravel, we can group routes and then the group can be given middleware like this.
Route::middleware(['auth'])->group(function () {
    Route::get('/', function () { ... });
    Route::get('/blog', function () { ... });
});
  1. Is providing one-by-one authentication in query/resolver the best way? Or is there a better way than providing authentication one by one? I want it so that there is no code duplication so the code is clean. I was thinking about making a function and then adding it to each query/resolver, but I'm not sure if this is the best way or not.
const resolvers = {
  Query: {
    blog: (_, { id, slug }) => {
      if (!user) {
        throw new Error("Unauthenticated!")
      }
      // return ...
    },
    blogs: (_, args, { user }) => {
      if (!user) {
        throw new Error("Unauthenticated!")
      }
      // return ...
    }

  }
}

Sorry if there are wrong words, because I am also still new to Graphql and not very good at English.

0 Answers
Related