SAHB.GraphQLClient icon indicating copy to clipboard operation
SAHB.GraphQLClient copied to clipboard

I'm trying to subscribe to this API, but I don't receive any response. These are my classes and this is the subscription

Open ClarenceMG opened this issue 5 years ago • 6 comments

ws://snowtooth.moonhighway.com/graphql

I'm using Xamarin iOS.

subscription { liftStatusChange { name capacity } }

public class LiftStatusChange
{
	public string Name { get; set; }
	public int Capacity { get; set; }
}

public class Data
{
	public LiftStatusChange LiftStatusChange { get; set; }
}

ClarenceMG avatar Feb 07 '20 16:02 ClarenceMG

I'm on the sofa now lol, so can't examine in detail - but my immediate thought was that it might be a case issue. See https://github.com/sahb1239/SAHB.GraphQLClient/issues/69 I find Wireshark a good tool for this kind of 'hidden' issue - look for the subscription request, the Ack, the sub messages being sent out. Often helps identify where the chain is broken. FWIW....

chelliwell avatar Feb 07 '20 16:02 chelliwell

I have created a example for you:

using System;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SAHB.GraphQLClient;
using SAHB.GraphQLClient.FieldBuilder;
using SAHB.GraphQLClient.FieldBuilder.Attributes;
using SAHB.GraphQLClient.QueryGenerator;
using SAHB.GraphQLClient.Subscription;

namespace LiftStatus
{
    class Program
    {
        
        static async Task Main(string[] args)
        {
            using (var wssClient = GraphQLSubscriptionWebSocketClient.Default())
            {
                // Connect
                var graphQLSubscriptionClient = await wssClient.Connect(new Uri("ws://snowtooth.moonhighway.com/graphql"));
                if (!graphQLSubscriptionClient.IsConnected)
                {
                    Console.WriteLine("Could not connect!");
                    Console.ReadKey();

                    return;
                }

                // Initilize
                await graphQLSubscriptionClient.Initilize();
                if (!graphQLSubscriptionClient.IsInitilized)
                {
                    Console.WriteLine("Could not initilize!");
                    Console.ReadKey();

                    return;
                }

                var operation = await graphQLSubscriptionClient.ExecuteOperation<Data>();
                operation.DataRecieved += (sender, e) =>
                {
                    Console.WriteLine("Lift changed: " + e.ReceivedData.Data.LiftStatusChange.Name);
                };

                IGraphQLHttpClient client = GraphQLHttpClient.Default();

                while (true)
                {
                    await SetLiftStatus(client, "astra-express", LiftStatus.OPEN);

                    Console.ReadKey();
                }
            }
        }

        static async Task<Lift> SetLiftStatus(IGraphQLHttpClient client, string id, LiftStatus liftStatus)
        {
            var output = await client.Execute<SetLiftStatus>(GraphQLOperationType.Mutation, url: "http://snowtooth.moonhighway.com/graphql", arguments:
                new GraphQLQueryArgument[] { new GraphQLQueryArgument<SetLiftStatus>("id", id, e => e.Lift),
                new GraphQLQueryArgument<SetLiftStatus>("status", liftStatus, e => e.Lift)});
            return output.Lift;
        }
    }

    public class Lift
    {
        public string Name { get; set; }
    }

    [JsonConverter(typeof(StringEnumConverter))]
    public enum LiftStatus
    {
        [EnumMember(Value = "OPEN")]
        OPEN,
        [EnumMember(Value = "CLOSED")]
        CLOSED,
        [EnumMember(Value = "HOLD")]
        HOLD
    }

    public class SetLiftStatus
    {
        [GraphQLArguments("id", "ID!", "id", true)]
        [GraphQLArguments("status", "LiftStatus!", "status", true, true)]
        [GraphQLFieldName("setLiftStatus")]
        public Lift Lift { get; set; }
    }

    public class LiftStatusChange
    {
        public string Name { get; set; }
        public int Capacity { get; set; }
    }

    public class Data
    {
        public LiftStatusChange LiftStatusChange { get; set; }
    }
}

This produces the following output: image

Note I'm using the latest version from develop - a preview NuGet feed is available (please see the readme) :)

Edit: If you remove e => e.Lift in the SetLiftStatus it should work with the version on NuGet.

sahb1239 avatar Feb 10 '20 20:02 sahb1239

Thank you so much! I will test the example.

ClarenceMG avatar Feb 10 '20 20:02 ClarenceMG

@ClarenceMG Does it work?

sahb1239 avatar Feb 25 '20 20:02 sahb1239

I haven't tested yet. I will test as soon as possible and I will let you know. Thank you.

ClarenceMG avatar Feb 25 '20 20:02 ClarenceMG

@sahb1239 your example is not working for me. Apparently I subscribe but I never receive an answer, so I think the library disconnects very quickly. I'm using Xamarin iOS 😕

ClarenceMG avatar Feb 26 '20 16:02 ClarenceMG