Reputation: 77
I am working on graphql: Server is .net core web api and I am using Hot Chocolate as graphql server. Client is reactjs and Apollo client.
All queries and mutations are working fine. Subscriptions, however are not working. I am getting: WebSocket connection to 'ws://localhost:5085/graphql/' failed:
I have cors policy set in .net core:
builder.Services.AddCors(options =>
{
options.AddPolicy(name: AllowSpecificOrigins,
builder =>
{
builder
.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed((host) => true)
.AllowCredentials();
});
});
When I test in https://eat.bananacakepop.com/ it is working fine and I am getting data.
This is client code:
const httpLink = new HttpLink({
uri: 'http://localhost:5085/graphql/'
});
const wsLink = new GraphQLWsLink(createClient({
url: 'ws://localhost:5085/graphql/',
}));
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
wsLink,
httpLink,
);
const client = new ApolloClient({
link: splitLink,
cache: new InMemoryCache()
});
Upvotes: 2
Views: 1710
Reputation: 51
Try using:
import { WebSocketLink } from "@apollo/client/link/ws";
const wsLink = new WebSocketLink({
uri: "ws://localhost:5085/graphql/",
});
Note: you may need to use wss
instead of ws
with Hot Chocolate.
Upvotes: 0
Reputation: 77
Based on @Michael Ingmar Staib comment. I upgraded Hot chocolate to version 13 preview packages. Now it is working fine.
Upvotes: 4