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

Customizing your WebSocket protocol using GraphQLWsProtocol documentation #6253

Closed
mzubair-cars-commerce opened this issue Nov 11, 2024 · 5 comments

Comments

@mzubair-cars-commerce
Copy link

Question

The documentation for WebSocket protocol using GraphQLWsProtocol leads to the following exception:

Caused by: java.lang.IllegalStateException: Apollo: 'webSocketEngine' has no effect if 'subscriptionNetworkTransport' is set.

Is there additional documentation or guidance available that explains the correct setup for subscriptions using GraphQLWsProtocol? The exception does not provide clear guidance on how to resolve this issue.

@martinbonnin
Copy link
Contributor

Hi 👋 Can you share your ApolloClient building code?

@mzubair-cars-commerce
Copy link
Author

mzubair-cars-commerce commented Nov 11, 2024

Hey @martinbonnin, here is what im using:

ApolloClient.Builder()
            .serverUrl(environment.graphQL)
            .subscriptionNetworkTransport(
                WebSocketNetworkTransport.Builder()
                    .protocol(GraphQLWsProtocol.Factory())
                    .serverUrl(environment.subscription)
                    .build()
            )
            .okHttpClient(okHttpClient)
            .addInterceptor(SomeInterceptor())
            .build(),```

@martinbonnin
Copy link
Contributor

Thanks for sending this 🙏

The issue comes from okHttpClient that calls webSocketEngine() under the hood. I agree the error message is not helping, I'll look into this. In the meantime, you'll need to configure your client for both transports like so:

    val client = ApolloClient.Builder()
      .serverUrl(httpUrl)
      .subscriptionNetworkTransport(
        // subscriptions
        WebSocketNetworkTransport.Builder()
          .protocol(GraphQLWsProtocol.Factory())
          .serverUrl(websocketUrl)
          .okHttpClient(okHttpClient)
          .build()
      )
      .networkTransport(
        // queries/mutations
        HttpNetworkTransport.Builder()
          .okHttpClient(okHttpClient)
          .build()
      )

@mzubair-cars-commerce
Copy link
Author

Thanks @martinbonnin that fixed it!

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 Kotlin 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
Projects
None yet
Development

No branches or pull requests

2 participants