|
* `* S! T8 I( K! j+ e( }<h2 id="系列导航">系列导航</h2>
+ _6 E1 |, @# ~: [' ]/ i<p><a href="https://www.cnblogs.com/code4nothing/p/graphql-net6-0.html">使用Hot Chocolate和.NET 6构建GraphQL应用文章索引</a></p>
* M" ~& I: ? L5 W# i( w<h2 id="需求">需求</h2>( o& ?8 `$ }- u: V" u) N
<p>在讨论完GraphQL中的查询需求后,这篇文章我们将演示如何实现GraphQL中的数据添加任务。</p>+ Y' g9 [6 _" G1 h3 j
<h2 id="思路">思路</h2>
2 F/ d& ]' o/ t3 S. V9 F# F<p>在GraphQL中,对数据进行查询使用<code>query</code>,而对于修改数据则需要使用<code>mutation</code>,包括新增和修改数据。Hot Chocolate在使用Mutation的逻辑上和使用Query的基本一致,但是需要根据需要定义用于创建或者更新的数据对象,所以我们直接进行实现。</p>
* X( x1 u7 T Z k' F<h2 id="实现">实现</h2>
# T7 F0 J$ j5 N; ~<p>为了保持简单,我们先定义以下两个类型:</p>) A1 ^- U- ~1 T1 a, c
<pre><code class="language-c#">// 定义新增Post的参数
% i( v; V' F1 v2 M3 Kpublic record AddPostInput(string Title, string Author);
& q V: F8 u% \# \7 c* h! S& R) H
6 U* H4 O9 s( k, ?/ U5 ?, [+ n// 定义新增Post的返回对象( v( _0 O# x: ]
public record AddPostPayload(Post Post);
1 k# ?4 w4 e' e</code></pre>0 ^" R8 { I' Y. g
<p>新建<code>Mutation.cs</code>用来定义相关接口:</p>3 U2 q! M( v1 E) U3 N, \6 d
<ul>( P7 N% B+ E# {
<li><code>Mutation.cs</code></li>
l# e8 I8 p% [5 E8 q& A, [</ul>. p, Q. K1 k0 H1 y7 H9 ^
<pre><code class="language-c#">namespace PostGraphi.Api.GraphQL;5 j1 H3 G; ?, I7 R- o+ ~+ ]" R2 f
% T; c8 ~# U7 l7 c
public class Mutation9 B3 T) g/ U9 B8 ~; t% |8 Y) K6 F
{+ u( |. V# v' s2 U( n
public async Task<AddPostPayload> AddPostAsync(AddPostInput input, [Service] IRepository<Post> repository)! V& N0 R; }3 G1 t, _8 S' @- l; P
{
; |- Q0 S4 M6 }- F0 O0 t4 {) ?- x( \ return new AddPostPayload(await repository.AddAsync(new Post) v8 J. I6 y0 I+ a
{$ v' Z9 A) k4 C; L+ ]0 U5 y
Title = input.Title,/ x; A0 Y7 w1 |2 N" h
Author = input.Author
: ^8 W; [1 W7 p' x% B }));
+ q. i0 f! o' B7 @" [+ T% F6 j6 d+ i; m; V }+ {. i# J; K& n* M# c
}
0 R$ S, ~) v$ o* z6 D2 `0 G& K- v- v6 u) {( B
</code></pre>& R# S8 i. x4 f; _; I
<p>最后在注入服务的地方进行配置:</p>' H" E! ^' ?( _
<ul>
1 S8 {* R3 V; U3 ?$ z6 m<li><code>ProgramExtensions.cs</code></li>' w* U) S: c- O! F
</ul>
- T/ l- j, n, t2 [<pre><code class="language-c#">builder.Services- `7 D4 G" t3 \, m* Y
.AddGraphQLServer()- K' U1 b3 e4 G) x0 ~8 J
.SetPagingOptions(new PagingOptions
7 i% h- n* D7 _$ u {. I) `6 R% M6 g! e
MaxPageSize = 50,5 K2 c/ l9 B1 T% u x6 T
IncludeTotalCount = true6 x( n$ \0 T/ A3 _' m l' w: O6 ?" L
})
! \) t0 c+ B1 f# g @. U .AddFiltering()
4 V. ?. f1 L; s# E& u .AddProjections()% E3 J) V( S+ X. W$ H
.AddSorting(); ?6 f, y A, ?2 [
.AddQueryType<Query>()" V5 ^0 F: S- {6 Q/ l% C/ X
.AddMutationType<Mutation>()
9 m' Q$ X9 v: V( Q* i .AddMutationConventions(new MutationConventionOptions
# J6 [9 w0 g8 t {. g( P& I/ B- }, C; v
ApplyToAllMutations = true,- L. M5 L. W O4 a) X& e5 A
InputArgumentName = "input",4 \. e2 w& } O1 C5 J! k3 P
InputTypeNamePattern = "{MutationName}Input",9 C/ \" K( v2 b! r( x" C$ U3 r6 ^
PayloadTypeNamePattern = "{MutationName}Payload",$ D) Z7 ~' C0 d7 Z3 d
PayloadErrorTypeNamePattern = "{MutationName}Error",. O9 h2 Q. q& f+ ~0 o) L- o8 r
PayloadErrorsFieldName = "errors"! ?' J, A) k7 @, V! O/ [' u
})
$ V3 Q, r8 H$ \ v .AddType<PostType>();
9 n! I$ q% ^2 H; A# ^</code></pre>
4 h; j2 k$ K* q( A h+ F, O<p>这样就实现了新增Post的需求,下面我们来验证一下。</p>
2 f7 G: N# R- A) a+ Z1 p0 O6 K<h2 id="验证">验证</h2>9 l! \" Y4 S5 C3 q' D
<p>启动<code>Api</code>项目,调用接口:</p>
" G5 J& p, a" r8 u. W g% i<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104544617-1400586374.png" ></p>/ ?$ K( y, h# ?! S$ |
<p>终端的日志输出如下:</p>$ s+ L8 f6 _; p* |( h+ u D
<pre><code class="language-bash">[10:45:15 INF] Executed DbCommand (1ms) [Parameters=[@p0='?' (DbType = Guid), @p1='?', @p2='?' (Size = 13), @p3='?', @p4='?' (DbType = DateTime), @p5='?', @p6='?' (DbType = DateTime), @p7='?', @p8='?', @p9='?' (DbType = DateTime), @p10='?' (Size = 30)], CommandType='Text', CommandTimeout='30']
6 Q. j% S2 }5 ?# g" G8 vINSERT INTO "Posts" ("Id", "Abstraction", "Author", "Content", "Created", "CreatedBy", "LastModified", "LastModifiedBy", "Link", "PublishedAt", "Title"), @, [( Z: ^- Y
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10);
$ [1 A0 J% Z6 c2 C[10:45:15 INF] Executed endpoint 'Hot Chocolate GraphQL Pipeline'
5 |0 h& }- Z0 E. [4 S: u; A, R @</code></pre>
# D+ c5 w6 C5 S" U4 d* w7 P<p>可以看到新建的Post已经存储到数据库中了,我们可以通过查询接口来获取详情:</p>; M" i5 S I/ a. R: z
<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104851825-1533915064.png" ></p>
' H+ D( @4 Y% ^) U<h2 id="总结">总结</h2>
2 `; P y6 r2 Q3 i! K t<p>在本文中我们实现了简单的新增Post操作,这里还有一些涉及到错误处理的内容,我没有在文章中演示,可以参考官方文档 <a href="https://chillicream.com/docs/hotchocolate/defining-a-schema/mutations/#errors">Errors</a>,在自定义异常对象后,有三种方式可以进行错误处理:直接返回异常;使用异常工厂方法;使用构造函数。甚至可以在<code>AggregateExceptions</code>中一次性返回多个异常。基本思路都是通过添加属性<code>[Error(typeof(SomeUserDefinedException))]</code>来实现的。</p>
5 q2 x' z% {# u3 `. E( u<p>在下一篇文章中,我们通过<code>Mutation</code>对已有数据进行更新。</p>
* m6 Q* w0 ^5 b; J; O. y: B" b; u8 G+ L; C& r: m
|
|