• GraphQL教程(二) .net Core api 2.1


    一、编辑Schema文件夹内容

    真正的GraphQL教程才刚刚开始QWQ,怕了吧,Schema的文件夹才是关于Graph的

    置于GraphQL是什么,一种用于 API 的查询语言

    GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。

    官网:https://graphql.cn/

    据我所知的是GraphQL由facebook所制造,现阶段github等一群大佬企业也在用这个
    在这里插入图片描述

    在类库schema文件夹中添加三个类MovieType,ActorType,MovieRatingEnum
    其中MovieRatingEnum是枚举类型,其他的你可以一一对应下
    代码看不看的懂,看不懂就对了,不然你也不用学了,直接抄吧,抄个一两遍就有感悟了,到时候再去官网逛逛提升下自己

    首先编写MovieRatingEnum类,直接上代码

    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类,直接上代码

    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));
    

    在这里插入图片描述

    2.调用GraphQL 的UI界面

    进入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上

  • 相关阅读:
    [转] linux下查看文件编码及修改编码
    offset Dimensions 详解
    style属性
    Using NodeLists
    Element Children
    Node、Document关系的探究
    Document、HTMLDocument关系的探究
    BOM Summary P268-P269
    Intervals and Timeouts
    Window Position
  • 原文地址:https://www.cnblogs.com/zuiren/p/10849934.html
Copyright © 2020-2023  润新知