Fork me on GitHub

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

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

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

 

 

所以为了能够跟上最新的步伐,当然他们肯定还会改进,最终的目的还是向之前说的,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"
                }
            };

 

需要注意的一点是:

我们是根据name字段查询的,name的类型是String,并且非空,所以再query我们定义

$name: String!

如果有多个字段,那么需要一一列出

$id: Int!, $operation: OperationType!, $brand: String

其中OperationType为枚举。

 

当然这个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; }
    }

 

posted @ 2020-11-16 16:19  猫叔Vincent  阅读(218)  评论(0编辑  收藏  举报