2021.NET Conf China上的GraphQL

今天给大家分享.NET中的GraphQL,半个小时,没有把所有的代码敲出来,高估了自己的写码速度,所以通过这个文章分享出来。

1、核心PPT

90ed93bdfe992db5d837f115e9145e75.png

c1d9a8d94e68429eec24965fc8e9ded8.png

2、主要代码

using Conf2021GraphQL;
using Conf2021GraphQL.Models;
using Microsoft.EntityFrameworkCore;
using System.Security.Cryptography;var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<ExamContext>(options =>options.UseSqlServer(builder.Configuration.GetConnectionString("ExamDatabase")));builder.Services.AddGraphQLServer().AddQueryType<Query>().AddTypeExtension<ExtendExamPaper>().AddInterfaceType<IDescribe>().AddType<SubjectTypeDescribe>().AddType<QuestionTypeDescribe>().AddMutationType<UserMutation>().AddProjections().AddFiltering().AddSorting();var app = builder.Build();
if (app.Environment.IsDevelopment())
{app.UseSwagger();app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();app.MapGraphQL();app.Run();public class Query
{[Serial][UseProjection][UseFiltering][UseSorting]public IQueryable<ExamPaper> GetExamPapers([Service] ExamContext context) => context.ExamPapers;[Serial][UseProjection][UseFiltering][UseSorting]public IQueryable<User> GetUsers([Service] ExamContext context) => context.Users;[UseProjection][UseFiltering][UseSorting]public IQueryable<IDescribe> GetDescribes(){return new List<IDescribe>{new SubjectTypeDescribe{Describe ="问题科目类型"},new QuestionTypeDescribe{Describe ="试题类型 "},}.AsQueryable();}
}[ExtendObjectType(typeof(ExamPaper))]
public class ExtendExamPaper
{public int Count([Parent] ExamPaper paper) => paper.Questions.Count;
}public interface IDescribe
{string? Describe { get; set; }
}
public class SubjectTypeDescribe : IDescribe
{public string? Describe { get; set; }
}
public class QuestionTypeDescribe : IDescribe
{public string? Describe { get; set; }public string[] Types { get; set; } = new string[] { "单选题", "多选题", "判断题" };
}public class UserMutation
{public async Task<User> AddUser(User user, [Service] ExamContext context, CancellationToken cancellationToken){var password = GetRandomString(8);user.Password = System.Text.Encoding.UTF8.GetString(SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(password + user.Salt)));await context.Users.AddAsync(user);await context.SaveChangesAsync(cancellationToken);user.Password = password;return user;}string GetRandomString(int length){string result = "";for (int i = 0; i < length; i++){char c = (char)new Random(Guid.NewGuid().GetHashCode()).Next(48, 123);result += c;}return result;}
}

3、GraphQL查询

查询,过滤,排序,扩展

67bd0817dfa1fad7f083fc517e73e11c.png

继承

0963f3f5c6194e45e8f4b56eb0a2b99b.png

mutation

36b76bdcee1c839bc2f359814ded0fd7.png

mutation结果查询

099ee9c2b3660a78b5ab88f6517ef6a2.png

4、githtub库:

https://github.com/axzxs2001/Conf2021GraphQL

5、数据库结构

