graphql-client: Subscribe with Authentication is not working for me
var options = new GraphQLHttpClientOptions
{
EndPoint = new Uri(APIUrl)
};
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var graphQLHttpClient = new GraphQLHttpClient(options, httpClient, new GraphQL.Client.Serializer.Newtonsoft.NewtonsoftJsonSerializer());
This is my client.
Now I try to subscribe:
IObservable<GraphQLResponse<object>> subscriptionStream = graphQLHttpClient.CreateSubscriptionStream<object>(request);
IDisposable subscription = subscriptionStream.Subscribe(response =>
{
var value = response.Data;
if (response.Data != null)
{
var serializeResponse = JsonConvert.SerializeObject(response);
Console.WriteLine(serializeResponse);
}
});
But I never receive a response.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15
Currently you’ll have to reimplement one of the serializers (because that uses a private implementation of
GraphQLRequestin order to correctly set JSON property names).Edit: In v2.1.2 you can just
Add()it, sinceGraphQLRequestis now based onDictionary<string, object>…Yes, that happens because this client does not send any
contextfield.What server library are u using? I suspect its apollo-server?
Currently there is no fixed specification on how to do authentication over websockets, and Apollo has from a quick glance decided to go for the “authenticate the websocket once on establishing the connection”.
I myself prefer the approach to add the authentication token to each query/mutation/subscription payload so you can check this on each request and be able to change it without destroying and reestablishing the websocket connection (since im using the websocket for queries/mutations too). How this can be done with graphql-dotnet was descibed in an earlier answer.
As I already said the implementation has to match that of the server. Feel free to locate and share the specification your server is using or even do a PR implementing it.