真正的GraphQL教程才刚刚开始QWQ,怕了吧,Schema的文件夹才是关于Graph的git
置于GraphQL是什么,一种用于 API 的查询语言github
GraphQL 既是一种用于 API 的查询语言也是一个知足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端可以准确地得到它须要的数据,并且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。web
官网:https://graphql.cn/sql
据我所知的是GraphQL由facebook所制造,现阶段github等一群大佬企业也在用这个
websocket
在类库schema文件夹中添加三个类MovieType,ActorType,MovieRatingEnum
其中MovieRatingEnum是枚举类型,其余的你能够一一对应下
代码看不看的懂,看不懂就对了,否则你也不用学了,直接抄吧,抄个一两遍就有感悟了,到时候再去官网逛逛提高下本身app
首先编写MovieRatingEnum类,直接上代码socket
using GraphQL.Types; using GraphStudy.Movies.Movies; namespace GraphStudy.Movies.Schema { public class MovieRatingEnum:EnumerationGraphType<MovieRatingEnum> { //先定义下构造函数,列表枚举的GraphQL public MovieRatingEnum() { Name = "MovieRating";//类型的名字 Description = "";//类型的描述 //将鼠标置于AddValue上显示name,description,value,[deprecationReason] []的含义是可填可不填的意思 AddValue(MovieRating.Unrated.ToString(), "Unrated",MovieRating.Unrated); AddValue(MovieRating.G.ToString(), "G", MovieRating.G); AddValue(MovieRating.PG.ToString(), "PG", MovieRating.PG); AddValue(MovieRating.PG13.ToString(), "PG13", MovieRating.PG13); AddValue(MovieRating.R.ToString(), "R", MovieRating.R); AddValue(MovieRating.NC17.ToString(), "NC17", MovieRating.NC17); } } }
编写ActorType类,直接上代码svg
using GraphQL.Types; using GraphStudy.Movies.Movies; namespace GraphStudy.Movies.Schema { public class ActorType:ObjectGraphType<Actor> { public ActorType() { Field(x => x.Id); Field(x => x.Name); } } }
编写MovieType类函数
using GraphQL.Types; using GraphStudy.Movies.Movies; using GraphStudy.Movies.Services; namespace GraphStudy.Movies.Schema { public class MovieType:ObjectGraphType<Movie> { //先定义下构造函数 public MovieType(IActorService actorService) { Name = "Movie";//类型的名字 Description = "";//类型的描述 Field(x => x.Id); Field(x => x.Name); Field(x => x.Company); Field(x => x.ReleaseDate); Field(x => x.ActorId); //将枚举的Graph也加入其中,这是在查询Movie中嵌套(学过sql应该懂得嵌套查询)一个MovieRatingEnum //取名movieRating,查询结果context.Source.MovieRating Field<MovieRatingEnum>("movieRating", resolve: context => context.Source.MovieRating); Field<ActorType>("Actor", resolve: context => actorService.GetByIdAsync(context.Source.ActorId)); //作一个小小的测试 Field<StringGraphType>("customString", resolve: context => "1234"); } } }
在Schema文件夹中进行添加查询类MoviesQuery工具
using GraphQL.Types; using GraphStudy.Movies.Services; namespace GraphStudy.Movies.Schema { //查询 class MoviesQuery:ObjectGraphType { public MoviesQuery(IMovieService movieService) { Name = "Query"; //查询全部Movie Field<ListGraphType<MovieType>>("movies", resolve: context => movieService.GetAsyncs()); } } }
在Service文件夹内添加MovieSchema类,之后的增删改查都在此添加服务
using GraphQL; namespace GraphStudy.Movies.Schema { class MovieSchema:GraphQL.Types.Schema { public MovieSchema(IDependencyResolver dependencyResolver, MoviesQuery moviesQuery) { DependencyResolver = dependencyResolver; Query = moviesQuery; } } }
在startup添加服务
services.AddSingleton<ActorType>(); services.AddSingleton<MovieRatingEnum>(); services.AddSingleton<MovieType>(); services.AddSingleton<MoviesQuery>(); services.AddSingleton<MovieSchema>(); services.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));
进入GraphQL的github
https://github.com/graphql-dotnet
找到这个
GraphStudy.Api在NuGet程序包添加这三个包
GraphQL.Server.Transports.AspNetCore
GraphQL.Server.Transports.WebSockets
GraphQL.Server.Ui.Playground
将途中的复制下来
startup所有代码
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using GraphQL; using GraphQL.Server; using GraphQL.Server.Ui.Playground; using GraphStudy.Movies.Schema; using GraphStudy.Movies.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace GraphStudy.Api { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IMovieService, MovieService>(); services.AddSingleton<IActorService, ActorService>(); services.AddSingleton<ActorType>(); services.AddSingleton<MovieRatingEnum>(); services.AddSingleton<MovieType>(); services.AddSingleton<MoviesQuery>(); services.AddSingleton<MovieSchema>(); services.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService)); // Add GraphQL services and configure options services.AddGraphQL(options => { options.EnableMetrics = true; options.ExposeExceptions = true;//是否包容异常,改下 }) .AddWebSockets() // Add required services for web socket support .AddDataLoader(); // Add required services for DataLoader support } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // this is required for websockets support app.UseWebSockets(); // use websocket middleware for ChatSchema at path /graphql app.UseGraphQLWebSockets<MovieSchema>("/graphql"); // use HTTP middleware for ChatSchema at path /graphql app.UseGraphQL<MovieSchema>("/graphql"); //去点另外两个UI,由于咱们刚刚添加的包就是Playground,因此保留这个就行 // use graphql-playground middleware at default url /ui/playground app.UseGraphQLPlayground(new GraphQLPlaygroundOptions()); } } }
运行成功
query{
movies{
id
name
company
movieRating
releaseDate
actorId
customString
}
}
是相似sql的查询语句,GraphQL独有
可参阅官方学习,或者之后教
https://github.com/1045683477/GraphQL- 这是到如今的写的,代码在GitHub上