Fork me on GitHub

GraphQL:面对复杂类型

  GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。

                                   ——出自 https://graphql.cn

  上一篇博文中,我们返回值是一个字符串,对于大多数情况,我们更多的是返回实体类的json格式。

第一版

using HotChocolate;
using HotChocolate.Data;
using HotChocolate.Execution;
using HotChocolate.Types;
using System;
using System.Collections.Generic;


namespace GraphQLBase002
{
    class Program
    {
        static void Main(string[] args)
       {         
            FirstVersion.Run();       
        }
    }
   //实体类
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }

    #region FirstVersion
    public class FirstVersion
    {
        public static void Run()
{
            var schema = SchemaBuilder.New()
                .AddQueryType<QueryType>()
                .Create();
            var executor = schema.MakeExecutable();
            //回为返回是字符串,所以用定义的Resolver name来查询
            Console.WriteLine(executor.Execute("{ students }").ToJson());
        }

        public class Query
        {
            public IList<Student> GetStudents()
            {
                return new List<Student>() {
                    new Student {
                        Id = 100,
                        Name = "ABCD",
                        Age=20
                    },
                     new Student {
                        Id = 101,
                        Name = "EFGH",
                        Age=19
                    }
                };
            }
        }

        public class QueryType : ObjectType<Query>
        {
            protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
{
                //定义了有students来请求GetStudents方法,返回的类型是StringType,所以在Resolver中会把实体转成Json
                descriptor.Field<Query>(t => t.GetStudents()).Name("students").Type<NonNullType<StringType>>().Resolver(ctx =>
               {
                   var result = ctx.Parent<Query>().GetStudents();
                   return Newtonsoft.Json.JsonConvert.SerializeObject(result);
               });
            }
        }
    }
    #endregion

  为了返回一个json,用Resolver来获取GetStudents,并把实例亲手转成json返回,因为是字符串,所以这个Field的Type是StringType。

  运行结果,看起来是个json,不,准确说是一个json格式的字符串,其实从我们定义Resolver来说就非常清楚了;这并不是我们想要的。

 

 

第二版

 

 #region SecondVersion
    public class SecondVersion
    {
        public static void Run()
{
            var schema = SchemaBuilder.New()
                .AddQueryType<QueryType>()
                .Create();
            var executor = schema.MakeExecutable();

            Console.WriteLine(executor.Execute("{ student {id name} }").ToJson());
            Console.WriteLine(executor.Execute("{ students {id name} }").ToJson());
        }
        public class Query
        {

            public Student GetStudent()
{
                return new Student
                {
                    Id = 1,
                    Name = "AAAAA",
                    Age = 19

                };
            }

            public List<Student> GetStudents()
            {
                return new List<Student>{
                    new Student
                    {
                        Id = 100,
                        Name = "ABCD",
                        Age = 19
                    },
                    new Student
                    {
                        Id = 101,
                        Name = "EFGH",
                        Age = 20
                    }
                };
            }
        }

        public class StudentType : ObjectType<Student>
        {
            protected override void Configure(IObjectTypeDescriptor<Student> descriptor)
{
            }
        }

        public class QueryType : ObjectType<Query>
        {
            protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
{
                descriptor.Field(t => t.GetStudent()).Type<NonNullType<StudentType>>().Name("student");
                descriptor.Field(t => t.GetStudents()).Type<ListType<NonNullType<StudentType>>>().Name("students");
            }
        }
    }
    #endregion

  这次我们为了不再是json格式字符串,在代码中定义了StudentType这个的类型,告诉系统Student不是一个简单类型,但Student内的属性,都是简单类型,所以在Configure中没有作任何处理(如果Student中有自定义复杂类型的属性,还得进一步定义这个类型,并在Configure中处理),在QueryType中,处理了Query中的两个方法的类型定义和重命名。

  运行结果如下,对,这就是我们要的结果;但总觉得为了实现返回json,我们的代价是不是有点大?

 

 

 第三版

#region ThreeVersion
    public class ThreeVersion
    {
        public static void Run()
        {
            var schema = SchemaBuilder.New()
                .AddProjections()
                .AddQueryType<Query>()
                .Create();
            var executor = schema.MakeExecutable();

            Console.WriteLine(executor.Execute("{ student{id name age} }").ToJson());
            Console.WriteLine(executor.Execute("{ students{id name age} }").ToJson());
        }
        public class Query
        {
            [UseProjection]            
            public Student GetStudent()
            {
                return new Student
                {
                    Id = 1,
                    Name = "AAAAA",
                    Age = 19

                };
            }
            [UseProjection]
            public List<Student> GetStudents()
            {
                return new List<Student>{
                    new Student
                    {
                        Id = 100,
                        Name = "ABCD",
                        Age = 19
                    },
                    new Student
                    {
                        Id = 101,
                        Name = "EFGH",
                        Age = 20
                    }
                };
            }
        }
    }
    #endregion

  这一版我们借鸡下蛋,用UseProjection来替代了我们定义的类型,连QueryType也消失了,这样的代码才是我们想要的,让我们更关注业务的逻辑,而不是关注为了GraphQL,而做很多技术配合工作;其实我们从第二版看定义的类型StudentType,QueryType,也知道这些类型是非常规律性的,是可以通过代码手段替代的,那就是UseProjection。

  运行结果与版本二一样。

 

  想要更快更方便的了解相关知识,可以关注微信公众号 
 

 

 

 
 
posted @ 2022-02-01 15:25  桂素伟  阅读(41)  评论(0编辑  收藏  举报