我正在尝试实现类似于此 python 示例的应用程序同步订阅,但在 .net https://aws.amazon.com/blogs/mobile/appsync-websockets-python/
我使用 nuget 包 GraphQL.Client https://www.nuget.org/packages/GraphQL.Client开始执行此 操作,查询/突变的执行工作正常,如https://github.com/graphql-的自述文件中给出的那样dotnet/graphql-client 但订阅不起作用。
我使用 GraphQL.Client 的代码:
using var graphQLClient = new GraphQLHttpClient("https://<MY-API-PATH>.appsync-realtime-api.<AWS-region>.amazonaws.com/graphql", new NewtonsoftJsonSerializer());
graphQLClient.HttpClient.DefaultRequestHeaders.Add("host", "<API HOST without https or absolute path and 'realtime-' text in the api address>"); //As given in the python example
graphQLClient.HttpClient.DefaultRequestHeaders.Add("x-api-key", "<API KEY>");
var req= new GraphQLRequest
{
Query = @"subscription SubscribeToEventComments{ subscribeToEventComments(eventId: 'test'){ content }}",
Variables = new{}
};
IObservable<GraphQLResponse<Response>> subscriptionStream = graphQLClient.CreateSubscriptionStream<Response>(req, (Exception ex) =>
{
Console.WriteLine("Error: {0}", ex.ToString());
});
var subscription …Run Code Online (Sandbox Code Playgroud)