UWP graphql-dotnet新版数据查询客户端的实现

之前写过一篇文章  UWP GraphQL数据查询客户端的实现,这次的标题基本差不多,只不过微软这个graphql-dotnet repo从1.x升级到了3.x,跨度有点大。

如果你用之前的项目,直接升级,那么所有的方法都报错,因为微软已经完全重写了这个graphql的实现??????

UWP graphql-dotnet新版数据查询客户端的实现

 

 

所以为了能够跟上最新的步伐,当然他们肯定还会改进,最终的目的还是向之前说的,Apollo框架那样,简单易用。

这次更新加入了变量的引用,更加人性化一点了。

为了方便测试,我找了一个公开的graphQL的api end point.

https://graphql-weather-api.herokuapp.com/

更多的在线地址,参考http://apis.guru/graphql-apis/

 

            GraphQLHttpClient graphQLClient = new GraphQLHttpClient("https://graphql-weather-api.herokuapp.com/", new NewtonsoftJsonSerializer());
            var request = new GraphQLRequest
            {
                Query = @"query($name: String!){
                getCityByName(name:$name){
                    id
                    name
                    country
                    coord{
                        lon
                        lat
                        }
                    weather{
                        summary{
                            title
                            description
                            icon
                            }
                        temperature{
                            actual
                            feelsLike
                            min
                            max
                            }
                        wind{
                            speed
                            deg
                            }
                        clouds{
                            all
                            humidity
                            visibility
                            }
                        timestamp
                        }
                    }
                }",
                Variables = new
                {
                    name = "Beijing"
                }
            };

 

当然这个query语句是加了变量了,变量在Variables种指定,你也可以按照之前的方式进行字符串拼接,不过使用变量语法出错的几率小很多。

然后执行query/mutation的语句是这样

//1. Define a reponse type
var graphQLResponse = await graphQLClient.SendQueryAsync<getCityByNameResponse>(request);
//2. Alternatively you could use an anonymous type:
var graphQLResponse = await graphQLClient.SendQueryAsync(
                request, () => new { getCityByName = new GetCityByName() });

  Debug.WriteLine("raw response:");
  Debug.WriteLine(JsonSerializer.Serialize(graphQLResponse, new JsonSerializerOptions { WriteIndented = true }));

 

就这样,执行了之后就有结果了。

 

Model:

    public class getCityByNameResponse
    {
        public GetCityByName getCityByName { get; set; }
    }

    public class GetCityByName
    {
        public string id { get; set; }
        public string name { get; set; }
        public string country { get; set; }

        public Coordinates coord { get; set; }
        public Weather weather { get; set; }
    }

    public class Weather
    {
        public Summary summary { get; set; }
        public Tempreature tempreature { get; set; }
        public Wind wind { get; set; }
        public Clouds clouds { get; set; }
        public long timestamp { get; set; }
    }

    public class Summary
    {
        public string title { get; set; }
        public string description { get; set; }
        public string icon { get; set; }
    }

    public class Tempreature
    {
        public float actual { get; set; }
        public float feelsLike { get; set; }
        public float min { get; set; }
        public float max { get; set; }
    }

    public class Wind
    {
        public float speed { get; set; }
        public int deg { get; set; }
    }

    public class Clouds
    {
        public int all { get; set; }
        public int visibility { get; set; }
        public int humidity { get; set; }
    }

    public class Coordinates
    {
        public float lon { get; set; }
        public float lat { get; set; }
    }

 

UWP graphql-dotnet新版数据查询客户端的实现

上一篇:软件行业好网站&好书推荐


下一篇:thinkphp 使用 redis