nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.36k stars 437 forks source link

Does group middleware works? #121

Closed mits87 closed 6 years ago

mits87 commented 6 years ago

Hi,

I tried use middleware in my queries and mutators like:

type Query @group(middleware: ["jwt.auth", "cors"]) {
  accounts: [Account!]! @paginate(model: "App\\Account")
}

unfortunately it doesn't work. How can I use the middleware in my Queries / Mutators etc. ?

kikoseijo commented 6 years ago

Should post your full schema. could be many things, and when you are working on middleware more,

Just a guess:: you probably missing the extend type Query. This is when you using more than 1 type query definition.

Another guess: make sure it works without graphql, you can always build the middleware in a route and just call the default one something like:

// routes/api.php
/*
|--------------------------------------------------------------------------
| GraphQL Lighthouse Auth Route
|--------------------------------------------------------------------------
|
*/
$controller = config('lighthouse.controller');

Route::group(['middleware' => ['auth:api']], function () use ($controller) {
    Route::post('graphql', ['as' => 'graphql.secured', 'uses' => '\\'.$controller]);
});
mits87 commented 6 years ago

Hi,

thank you for your response. First of all my middlewares works.

My full schema:

auth.graphql

type AuthPayload {
    access_token: String!
}

extend type Mutation @group(namespace: "App\\Http\\GraphQL\\Mutations") {
  authenticate(
    email: String!
    password: String!
  ): AuthPayload @field(resolver: "AuthMutator@login")
}

schema.graphql

#import ./auth.graphql

type Account {
  id: ID!
  identifier: String!
}

type User {
  id: ID!
  email: String!
  account: Account @belongsTo
}

type Query @group(middleware: ["jwt.auth", "cors"]) {
  accounts: [Account!]! @paginate(model: "App\\Account")
}

type Mutation @group(middleware: ["jwt.auth", "cors"], namespace: "App\\Http\\GraphQL\\Mutations") {
  createUser(
    email: String! @validate(rules: ["min:5"])
  ): User @field(resolver: "UserMutator@create")
}

After execute query:

query {
  accounts(count: 1) {
    data {
      id
    }
  }
}

I got:

Error at file:///Applications/GraphQL%20Playground.app/Contents/Resources/app.asar/lib/dist/app.e59169620134e31c7561.js:33636:31 at at process._tickCallback (internal/process/next_tick.js:109:7)

And I would like use middleware in my schema. It will be more readable. I don't want create separate routes for graphql because I would like keep everything with this abstraction in one place.

Any solution?

kikoseijo commented 6 years ago

You error its at javascript level...... need to go deeper.

Why don you try to make the call with a simple rest client? see what server response its, but most of the time, even that, its not very helpful.

chrissm79 commented 6 years ago

Hey @mits87, are you still having issues with the middleware or were you able to get it resolved? Let me know if you have any questions!

mits87 commented 6 years ago

At the moment I left this package. It seems to me that there is too poor documentation and not enough examples of use cases to using it on production. But very interested in project. I hope that in the future I will come back to using this package.

kikoseijo commented 6 years ago

Hi @mits87

you got to consider the following:

When you create a middleware and put it in front of your router, before any action its taken in place the route does not get call in the the case of auth.

You can try it, when you put the middleware in the lighthouse.php config file gives you different results that when you use it in the @group middleware.

When you throw an error inside a try catch we are able to replay back to the graqhql client, as if its only thrown an exception.

Now, when you expect a different behaviour its a matter on where you place things. For example: im using passport, for tokens, so if I put the auth:api in the route I get an exception and client goes crazy. If I put in the group, im able to read the token, but the exception must be thrown by hand anywhere before replaying back.

Hope it makes sense.

From my tests, works, if it does not for you please explain what you expect and why does not work.

BTW: on the errors, you able to track down what middleware been loading since request hits the server.

Probably documentation its poor, but don't you think you getting personalised support? Believe me its best package right now under Laravel, if you find one better let us know,, please.

mits87 commented 6 years ago

Hi @kikoseijo,

thank you for your message. I didn't check how the library works under the hood but if you are able to use the directive like @auth then the middleware directive should also works.

From my tests, works, if it does not for you please explain what you expect and why does not work.

For me the most important is separate the GraphQL logic from the rest of the code. So if I'm using the GraphQL schema then I would like use only GraphQL without extra logic in routes or controllers.

Probably documentation its poor, but don't you think you getting personalised support? Believe me its best package right now under Laravel, if you find one better let us know,, please.

Yes, I know the GraphQL support for Laravel is poor and your library it can be the best library for Laravel.

I will try to do one more approach using your library in my project. Thx

kikoseijo commented 6 years ago

Hi @mits87

check out this thread, we have an update it might resolve this issue.

134

spawnia commented 6 years ago

This should be resolved through #140. I will close this for now, feel free to reopen if the issue persists.