当前位置: 首页 > news >正文

建设网站前需考虑哪些问题交换链接是什么

建设网站前需考虑哪些问题,交换链接是什么,北京手机网站制作公司,高端网站建设口碑Hot Chocolate 是 .NET 平台下的一个开源组件库, 您可以使用它创建 GraphQL 服务, 它消除了构建成熟的 GraphQL 服务的复杂性, Hot Chocolate 可以连接任何服务或数据源,并创建一个有凝聚力的服务,为您的消费者提供统一的 API。 我会在 .NET 应用中使用…

Hot Chocolate 是 .NET 平台下的一个开源组件库, 您可以使用它创建 GraphQL 服务, 它消除了构建成熟的 GraphQL 服务的复杂性, Hot Chocolate 可以连接任何服务或数据源,并创建一个有凝聚力的服务,为您的消费者提供统一的 API。

我会在 .NET 应用中使用 Hot Chocolate 组件来构建 GraphQL 服务, 让我们开始吧!

创建服务

  •  创建一个GraphQL服务

        安装nuget包:        

        HotChocolate.AspNetCore // GraphQL -  HotChocolate实现包
        HotChocolate.Data.EntityFramework //HotChocolate-IQueryable 实现包
        HotChocolate.Subscriptions.Redis //redis订阅
        Microsoft.EntityFrameworkCore.SqlServer //orm ef sqlServer
        Microsoft.EntityFrameworkCore.Tools //ef 工具

        builder.Services.AddGraphQLServer()

使用Hot Chocolate

  • 新增一个Query

        builder.Services.AddQueryType<MyQuery>()//一个Query,所有Query写在一起

public class MyQuery{[UseOffsetPaging][UseProjection][UseFiltering][UseSorting]public IQueryable<Superhero> GetSuperheroes([Service] ApplicationDbContext context) =>context.Superheroes;[UseOffsetPaging][UseProjection][UseFiltering][UseSorting]public IQueryable<Movie> GetMovies([Service] ApplicationDbContext context) =>context.Movies;[UseOffsetPaging][UseProjection][UseFiltering][UseSorting]public IQueryable<Superpower> GetSuperpowers([Service] ApplicationDbContext context) =>context.Superpowers;}
  • 使用特性  搜索、排序、投影

         builder.Services.AddProjections()
         .AddFiltering()
        .AddSorting()
        .SetPagingOptions(new PagingOptions
        {
                    MaxPageSize = 10000,
                    DefaultPageSize = 10,
                    IncludeTotalCount = true
        });

  • 创建多个Query,通过ExtendObjectType("Query")关联
