|
|
: m+ i' D2 F6 |7 I) W5 b* s" a<h2 id="系列导航">系列导航</h2>( z, @* n6 r% a) \1 a9 c& t
<p><a href="https://www.cnblogs.com/code4nothing/p/graphql-net6-0.html">使用Hot Chocolate和.NET 6构建GraphQL应用文章索引</a></p>9 v5 }1 K( q4 ~
<h2 id="需求">需求</h2>
4 `6 ~6 K1 M$ ^( c+ ? [. E1 x/ a<p>在讨论完GraphQL中的查询需求后,这篇文章我们将演示如何实现GraphQL中的数据添加任务。</p># n3 h6 C5 c c
<h2 id="思路">思路</h2>/ p: U0 M; Q0 } t
<p>在GraphQL中,对数据进行查询使用<code>query</code>,而对于修改数据则需要使用<code>mutation</code>,包括新增和修改数据。Hot Chocolate在使用Mutation的逻辑上和使用Query的基本一致,但是需要根据需要定义用于创建或者更新的数据对象,所以我们直接进行实现。</p>2 b3 u5 t( t4 Z+ R
<h2 id="实现">实现</h2>, Y6 V+ {' ]8 a8 {. g
<p>为了保持简单,我们先定义以下两个类型:</p>% V7 s2 d3 `2 Y# Q
<pre><code class="language-c#">// 定义新增Post的参数8 F! k! N; v( {# |" H; a* z2 M+ ]' ~4 c
public record AddPostInput(string Title, string Author);. d% Y/ g; o. q3 \
) ], @1 O) y' a: h// 定义新增Post的返回对象
C8 d5 d ]- A }public record AddPostPayload(Post Post);' }" }: F4 K/ Z0 f' l, i& `$ w w/ `. z
</code></pre>
8 g7 F5 A1 S+ D* m* [& H<p>新建<code>Mutation.cs</code>用来定义相关接口:</p>
3 [2 M* C! n, L5 d; m& V/ v6 b3 m<ul>4 J4 a) \: J E& @9 g% V% S
<li><code>Mutation.cs</code></li>5 i3 `" k; @: }2 M7 m. {
</ul>
& F4 W) Y% U# ?& M6 l<pre><code class="language-c#">namespace PostGraphi.Api.GraphQL;( i3 H8 r% u) P& J% ?
' O7 x' [" o. D' Z' t
public class Mutation$ G! ~1 h; N* n9 W* e& S9 w: W
{( s2 P5 r0 w8 K. k/ N7 P
public async Task<AddPostPayload> AddPostAsync(AddPostInput input, [Service] IRepository<Post> repository)
1 `4 w& _5 ^7 g { s) v+ t; K. _
return new AddPostPayload(await repository.AddAsync(new Post
1 j z6 t# k2 j3 t+ Q {
; Z0 l4 ?+ j3 e Title = input.Title,2 F. S+ |; l* p5 R4 Z
Author = input.Author6 N* e' ~( q- O6 P! Y# V
}));3 J5 x t% S1 s! z5 s9 `
}+ s0 A/ o/ [: l
}
# a' a) P2 C* h. p# k6 n
7 u0 O6 [6 i9 F7 r1 O& H" S</code></pre>/ M- U2 Y* V5 B3 S; e& p
<p>最后在注入服务的地方进行配置:</p>! q- x% H% p2 }
<ul>, A4 K* n& X/ \; Z( ]
<li><code>ProgramExtensions.cs</code></li>& T& }/ p* U; l: A# [* a
</ul>7 u4 g3 A( a( k0 {( v) x1 J* x3 Z$ r7 ^, m
<pre><code class="language-c#">builder.Services
, h; q U# Z9 b. ]/ f0 j .AddGraphQLServer()
8 P+ L3 o* A5 h1 ] .SetPagingOptions(new PagingOptions
2 l9 Y1 `# `* p4 I {
- g3 J- h+ D$ a) l( d; X! K' Y MaxPageSize = 50,2 R I+ n8 |2 h' n, B3 O
IncludeTotalCount = true7 q, P" t- v4 k: p
})# D0 T0 r5 I/ i) A) d3 s
.AddFiltering()- r! M- I( {" ~
.AddProjections()7 N/ r3 |/ m/ [6 v% v) g$ ~% y
.AddSorting(). M& a6 c1 a7 t5 p, F a
.AddQueryType<Query>()
' p. `/ c& y: [# K .AddMutationType<Mutation>()1 M. P' @. c4 `
.AddMutationConventions(new MutationConventionOptions
: `& R/ r) c* a: Z3 v {
0 T0 B3 k& I; K" W( v ApplyToAllMutations = true,
) Q& c- k( t' _+ f8 v$ ^7 v5 Z InputArgumentName = "input",
) [1 o. ^/ ` ~- ~ InputTypeNamePattern = "{MutationName}Input",( K& m0 [: {) d0 D( ]
PayloadTypeNamePattern = "{MutationName}Payload",
0 n, z6 b7 {% }" p, \/ C PayloadErrorTypeNamePattern = "{MutationName}Error",
0 v% S+ c6 a0 Y3 n9 U v6 F PayloadErrorsFieldName = "errors"
1 e' G' B2 z1 D( p3 {9 g })+ P5 n; Q8 R$ m2 N) I
.AddType<PostType>();
3 n- I$ i: G" t) B" k</code></pre>
5 F. Z* f! N% ^7 U<p>这样就实现了新增Post的需求,下面我们来验证一下。</p>2 O1 [5 C2 S6 \
<h2 id="验证">验证</h2>3 M3 x9 c5 I/ `: a5 E
<p>启动<code>Api</code>项目,调用接口:</p>" _0 H9 d w. ^; U
<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104544617-1400586374.png" ></p>
) C5 l6 e. j1 S; Q$ n2 _0 p<p>终端的日志输出如下:</p>
3 [- l' ^" y( m; @7 W% Q$ J<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']
7 @* Y8 R, S0 y7 w _. sINSERT INTO "Posts" ("Id", "Abstraction", "Author", "Content", "Created", "CreatedBy", "LastModified", "LastModifiedBy", "Link", "PublishedAt", "Title")( v6 `. B9 |2 }/ J3 R
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10);
3 u& i# U) f& w% [% V[10:45:15 INF] Executed endpoint 'Hot Chocolate GraphQL Pipeline'
' Y" k# I) m3 K' t</code></pre>4 g4 m4 z. e4 F0 `& h) K
<p>可以看到新建的Post已经存储到数据库中了,我们可以通过查询接口来获取详情:</p>; Z* O1 Z' G$ D' W
<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104851825-1533915064.png" ></p>
2 u5 q* E7 V- |<h2 id="总结">总结</h2> p( R3 a+ l& G% B1 O, G( V
<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>
* H, s4 e8 L8 K3 \/ e. g# j<p>在下一篇文章中,我们通过<code>Mutation</code>对已有数据进行更新。</p>
. U# A3 B: n) q, G( j8 k: ~) j
5 o4 L, W# v0 D1 Z |
|