USE [Exam]
GO
/****** Object:  Table [dbo].[Answers]    Script Date: 2021/12/18 23:36:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Answers]([ID] [int] IDENTITY(1,1) NOT NULL,[Sequre] [nvarchar](8) NOT NULL,[Answer] [nvarchar](200) NOT NULL,[IsTrue] [bit] NOT NULL,[QuestionID] [int] NOT NULL,CONSTRAINT [PK_Answers] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[ExamPaperQuestions]    Script Date: 2021/12/18 23:36:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ExamPaperQuestions]([ExamPaperID] [int] NOT NULL,[QuestionID] [int] NOT NULL,CONSTRAINT [PK_ExamPaperQuestions_1] PRIMARY KEY CLUSTERED 
([ExamPaperID] ASC,[QuestionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[ExamPapers]    Script Date: 2021/12/18 23:36:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ExamPapers]([ID] [int] IDENTITY(1,1) NOT NULL,[Title] [nvarchar](200) NOT NULL,[Memo] [nvarchar](1000) NULL,[CreateTime] [datetime2](7) NOT NULL,CONSTRAINT [PK_ExamPapers] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Questions]    Script Date: 2021/12/18 23:36:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Questions]([ID] [int] IDENTITY(1,1) NOT NULL,[Question] [nvarchar](300) NOT NULL,[Score] [float] NOT NULL,[QuestionTypeID] [int] NOT NULL,[SujectTypeID] [int] NOT NULL,CONSTRAINT [PK_Questions] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[QuestionTypes]    Script Date: 2021/12/18 23:36:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[QuestionTypes]([ID] [int] IDENTITY(1,1) NOT NULL,[TypeName] [nvarchar](50) NOT NULL,CONSTRAINT [PK_QuestionTypes] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[SubjectTypes]    Script Date: 2021/12/18 23:36:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SubjectTypes]([ID] [int] IDENTITY(1,1) NOT NULL,[TypeName] [nvarchar](50) NOT NULL,CONSTRAINT [PK_SubjectTypes] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[UserExamAnswers]    Script Date: 2021/12/18 23:36:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserExamAnswers]([ID] [int] IDENTITY(1,1) NOT NULL,[UserExamID] [int] NOT NULL,[AnswerID] [int] NOT NULL,[CreateTime] [datetime] NOT NULL,CONSTRAINT [PK_UserExamAnswers] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[UserExams]    Script Date: 2021/12/18 23:36:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserExams]([ID] [int] IDENTITY(1,1) NOT NULL,[UserID] [int] NOT NULL,[ExamPapgerID] [int] NOT NULL,[BeginTime] [datetime] NOT NULL,[EndTime] [datetime] NOT NULL,CONSTRAINT [PK_UserExams] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Users]    Script Date: 2021/12/18 23:36:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Users]([ID] [int] IDENTITY(1,1) NOT NULL,[UserName] [nvarchar](50) NOT NULL,[Password] [nvarchar](50) NOT NULL,[Salt] [nvarchar](50) NOT NULL,[Name] [nvarchar](50) NOT NULL,[Tel] [varchar](11) NULL,CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ExamPapers] ADD  DEFAULT (getdate()) FOR [CreateTime]
GO
ALTER TABLE [dbo].[UserExamAnswers] ADD  DEFAULT (getdate()) FOR [CreateTime]
GO
ALTER TABLE [dbo].[Answers]  WITH CHECK ADD  CONSTRAINT [FK_Answers_Questions] FOREIGN KEY([QuestionID])
REFERENCES [dbo].[Questions] ([ID])
GO
ALTER TABLE [dbo].[Answers] CHECK CONSTRAINT [FK_Answers_Questions]
GO
ALTER TABLE [dbo].[ExamPaperQuestions]  WITH CHECK ADD  CONSTRAINT [FK_ExamPaperQuestions_ExamPapers] FOREIGN KEY([ExamPaperID])
REFERENCES [dbo].[ExamPapers] ([ID])
GO
ALTER TABLE [dbo].[ExamPaperQuestions] CHECK CONSTRAINT [FK_ExamPaperQuestions_ExamPapers]
GO
ALTER TABLE [dbo].[ExamPaperQuestions]  WITH CHECK ADD  CONSTRAINT [FK_ExamPaperQuestions_Questions] FOREIGN KEY([QuestionID])
REFERENCES [dbo].[Questions] ([ID])
GO
ALTER TABLE [dbo].[ExamPaperQuestions] CHECK CONSTRAINT [FK_ExamPaperQuestions_Questions]
GO
ALTER TABLE [dbo].[Questions]  WITH CHECK ADD  CONSTRAINT [FK_Questions_QuestionTypes] FOREIGN KEY([QuestionTypeID])
REFERENCES [dbo].[QuestionTypes] ([ID])
GO
ALTER TABLE [dbo].[Questions] CHECK CONSTRAINT [FK_Questions_QuestionTypes]
GO
ALTER TABLE [dbo].[Questions]  WITH CHECK ADD  CONSTRAINT [FK_Questions_SubjectTypes] FOREIGN KEY([SujectTypeID])
REFERENCES [dbo].[SubjectTypes] ([ID])
GO
ALTER TABLE [dbo].[Questions] CHECK CONSTRAINT [FK_Questions_SubjectTypes]
GO
ALTER TABLE [dbo].[UserExamAnswers]  WITH CHECK ADD  CONSTRAINT [FK_UserExamAnswers_Answers] FOREIGN KEY([AnswerID])
REFERENCES [dbo].[Answers] ([ID])
GO
ALTER TABLE [dbo].[UserExamAnswers] CHECK CONSTRAINT [FK_UserExamAnswers_Answers]
GO
ALTER TABLE [dbo].[UserExamAnswers]  WITH CHECK ADD  CONSTRAINT [FK_UserExamAnswers_UserExams] FOREIGN KEY([UserExamID])
REFERENCES [dbo].[UserExams] ([ID])
GO
ALTER TABLE [dbo].[UserExamAnswers] CHECK CONSTRAINT [FK_UserExamAnswers_UserExams]
GO
ALTER TABLE [dbo].[UserExams]  WITH CHECK ADD  CONSTRAINT [FK_UserExams_ExamPapers] FOREIGN KEY([ExamPapgerID])
REFERENCES [dbo].[ExamPapers] ([ID])
GO
ALTER TABLE [dbo].[UserExams] CHECK CONSTRAINT [FK_UserExams_ExamPapers]
GO
ALTER TABLE [dbo].[UserExams]  WITH CHECK ADD  CONSTRAINT [FK_UserExams_Users] FOREIGN KEY([UserID])
REFERENCES [dbo].[Users] ([ID])
GO
ALTER TABLE [dbo].[UserExams] CHECK CONSTRAINT [FK_UserExams_Users]
GO

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/293956.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Rust 社区求变,PHP 大旗不倒?

文 | 一君出品 | OSC开源社区&#xff08;ID&#xff1a;oschina2013&#xff09;上月底发生了两件开源语言相关的大事。一是 JetBrains 牵头成立 PHP 基金会&#xff0c;希望能确保语言的长寿和繁荣。二是 Rust 审核团队集体辞职&#xff0c;理由是 Rust 核心团队造成了“寡头…

centos安装vsftp后的虚拟用户设置

2019独角兽企业重金招聘Python工程师标准>>> 安装好vsftpd之后&#xff0c;有的时候需要配置虚拟用户&#xff0c;可以先写一个login.txt&#xff0c;奇数行为用户名&#xff0c;偶数为用户名密码&#xff0c;例如: user1 password1 user2 password2 然后将其生成文…

2021 .NET 开发者峰会顺利在网上落幕,线上直播回看汇总

.NET Conf China 2021 是面向开发人员的社区峰会&#xff0c;基于 .NET Conf 2021的活动&#xff0c;庆祝 .NET 6 的发布和回顾过去一年来 .NET 在中国的发展成果展示&#xff0c;它是由中国各地区的技术社区共同发起举办、知名企业和开源组织联合协办&#xff0c;是年度国内规…

使用 HP 3PAR Peer Persistence 实现 vSphere Metro Storage Cluster (vMSC)

配置图示如下&#xff1a;测试结果方案HP 3PAR StoreServ Storage 系统行为VMware HA 行为单个阵列主机路径故障主机使用备用路径来维护卷访问。未观察到影响单个阵列节点故障主机使用站点上未出现故障的阵列节点的备用路径来维护卷访问。未观察到影响单个存储站点间链路故障无…

java反射--注解的定义与运用以及权限拦截

自定义注解类编写的一些规则: 1. Annotation型定义为interface, 所有的Annotation会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是接口. 2. 参数成员只能用public或默认(default)这两个访问权修饰 3. 参数成员只能用基本类型byte,short,char,int,long,floa…

微信公众平台消息接口开发(2)-封装weixin.class.php

微信公众平台消息接口开发&#xff08;2&#xff09;-封装weixin.class.php 一、封装weixin.class.php 由于微信公众平台的通信使用的是特定格式的XML数据&#xff0c;每次接受和回复都要去做一大堆的数据处理。 我们就考虑在这个基础上做一次封装&#xff0c;weixin.class.php…

这个外挂要上天了!教一千遍都不会的数理技巧,还不如搞懂最根本的概念!

▲ 点击查看数理化的学习对于很多孩子&#xff0c;包括家长都是一个大难题。比如&#xff0c;我们要教孩子认识动物&#xff0c;一般是要给孩子看动物的图片或实体&#xff0c;孩子自然就对这个动物有个认知。要教孩子数字&#xff0c;就会用一件玩具、两个苹果这种和现实有关联…

.NET 6新特性试用 | SDK工作负载

前言为了应对.NET SDK能够支持的程序集项目&#xff08;例如iOS、Android、WASM&#xff09;的不断增长&#xff0c;从.NET 6开始&#xff0c;允许用户仅安装必要的SDK&#xff08;例如ASP.NET Core&#xff09;&#xff0c;而不是一次性安装“完整版”SDK。这一切的基础&#…

“24小时城市图鉴”看人间,每个城市都有属于她的独特记忆!

全世界只有3.14 % 的人关注了爆炸吧知识一个城市的名片是留给人们对一座城最初的印象&#xff0c;无论是车站、居民区&#xff0c;每个城市都有着属于她自己的故事。由央视纪录频道推出的系列纪录片《城市24小时》&#xff08;第一季&#xff09;聚焦郑州、武汉、深圳、成都、厦…

史上最可怕最震撼的46亿年世界历史!看完我跪了....

▲ 点击查看雨果在《笑面人》中曾说过&#xff1a;历史是什么&#xff0c;是过去传到将来的回声&#xff0c;是将来对过去的反映。哈佛大学、哥伦比亚大学、加州大学&#xff0c;这些精英摇篮&#xff0c;都要求所有学生必须选修历史。马云在演讲中说&#xff0c;自己喜欢历史&…

使用 dotnet format 格式化代码

使用 dotnet format 格式化代码Introdotnet-format 在之前的版本是一个独立的命令行工具&#xff0c;在 .NET 6 里已经成为了 SDK 的一部分&#xff0c;我们可以使用&#xff0c;使用 dotnet format 我们可以结合 editorconfig 保持代码风格的一致&#xff0c;我们也可以将 dot…

计算机管理用户和组无法访问,同一工作组无法访问如何解决【详解】

在生活中&#xff0c;不知道你是否和小编一样&#xff1a;计算机会出现各种各样&#xff0c;千奇百怪的问题&#xff0c;其中最常见的是同一工作组无法访问的问题。由于某种不知名的原因&#xff0c;在打开计算机网上邻居时&#xff0c;计算机会弹出讨人厌的提示——无法访问。…

关于Asp.net core配置信息读取的源码分析梳理

概述我们都知道asp.net core配置信息的读取离不开IConfigurationSource和IConfigurationProvider这两个类&#xff0c;ConfigurationSource可以提供一个ConfigurationProvider&#xff0c;然后去读取信息。究竟他们之间有着怎样的千丝万缕&#xff0c;我们一起来看看源码。首先…

MCGS与PLC通讯不上

为什么80%的码农都做不了架构师&#xff1f;>>> 组态串口参数错误 应为偶校验 转载于:https://my.oschina.net/5star/blog/118301

MySQL案例分析--QueryCache

QueryCache联动内容:http://blog.itpub.net/29510932/viewspace-1694922/ -------------------------------------------------------------------------------------------------正文--------------------------------------------------------------------------------------…

Android之PowerManager简介

android.os. PowerManager 通过 PowerManager 类我们可以对设备的电源进行管理。对该类API的使用将影响到电池寿命。只有在必须使用WakeLocks的时候,才使用WakeLocks,且在不使用它的时候要及时释放(release). 默认情况下,当用户对手机有一段时间没有操作后࿰

什么时候告白最合适?

1 电饭煲吐血了&#xff01;&#xff08;素材来源网络&#xff0c;侵删&#xff09;▼2 比冰淇淋掉地上更悲剧的事&#xff08;素材来源网络&#xff0c;侵删&#xff09;▼3 老师的火气又上去了▼4 什么时候最适合告白&#xff08;via.银教授&#xff0c;侵删&#xff09;▼…