>GraphQL 既是一种用于 API 的查询语言也是一个知足你数据查询的运行时。GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端可以准确地得到它须要的数据,并且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。json
> ——出自 https://graphql.cn
上一篇博文中,咱们返回值是一个字符串,对于大多数状况,咱们更多的是返回实体类的json格式。ide
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来讲就很是清楚了;这并非咱们想要的。ui
#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中的两个方法的类型定义和重命名。spa
运行结果以下,对,这就是咱们要的结果;但总以为为了实现返回json,咱们的代价是否是有点大?code
#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。blog
运行结果与版本二同样。ip