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()
});