seo网站优化做什么北京软件外包公司名单
seo网站优化做什么,北京软件外包公司名单,网站做扫一扫,太阳镜商城网站建设GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述#xff0c;使得客户端能够准确地获得它需要的数据#xff0c;而且没有任何冗余#xff0c;也让 API 更容易地随着时间推移而演进#xff0c… 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 FirstVersionpublic class FirstVersion{public static void Run()
{var schema SchemaBuilder.New().AddQueryTypeQueryType().Create();var executor schema.MakeExecutable();//回为返回是字符串所以用定义的Resolver name来查询Console.WriteLine(executor.Execute({ students }).ToJson());}public class Query{public IListStudent GetStudents(){return new ListStudent() {new Student {Id 100,Name ABCD,Age20},new Student {Id 101,Name EFGH,Age19}};}}public class QueryType : ObjectTypeQuery{protected override void Configure(IObjectTypeDescriptorQuery descriptor)
{//定义了有students来请求GetStudents方法返回的类型是StringType所以在Resolver中会把实体转成Jsondescriptor.FieldQuery(t t.GetStudents()).Name(students).TypeNonNullTypeStringType().Resolver(ctx {var result ctx.ParentQuery().GetStudents();return Newtonsoft.Json.JsonConvert.SerializeObject(result);});}}}#endregion
为了返回一个json用Resolver来获取GetStudents并把实例亲手转成json返回因为是字符串所以这个Field的Type是StringType。运行结果看起来是个json不准确说是一个json格式的字符串其实从我们定义Resolver来说就非常清楚了这并不是我们想要的。第二版 #region SecondVersionpublic class SecondVersion{public static void Run()
{var schema SchemaBuilder.New().AddQueryTypeQueryType().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 ListStudent GetStudents(){return new ListStudent{new Student{Id 100,Name ABCD,Age 19},new Student{Id 101,Name EFGH,Age 20}};}}public class StudentType : ObjectTypeStudent{protected override void Configure(IObjectTypeDescriptorStudent descriptor)
{}}public class QueryType : ObjectTypeQuery{protected override void Configure(IObjectTypeDescriptorQuery descriptor)
{descriptor.Field(t t.GetStudent()).TypeNonNullTypeStudentType().Name(student);descriptor.Field(t t.GetStudents()).TypeListTypeNonNullTypeStudentType().Name(students);}}}#endregion
这次我们为了不再是json格式字符串在代码中定义了StudentType这个的类型告诉系统Student不是一个简单类型但Student内的属性都是简单类型所以在Configure中没有作任何处理如果Student中有自定义复杂类型的属性还得进一步定义这个类型并在Configure中处理在QueryType中处理了Query中的两个方法的类型定义和重命名。运行结果如下对这就是我们要的结果但总觉得为了实现返回json我们的代价是不是有点大第三版#region ThreeVersionpublic class ThreeVersion{public static void Run(){var schema SchemaBuilder.New().AddProjections().AddQueryTypeQuery().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 ListStudent GetStudents(){return new ListStudent{new Student{Id 100,Name ABCD,Age 19},new Student{Id 101,Name EFGH,Age 20}};}}}#endregion
这一版我们借鸡下蛋用UseProjection来替代了我们定义的类型连QueryType也消失了这样的代码才是我们想要的让我们更关注业务的逻辑而不是关注为了GraphQL而做很多技术配合工作其实我们从第二版看定义的类型StudentTypeQueryType也知道这些类型是非常规律性的是可以通过代码手段替代的那就是UseProjection。运行结果与版本二一样。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/92286.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!