hasura / graphql-engine

Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
https://hasura.io
Apache License 2.0
31.16k stars 2.76k forks source link

Error: server returned results with length 1, expected length of 2 with Batched Network Queries #5227

Open justinr1234 opened 4 years ago

justinr1234 commented 4 years ago

Very similar to this issue: https://github.com/apollographql/apollo-server/issues/1193#issue-333617316

I am seeing an error when the JWT token is expired, hasura is returning a 200 OK status despite having errors. This happens when there are batched queries going on. For single queries, Hasura returns a proper error. I believe Hasura should be returning an error response for each of the batched queries.

I would expect hasura should return a 401 status code.

In the non-batch case, it is only returning a 400 code: https://github.com/hasura/graphql-engine/blob/4c9362c91b7d5b302e37498d6d5d4971caa630a4/server/src-lib/Hasura/Server/Auth/JWT.hs#L356

For the batch case, I'm not sure.

As a workaround, I have implemented an apollo link that looks like this:

const jwtExpirationMessage = 'Could not verify JWT: JWTExpired';

const logoutLink = onError(({ graphQLErrors, networkError }) => {
  if (networkError?.result?.[0]?.errors?.[0]?.message === jwtExpirationMessage || networkError?.statusCode === 401 || graphQLErrors?.find(({ message }) => message === jwtExpirationMessage)) {
    handleLogout();
  }
});

Relevant error stack:

 Error: server returned results with length 1, expected length of 2
     at Object.next (webpack-internal:///./node_modules/apollo-link-batch/lib/batching.js:98:33)
     at notifySubscription (webpack-internal:///./node_modules/zen-observable/lib/Observable.js:126:18)
     at onNotify (webpack-internal:///./node_modules/zen-observable/lib/Observable.js:161:3)
     at SubscriptionObserver.next (webpack-internal:///./node_modules/zen-observable/lib/Observable.js:215:7)
     at eval (webpack-internal:///./node_modules/apollo-link-batch-http/lib/batchHttpLink.js:97:30)
1fexd commented 2 years ago

Hi, I am having a similar issue, I'm using apollo + okhttp on android and am trying to refresh my jwt access token when Hasura's Graphql endpoint returns JWTExpired, but I can't do that since it uses the status code 200 instead of 401.

rlebranchu commented 3 months ago

Has anyone found a solution? I'm using Ktor and okhttpclient having defined the refreshToken behavior (triggered on return of a 401 response status). But hasura returns status 200 for the JWT Expired error. I'm interested in a solution, even if it's a patch.