Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass http headers when making graphql subscription from ios #3436

Closed
jisaacks opened this issue Aug 30, 2024 · 3 comments
Closed

How to pass http headers when making graphql subscription from ios #3436

jisaacks opened this issue Aug 30, 2024 · 3 comments
Labels
question Issues that have a question which should be addressed

Comments

@jisaacks
Copy link

Question

I am using istio to intercept requests, authenticate an Access-Token header and then provide a X-User-ID header if the request was authenticated. (Otherwise the request gets stopped at the istio layer and never reaches the main server)

The server itself is an apollo graphql server that provides subscriptions.

When making a subscription to this server from a website, we just pass the Access-Token as a header, istio intercepts, injects the X-User-ID and everything is fine.

When it comes to ios, I cannot figure out how to pass header, the docs say to use connectingPayload like so:

let webSocketTransport: WebSocketTransport = {
  let url = URL(string: "ws://localhost:8080/websocket")!
  let webSocketClient = WebSocket(url: url, protocol: .graphql_transport_ws)
  let authPayload: JSONEncodableDictionary = ["authToken": myAuthToken]
  let config = WebSocketTransport.Configuration(connectingPayload: authPayload)
  return WebSocketTransport(websocket: webSocketClient, config: config)
}()

This does not seem to work when there is an istio layer. I think I need to just set headers for the request directly, but I cannot find docs that specify how.

@jisaacks jisaacks added the question Issues that have a question which should be addressed label Aug 30, 2024
@andr3a88
Copy link

Try setting the header like this: webSocketClient.request.setValue(accessToken, forHTTPHeaderField: "Authorization")

I suggest using Postman to quickly test the connection to the websocket so you can figure out where to place the authentication token and see if it works!

@calvincestari
Copy link
Member

Hi @jisaacks - there isn't another way unfortunately. The docs say to use connectingPayload because that's the supported way to supply auth for websockets. @andr3a88 is correct that you can probably set the headers directly on the request because the property is publicly exposed but it's not the supported method.

Copy link
Contributor

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that have a question which should be addressed
Projects
None yet
Development

No branches or pull requests

3 participants