Search code examples
graphqldeserializationgraphql-dotnetgraphqlclient

How to deserialize Graphql Response


Hi I have a graphql Response after Mutating

{{
 "carCreate": {
   "car": {
     "id": "TestId"
   }
 }
}}

I want to Desealize it I am using The following Code

var graphQlClient = new GraphQLHttpClient(AppConfig.GraphQlUrl, new NewtonsoftJsonSerializer());

I have tried to resolve with the following code

var response = await graphQlClient.SendMutationAsync<CarCreate>(request);

My Created Model is :

 public class Car
    {
        public string Id { get; set; }
    }
public class CarCreate
    {
        public Car Car { get; set; }
    }

Solution

  • Your class should be something like this,

    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
        public class Car
        {
            public string id { get; set; }
        }
    
        public class CarCreate
        {
            public Car car { get; set; }
        }
    
        public class Root
        {
            public CarCreate carCreate { get; set; }
        }