泰兴网站建设开发,网站侧边栏,wordpress 模板怎么用,做网站大量视频怎么存储Union就是把不相干的一些数据实体#xff0c;合并起来#xff0c;一起供外部查询。不用像webapi#xff0c;完成查询不同的数据#xff0c;需要多次请求。一次请求#xff0c;获取多样数据#xff0c;减少请求次数#xff0c;这也是GraphQL的优势之一。怎么弄#xff0… Union就是把不相干的一些数据实体合并起来一起供外部查询。不用像webapi完成查询不同的数据需要多次请求。一次请求获取多样数据减少请求次数这也是GraphQL的优势之一。怎么弄来来来代码看过来using HotChocolate;
using HotChocolate.Execution;
using HotChocolate.Types;
using System;namespace GraphQLBase006
{class Program{static void Main(string[] args){UnionDemo.Run();}}public class UnionDemo{public static void Run(){var schema SchemaBuilder.New().AddQueryTypeQuery().AddTypeCar().AddTypeCabbage().AddTypeEarth().AddProjections().Create();var executor schema.MakeExecutable();Console.WriteLine(executor.Execute(
{formats{__typename,... on Car{brand,price},... on Cabbage{name,nutrition}... on Earth{diameter }}
}).ToJson());}}public class Query{public IUnion[] GetFormats(){return new IUnion[]{new Car{BrandBenz,Price1000000},new Cabbage{Name灰子白,Nutrition纤维}, new Earth{Diameter12742}};}}[UnionType(Unio)]public interface IUnion{}public class Car : IUnion{public string Brand { get; set; }public decimal Price { get; set; }}public class Cabbage : IUnion{public string Name { get; set; }public string Nutrition { get; set; }}public class Earth : IUnion{public double Diameter { get; set; } }
}
案例中就是把不相干的Car,Cabbage,Earth通过继承一个空接口合并起来供外部访问通过自定义查询接口语句达到灵活取想要的数据比如一些数据字典的查询就很有用不相干的一些配置同时获取到以备后用。结果{data: {formats: [{__typename: Car,brand: Benz,price: 1000000},{__typename: Cabbage,name: \u7070\u5B50\u767D,nutrition: \u7EA4\u7EF4},{__typename: Earth,diameter: 12742}]}
}