ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

GraphQL:DataLoader的神奇

2022-02-01 16:01:17  阅读:175  来源: 互联网

标签:Microsoft DataLoader System id GraphQL student using public 神奇


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

  Rest API中,一个api,通过参数,只能获取这个参数范围内的数据,如果想获取别的参数范围内数据,就得再次调用,比如GetStudent(id),想获取多个Student,就得多次调用,把替换id就可以,GraphQL的其中优势就是,在一次请求中,返回你所要的数据,减少请求,就像请求聚合。

using GreenDonut;
using HotChocolate;
using HotChocolate.Data;
using HotChocolate.Fetching;
using HotChocolate.Resolvers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace GraphQLDemo04
{
    public class Startup
    {
        /// <summary>
        /// Persons只是假装数据库
        /// </summary>

        public static List<Result<Student>> Persons = new List<Result<Student>>();

        public void ConfigureServices(IServiceCollection services)
        {
            for (int i = 0; i < 200; i++)
            {
                //NameCreater.GetFullName只是随机生成人的名称
                var student = new Student { Id = i, Tel = "13453467" + i.ToString("D3"), Name = NameCreater.GetFullName() };
                var result = Result<Student>.Resolve(student);
                Persons.Add(result);
            }

            services
                .AddGraphQLServer()
                .AddQueryType<Query>()
                ;
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGraphQL();
            });
        }
    }

    /// <summary>
    /// 查询类
    /// </summary>
    public class Query
    {
        public Task<Student> GetStudent(int id, [DataLoader] StudentDataLoader loader)
        {
            return loader.LoadAsync(id);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class StudentDataLoader : DataLoaderBase<int, Student>
    {
        public StudentDataLoader(IBatchScheduler scheduler) : base(scheduler)
        {
        }
        protected override ValueTask<IReadOnlyList<Result<Student>>> FetchAsync(IReadOnlyList<int> keys, CancellationToken cancellationToken)
        {
            return new ValueTask<IReadOnlyList<Result<Student>>>(Startup.Persons.Where(s => keys.Contains(s.Value.Id)).ToList());
        }

    }
    /// <summary>
    /// 学生
    /// </summary>
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Tel { get; set; }
    }
}

代码中的StudentDataLoader就是来完成请求聚合的。
请求条件如下,请求四个id=10,id=20,id=30,id=40

{
    a:student(id:10){
        id
        name
        tel
        }
    b:student(id:20){
        id
        name
        tel
        }
    c:student(id:30){
        id
        name
        tel
        }
    d:student(id:40){
        id
        name
        tel
        }
}

返回结果:

 

   调置断点,可以看到监视里的keys是四个元素,一次请求来的,DataLoader会把条件id分离出来提供给我们,方便进行后端数据查询,这也是便是DataLoader的神奇之处。

 

 

  想要更快更方便的了解相关知识,可以关注微信公众号   

 

 

标签:Microsoft,DataLoader,System,id,GraphQL,student,using,public,神奇
来源: https://www.cnblogs.com/axzxs2001/p/15859140.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有