0

I have a requirement like I want to subscribe an GraphQLHttpClient using CreateSubscriptionStream into a function app .


stream.Subscribe(
                response =>
                {
                    Console.WriteLine($"RaceUpdatesSubscription message: \"{response.Data}\" ");
                    SendMsgToTopic.SendMessageToTopicAsync(response.Data, _serviceBusConnectionString, _raceUpdatesTopicName).Wait();
                },
                exception => Console.WriteLine($"message RaceUpdatesSubscription stream failed: {exception}"),
                () => Console.WriteLine($"message RaceUpdatesSubscription stream completed")
            );

How can I achieve that Like is there any way to user any trigger from azure function or register this for subscription (Any changes happen then I need to listen that)

Now its working fine with console app but trying to use function app.

amethianil
  • 480
  • 2
  • 7
  • 16

1 Answers1

0

To CreateSubscriptionStream into a function app. Below are the details to subscribe

// To use NewtonsoftJsonSerializer, add a reference to NuGet package GraphQL.Client.Serializer.Newtonsoft
var graphQLClient = new GraphQLHttpClient("https://api.example.com/graphql", new NewtonsoftJsonSerializer());

Below is the code to use subscriptions

public class UserJoinedSubscriptionResult {
    public ChatUser UserJoined { get; set; }

    public class ChatUser {
        public string DisplayName { get; set; }
        public string Id { get; set; }
    }
}

Below is the code how to create subscription

var userJoinedRequest = new GraphQLRequest {
    Query = @"
    subscription {
        userJoined{
            displayName
            id
        }
    }"
};

IObservable<GraphQLResponse<UserJoinedSubscriptionResult>> subscriptionStream 
    = client.CreateSubscriptionStream<UserJoinedSubscriptionResult>(userJoinedRequest);

var subscription = subscriptionStream.Subscribe(response => 
    {
        Console.WriteLine($"user '{response.Data.UserJoined.DisplayName}' joined")
    });

To get complete information on CreateSubscriptionStream refer this GraphQL

SaiSakethGuduru
  • 2,218
  • 1
  • 5
  • 15
  • I have already written this code block but issue was how I can use this subscription with azure function app. I found solution I use azure configure methods of FunctionsStartup to subscribe GraphQl change .Thanks – amethianil Aug 27 '21 at 11:36
  • [assembly: FunctionsStartup(typeof(MyNamespace.Startup))] namespace MyNamespace { public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { // Register grapgql subscription here } } }'' – amethianil Aug 27 '21 at 11:38