Skip to content

Throwing an error from a plugin result in Unexpected response #2984

@j0k3r

Description

@j0k3r

Describe the bug

When a plugin is throwing a GraphQLError, the result is Unexpected response.

I created a custom plugin which throw a GraphQLError in case of a problem (to make it short) and when that error happen, the server respond Unexpected response refering to an Internal Server Error.

I was thinking of a problem on my side then I tested using the useJWT plugin from Yoga (using the Hello world as base template) and I got the same problem.

I sent a request to the server using these headers (which obvisouly throw an error):

{
  "authorization": "oups"
}

The response:

{
  "errors": [
    {
      "message": "Unexpected response: \"GraphQLError: Unsupported token type provided: \\\"oups\\\"\\n    at createGraphQLError (/private/tmp/jwt/examples/hello-world/node_modules/@graphql-tools/utils/cjs/errors.js:9:12)\\n    at unauthorizedError (/private/tmp/jwt/examples/hello-world/node_modules/@graphql-yoga/plugin-jwt/cjs/index.js:54:50)\\n    at defaultGetToken (/private/tmp/jwt/examples/hello-world/node_modules/@graphql-yoga/plugin-jwt/cjs/index.js:97:15)\\n    at onRequest (/private/tmp/jwt/examples/hello-world/node_modules/@graphql-yoga/plugin-jwt/cjs/index.js:29:27)\\n    at /private/tmp/jwt/examples/hello-world/node_modules/@whatwg-node/server/cjs/createServerAdapter.js:67:125\\n    at iterate (/private/tmp/jwt/examples/hello-world/node_modules/@whatwg-node/server/cjs/utils.js:306:25)\\n    at iterate (/private/tmp/jwt/examples/hello-world/node_modules/@whatwg-node/server/cjs/utils.js:318:16)\\n    at /private/tmp/jwt/examples/hello-world/node_modules/@whatwg-node/server/cjs/utils.js:312:24\"",
      "extensions": {
        "requestBody": {
          "query": "{\n  hello\n}"
        },
        "responseDetails": {
          "status": 500,
          "statusText": "Internal Server Error"
        }
      }
    }
  ]
}

Your Example Website or App

https://codesandbox.io/p/sandbox/magical-nobel-5283sf

Steps to Reproduce the Bug or Issue

  1. The app should run on codesandbox
  2. Open GraphiQL
  3. Select Header
  4. Define that header:
    {
      "authorization": "oups"
    }
  5. Run the default query
  6. The app will crash

Expected behavior

The user should get a 401 response from the server as per the GraphQLError status code: https://github.com/dotansimha/graphql-yoga/blob/d143af175f2c816fdd094c9f76fde6c75a2f5872/packages/plugins/jwt/src/index.ts#L117

Screenshots or Videos

No response

Platform

  • OS: macOS
  • NodeJS: v18.16.0
  • @graphql-yoga/*:
    "@graphql-yoga/plugin-jwt": "^1.0.1",
    "@graphql-yoga/render-graphiql": "4.0.4",
    "graphql": "16.6.0",
    "graphql-yoga": "4.0.4"

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions