question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async connectionParams at request time

See original GitHub issue

Issue

We supply the authentication token while the request is being made for the http case, this comes in the form of a Token http header.

For the WS case, we wish to supply authentication information (preferably when we first subscribe to the subscription), this is because when the GraphQLWsLink is created, the user has not logged in yet and hence we have no token to supply.

Is there a way of doing some WS equivalent:

const authLink = new ApolloLink(async (operation, forward) => {
  // Mint the token
  const token = createToken();

  // Use the setContext method to set the HTTP headers.
  operation.setContext({
    headers: {
      authorization: token ? `Bearer ${token}` : "",
    },
  });

  // Call the next link in the middleware chain.
  return forward(operation);
});

What we’re after is some way to do something like this:

const wsLink = new GraphQLWsLink(
  createClient({
    url: graphqWSlURL,
      // Called sometime after we've had the chance to log the user in.
+    buildConnectionParams: () => {
+        // Mint the token
+        const token = createToken();
+        return {
+            authorization: token ? `Bearer ${token}` : "",
+        }
+    }
      //The user isn't logged in yet
-    connectionParams: {
-      authorization: //we dont have the token yet :(
-    },
  })
);

Maybe there’s a way to do this that I don’t know about?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
felixmccuaigcommented, Mar 29, 2022

Looks like you can supply an async function, cheers!

1reaction
KillerCodeMonkeycommented, Jun 22, 2022

strange, but the global typing says “no” in my ide, and when i try it, i get the error, that the method takes no parameter. But that’s none of your business.

Thanks again 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

[FEATURE REQUEST] `connectionParams` accept async ...
I want to fetch authToken from local storage any time socket init. But i can't do it right now, because it dont accept...
Read more >
Subscriptions in Apollo Server - Apollo GraphQL Docs
Those connectionParams are sent to your server when it connects, allowing you to extract information from the client request by accessing Context.
Read more >
GraphQL Subscription Authentication Using AsyncStorage
Am trying to use subscription on my client app to send JWT Token and refreshToken to the server using WebSocketLink connectionParams.
Read more >
Using Asyncio with Elasticsearch
import asyncio from elasticsearch import AsyncElasticsearch es ... timeout – Time each individual bulk request should wait for shards that are unavailable.
Read more >
Subscriptions | NestJS - A progressive Node.js framework
GraphQL subscriptions are a way to push data from the server to the clients that choose to listen to real time messages from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found