1

I am trying to test a configuration for securing graphql subscriptions in my application.

This is my config in the ApolloServer constructor:

  const app = express();

  const jwt_authentication = jwt({
    secret: JWT_SECRET,
    credentialsRequired: false
  })

  const server = new ApolloServer({
    typeDefs,
    resolvers,
    introspection: true,
    playground: true,
    formatError: error => {
      console.log(error);
    },
    context: async ({req, connection }) => {
      if (connection) {
        return connection.context;
      } else {
        return some_method_to_return_user_info;
      }
    },
    subscriptions: {
      onConnect: async (connectionParams, webSocket, context) => {
        const user = await jsonwebtoken.verify(connectionParams.jwt, JWT_SECRET);

        const userInfo= some_method_to_return_user_info;
        if (userInfo) {
           return { user: userInfo };
        }

        throw new Error("Unauthorized subscription");
      }
    }
  });

  app.use(GRAPHQL_PATH, jwt_authentication);
  //...

When I run a subscription in GraphQL Playground I get the error:

jwt must be provided

I tested with the header "Authorization": "Bearer MY_TOKEN" and then with "jwt": "MY_TOKEN", but I believe that it's not as straightforward as that.

Is there any possibility to test my subscriptions without implementing a client code?

Strider
  • 3,539
  • 5
  • 32
  • 60

1 Answers1

1

I got it working in GraphQL Playground by adding the HTTP Header that way:

{
  "jwt": "MY_TOKEN"
}
Strider
  • 3,539
  • 5
  • 32
  • 60