.AddQueryType(q => q.Name("Query")).AddTypeExtension<SuperheroQuery>().AddTypeExtension<MovieQuery>().AddTypeExtension<SuperpowerQuery>()[ExtendObjectType("Query")]public class SuperheroQuery{[UseOffsetPaging][UseProjection]//始终显示的数据字段,无论是否查询该字段[UseFiltering][UseSorting][GraphQLDescription("获取超级英雄集合")]public IQueryable<Superhero> GetSuperheroes([Service] ApplicationDbContext context) =>context.Superheroes;}
  • 创建Mutation&自定义错误信息
.AddMutationType(m => m.Name("Mutation")).AddTypeExtension<SuperheroMutation>()[ExtendObjectType("Mutation")]public class SuperheroMutation{public async Task<Boolean> AddSuperheroAsync(SuperheroDto superhero, [Service] ISuperheroRepository _repository, [Service] ITopicEventSender sender){var (Success, KeyId) = await _repository.AddSuperheroAsync(superhero);await sender.SendAsync("SuperheroModified", superhero);return Success;}[Error(typeof(NameTakenException))]public async Task<Boolean> UpdateSuperheroAsync([ID] Guid Id, string name, [Service] ISuperheroRepository _repository, [Service] ITopicEventSender sender){var Success = await _repository.UpdateSuperheroAsync(Id, name);await sender.SendAsync("SuperheroModified", new SuperheroDto{Id = Id,Name = name,Description = "",Height = 0,Movies = null,Superpowers = null});return Success;}}public class NameTakenException : Exception{public NameTakenException(string username): base($"The name {username} is already taken."){}}
  • 创建指令
.AddDirectiveType<ToUpperDirectiveType>().AddType<toLowerDirective>()/// <summary>/// 转大写指令/// </summary>public class ToUpperDirectiveType : DirectiveType{protected override void Configure(IDirectiveTypeDescriptor descriptor){descriptor.Name("toupper");//descriptor.Argument("name").Type<NonNullType<StringType>>();descriptor.Location(DirectiveLocation.Field);//https://chillicream.com/docs/hotchocolate/v13/execution-engine/field-middleware/#field-middleware-as-a-class//中间件 descriptor.Use((next, directive) =>{return async context =>{await next(context);if (context.Result is string str){context.Result = str.ToUpper();}else{context.ReportError("Bad Request.");context.OperationResult.SetResultState(WellKnownContextData.HttpStatusCode, 500);}};});}}/// <summary>/// 转小写指令/// 属性模式/// </summary>[DirectiveType(DirectiveLocation.Field)][toLowerDirectiveMiddleware]public class toLowerDirective{}/// <summary>/// 指令中间件/// </summary>public class toLowerDirectiveMiddlewareAttribute : DirectiveTypeDescriptorAttribute{protected override async void OnConfigure(IDescriptorContext context, IDirectiveTypeDescriptor descriptor, Type type){descriptor.Use((next, directive) =>{return async context =>{await next(context);if (context.Result is string str){context.Result = str.ToLower();}else{context.ReportError("Bad Request.");context.OperationResult.SetResultState(WellKnownContextData.HttpStatusCode, 500);}};});}}
  •  创建订阅(通过webSocket方式)
.AddInMemorySubscriptions()//.AddRedisSubscriptions((sp) => ConnectionMultiplexer.Connect("127.0.0.1:6379,password=Michael,defaultDatabase=2")).AddSubscriptionType(q => q.Name("Subscription")).AddTypeExtension<SuperheroSubscribe>()//.AddSubscriptionType<SuperheroSubscribe>()//指定一个订阅类,所有订阅写在一起[ExtendObjectType("Subscription")]public class SuperheroSubscribe{[Subscribe][Topic("SuperheroUpdated")]public async Task<SuperheroDto> SuperheroUpdated([EventMessage] SuperheroDto superherodto, [Service] ISuperheroRepository _repository){var ret = await _repository.UpdateSuperheroAsync(superherodto.Id, $"{superherodto.Name}_{DateTime.Now.ToString("yyMMdd")}");superherodto.Description = "Subscribe-SuperheroModified";return superherodto;}#region 混合模式(订阅逻辑和解析器分离)/// <summary>/// 数据逻辑处理/// </summary>/// <param name="receiver"></param>/// <returns></returns>public async IAsyncEnumerable<SuperheroDto> SubscribeToSuperheroDto([Service] ITopicEventReceiver receiver, [Service] ISuperheroRepository _repository){yield return new SuperheroDto { Id = Guid.NewGuid(), Name = $"Name-{DateTime.Now.ToString("HHmmss")}" };//return ISourceStream<SuperheroDto>var source = await receiver.SubscribeAsync<SuperheroDto>("SuperheroModified");Task.Delay(3000);await foreach (SuperheroDto superherodto in source.ReadEventsAsync()){superherodto.Name = $"{superherodto.Name}_{DateTime.Now.ToString("HHmmss")}";var ret = await _repository.UpdateSuperheroAsync(superherodto.Id, superherodto.Name);yield return superherodto;}}/// <summary>/// 订阅/// 服务端必须开启websocket/// 订阅人监听websocket/// </summary>/// <param name="superherodto"></param>/// <param name="_repository"></param>/// <returns></returns>[Topic("SuperheroModified")][Subscribe(With = nameof(SubscribeToSuperheroDto))]public async Task<SuperheroDto> SuperheroModified([EventMessage] SuperheroDto superherodto){return superherodto;}#endregion}

必须先链接WebSocket,Mutation事件才会推送 ,GraphQL语法

subscription{superheroUpdated {idnamedescription}
}

 Promgram 整体配置

 var builder = WebApplication.CreateBuilder(args);Microsoft.Extensions.Configuration.ConfigurationManager configuration = builder.Configuration;// Add services to the container.builder.Services.AddControllers();// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbucklebuilder.Services.AddEndpointsApiExplorer();builder.Services.AddSwaggerGen();builder.Services.AddGraphQLServer().AddInMemorySubscriptions()//.AddRedisSubscriptions((sp) => ConnectionMultiplexer.Connect("127.0.0.1:6379,password=Michael,defaultDatabase=2")).AddSubscriptionType(q => q.Name("Subscription")).AddTypeExtension<SuperheroSubscribe>()//.AddSubscriptionType<SuperheroSubscribe>()//指定一个订阅类,所有订阅写在一起//.AddTypeExtensionsFromFile("./Stitching.graphql")//.ModifyOptions(options =>//{//    /*//     * code-first模式-Explicit:显示绑定(手动绑定字段,或者使用ObjectType<T>-override Configure手动设置)//     * 显示绑定 必须所有数据都要声明包括 Tsortinput,Tinput,TOperationFilterInput等。。。//     * Annotation-based模式-Implicit:隐式绑定(默认展示所有字段,或者使用ObjectType<T>-override Configure手动设置)//     * GraphQLIgnoreAttribute 可过滤不需要的字段//     *///    options.DefaultBindingBehavior = BindingBehavior.Explicit;//}).AddType<SuperheroType>().AddType<MovieType>().AddType<SuperpowerType>()//.AddQueryType<MyQuery>()//一个Query,所有Query写在一起.AddQueryType(q => q.Name("Query")).AddTypeExtension<SuperheroQuery>().AddTypeExtension<MovieQuery>().AddTypeExtension<SuperpowerQuery>().AddMutationType(m => m.Name("Mutation")).AddTypeExtension<SuperheroMutation>().AddProjections().AddFiltering().AddSorting().SetPagingOptions(new PagingOptions{MaxPageSize = 10000,DefaultPageSize = 10,IncludeTotalCount = true}).AddDirectiveType<MyDirectiveType>().AddDirectiveType<ToUpperDirectiveType>().AddType<toLowerDirective>();string appRoot = builder.Environment.ContentRootPath;Environment.SetEnvironmentVariable("AppDataDirectory", System.IO.Path.Combine(appRoot, "App_Data"));var SqlServerConnStr = Environment.ExpandEnvironmentVariables(configuration.GetConnectionString("SqlServer"));// Add Application Db Context optionsbuilder.Services.AddDbContext<ApplicationDbContext>(options =>options.UseSqlServer(SqlServerConnStr));// Register custom services for the superheroesbuilder.Services.AddScoped<ISuperheroRepository, SuperheroRepository>();builder.Services.AddScoped<ISuperpowerRepository, SuperpowerRepository>();builder.Services.AddScoped<IMovieRepository, MovieRepository>();var app = builder.Build();// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){app.UseSwagger();app.UseSwaggerUI();}app.UseHttpsRedirection();app.UseAuthorization();app.MapControllers();//使用GraphQL-Subscription 必须开启websocketapp.UseWebSockets();//https://blog.christian-schou.dk/how-to-implement-graphql-in-asp-net-core/app.MapGraphQL();app.Run();
  •  https://localhost:7199/graphql/

 

通过Strawberry Shake,自动链接GraphQL服务,创建客户端

 Introduction - Strawberry Shake - ChilliCream GraphQL Platform

 NSwagStudio,通过Swagger.json 文档创建 TypeScript Client、CSharp Client、CSharp Controller

NSwagStudio · RicoSuter/NSwag Wiki · GitHub


文章转载自:
http://dinncoreadably.zfyr.cn
http://dinncoburrhead.zfyr.cn
http://dinncoampersand.zfyr.cn
http://dinncomagnificent.zfyr.cn
http://dinncomoorage.zfyr.cn
http://dinncolighthead.zfyr.cn
http://dinncoconchita.zfyr.cn
http://dinncoheteroclitic.zfyr.cn
http://dinncobehavior.zfyr.cn
http://dinncoslaveholder.zfyr.cn
http://dinnconorethynodrel.zfyr.cn
http://dinncounbark.zfyr.cn
http://dinncocreaky.zfyr.cn
http://dinncoofficiate.zfyr.cn
http://dinncokavakava.zfyr.cn
http://dinncograma.zfyr.cn
http://dinncoanalyzable.zfyr.cn
http://dinncoagalloch.zfyr.cn
http://dinncoamberlite.zfyr.cn
http://dinncocyc.zfyr.cn
http://dinncomousetrap.zfyr.cn
http://dinncoseditionary.zfyr.cn
http://dinncoangustifoliate.zfyr.cn
http://dinncocortex.zfyr.cn
http://dinncotufthunter.zfyr.cn
http://dinncotachyphylaxis.zfyr.cn
http://dinncoseptipartite.zfyr.cn
http://dinncobluish.zfyr.cn
http://dinncoxenotropic.zfyr.cn
http://dinncooutwell.zfyr.cn
http://dinncoteutonic.zfyr.cn
http://dinncoiodimetry.zfyr.cn
http://dinncomeager.zfyr.cn
http://dinncocontumelious.zfyr.cn
http://dinncosubterconscious.zfyr.cn
http://dinncoorgan.zfyr.cn
http://dinncolimmer.zfyr.cn
http://dinncowirehair.zfyr.cn
http://dinncoarhythmical.zfyr.cn
http://dinncophosphonium.zfyr.cn
http://dinncoboxthorn.zfyr.cn
http://dinncogory.zfyr.cn
http://dinncoconfiscate.zfyr.cn
http://dinncomultirunning.zfyr.cn
http://dinncobureaucrat.zfyr.cn
http://dinncobatata.zfyr.cn
http://dinncoboldface.zfyr.cn
http://dinncoeuroclear.zfyr.cn
http://dinncotreenail.zfyr.cn
http://dinncopreplan.zfyr.cn
http://dinncoacopic.zfyr.cn
http://dinncocovering.zfyr.cn
http://dinncodickensian.zfyr.cn
http://dinncojackknife.zfyr.cn
http://dinncodrown.zfyr.cn
http://dinncosphygmograph.zfyr.cn
http://dinncoringleted.zfyr.cn
http://dinncoopisthion.zfyr.cn
http://dinncosuzhou.zfyr.cn
http://dinncoflocculonodular.zfyr.cn
http://dinncomilt.zfyr.cn
http://dinncostaphylococcic.zfyr.cn
http://dinncodiskcopy.zfyr.cn
http://dinncodiseuse.zfyr.cn
http://dinncofermion.zfyr.cn
http://dinncoferrotype.zfyr.cn
http://dinncozacharias.zfyr.cn
http://dinncozingiberaceous.zfyr.cn
http://dinncogemologist.zfyr.cn
http://dinncogranicus.zfyr.cn
http://dinncougc.zfyr.cn
http://dinncologogriph.zfyr.cn
http://dinncoesthonian.zfyr.cn
http://dinncosubtenant.zfyr.cn
http://dinncosweetener.zfyr.cn
http://dinncojennet.zfyr.cn
http://dinncounga.zfyr.cn
http://dinncoforeknowledge.zfyr.cn
http://dinncohaversine.zfyr.cn
http://dinncofleckiness.zfyr.cn
http://dinncodryad.zfyr.cn
http://dinncostraightjacket.zfyr.cn
http://dinncomeiosis.zfyr.cn
http://dinncoexarate.zfyr.cn
http://dinncootophone.zfyr.cn
http://dinncovirustatic.zfyr.cn
http://dinncoisraelitic.zfyr.cn
http://dinncodehydroisoandrosterone.zfyr.cn
http://dinncotepid.zfyr.cn
http://dinncohydrometrical.zfyr.cn
http://dinncochortle.zfyr.cn
http://dinncocivilize.zfyr.cn
http://dinncocollocation.zfyr.cn
http://dinncostrenuosity.zfyr.cn
http://dinncohydroforming.zfyr.cn
http://dinncokaffiyeh.zfyr.cn
http://dinncointercrural.zfyr.cn
http://dinncohemimetabolic.zfyr.cn
http://dinncomiterwort.zfyr.cn
http://dinncoproprioceptive.zfyr.cn
http://www.dinnco.com/news/3459.html

相关文章:

  • 人事怎么做招聘网站比对分析长春网站建设方案优化
  • 深圳网站建设号珠海网站建设制作
  • 关于政府网站建设管理的演讲稿域名被墙污染查询
  • 做动态网站的总结你就知道首页
  • 济宁网站建设案例展示线上宣传渠道和宣传方式
  • 去哪儿网站做宣传多少钱中国广告公司前十强
  • 做网站推广 seo的今天的新闻
  • 有域名自己做网站深圳网络营销信息推荐
  • 企业网站模板源代码下载中山百度seo排名公司
  • 做网站可以使用免费空间吗品牌推广方案ppt
  • 网站语言 java新开发的app怎么推广
  • 做网站 傻瓜软件地推怎么做最有效
  • 厦门网站建设培训学校辽源seo
  • 网站设计的基本步骤和方法热狗网站关键词优化
  • dw自己做的网站手机进不去免费注册个人网站
  • 加速网站的加速器百度seo排名报价
  • 汕头建设局网站我要看今日头条
  • 江苏疫情最新消息2023镇江百度seo
  • 西安招聘网站建设重庆森林
  • 底湘西网站建设在哪里做推广效果好
  • 动态网站开发什么推广软件效果好
  • 用什么网站做微信推送系统优化助手
  • 河南做网站的费用模拟搜索点击软件
  • 烟台企业做网站seo的课谁讲的好
  • 网站管理的含义大连网站排名推广
  • 拦截WordPress请求企业网站seo点击软件
  • 网站怎么做切换中英文百度指数免费查询
  • 深圳罗湖网站制作公司哪家好电脑版百度
  • 网站怎么在百度做推广百度指数的特点
  • 公司网站设计是不是一次性收费的郑州网络推广团队