accounts-js / accounts

Fullstack authentication and accounts-management for Javascript.
https://www.accountsjs.com/
MIT License
1.5k stars 141 forks source link

Using context: accountsGraphQL.context breaks my subscriptions. #1102

Closed tintin10q closed 3 years ago

tintin10q commented 3 years ago

Bug report

When I put context: accountsGraphQL.context in new ApolloServer() my subscriptions stop working. When I try to run a subscription I get the following error message in the console

(node:18608) UnhandledPromiseRejectionWarning: TypeError: Cannot use 'in' operator to search for 'session' in undefined
    at normalizeSession (C:\....\node_modules\@graphql-modules\core\dist\index.cjs.js:180:19)
    at GraphQLModule._cache.contextBuilder (C:\....\node_modules\@graphql-modules\core\dist\index.cjs.js:1334:31)
    at C:\....\node_modules\@graphql-modules\core\dist\index.cjs.js:1342:112
    at Array.map (<anonymous>)
    at C:\Projects\t....\node_modules\@graphql-modules\core\dist\index.cjs.js:1342:91
    at new Promise (<anonymous>)
    at ApolloServer._cache.contextBuilder [as context] (C:\....\node_modules\@graphql-modules\core\dist\index.cjs.js:1336:63)
    at ApolloServer.<anonymous> (C:....\node_modules\apollo-server-core\dist\ApolloServer.js:365:42)
    at Generator.next (<anonymous>)
    at C:\...\node_modules\apollo-server-core\dist\ApolloServer.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\....\node_modules\apollo-server-core\dist\ApolloServer.js:4:12)
    at SubscriptionServer.onOperation (C:\....\node_modules\apollo-server-core\dist\ApolloServer.js:354:51)
    at C:\....\node_modules\subscriptions-transport-ws\dist\server.js:175:68
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:18608) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was no
t handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (
rejection id: 1)
(node:18608) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The subscription stops lissenning and returns

{
  "error": {
    "message": "Invalid value used as weak map key"
  }
}

If I don't put accountsGraphQL.context then there are no problems and the subscriptions work fine

To Reproduce

Make a apolloserver server working subscriptions Now add accountsGraphQL.context as the context like so

const server = new ApolloServer({
            subscriptions,
            typeDefs,
            resolvers,
            context: accountsGraphQL.context
        });

now you should be getting a lot of error messages.

Expected behavior

The subscriptions should just listen like normal

System information

pradel commented 3 years ago

We should definitely handle this internally but for now, as a workaround, you can do the following:

        const server = new ApolloServer({
            subscriptions,
            typeDefs,
            resolvers,
            context: ({ req, connection }) => {
                // If connection is set it means it's a websocket connection
                if (connection) {
                    return connection.context;
                }

                return accountsGraphQL.context({ req });
            }
        });
tintin10q commented 3 years ago

This works fine thanks

pradel commented 3 years ago

I am keeping the issue open until a fix is merged :)

tintin10q commented 3 years ago

Yeah sorry I was also thinking that right after I closed it