Fork me on GitHub

2021.NET Conf China上的GraphQL

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

1、核心PPT

 

 

 

 

 

 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查询

查询,过滤,排序,扩展

 

 

 继承

 

 

mutation 

 

 

 mutation结果查询

 

 

 

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
 
想要更快更方便的了解相关知识,可以关注微信公众号 
 
posted @ 2022-05-11 20:59  桂素伟  阅读(108)  评论(0编辑  收藏  举报