|
|
& L# M/ ?# u* Z<h2 id="系列导航">系列导航</h2>
% n$ @( @9 e/ C9 V<p><a href="https://www.cnblogs.com/code4nothing/p/graphql-net6-0.html">使用Hot Chocolate和.NET 6构建GraphQL应用文章索引</a></p> h8 G# v. E( R
<h2 id="需求">需求</h2>& t) A; x& [- }& T
<p>在讨论完GraphQL中的查询需求后,这篇文章我们将演示如何实现GraphQL中的数据添加任务。</p>
: E! d( b8 Z& E: _# X<h2 id="思路">思路</h2>+ x# ?2 ~7 t2 i3 @3 h: l1 n" E
<p>在GraphQL中,对数据进行查询使用<code>query</code>,而对于修改数据则需要使用<code>mutation</code>,包括新增和修改数据。Hot Chocolate在使用Mutation的逻辑上和使用Query的基本一致,但是需要根据需要定义用于创建或者更新的数据对象,所以我们直接进行实现。</p>
1 a( U4 L: l% _/ u5 P<h2 id="实现">实现</h2>
c, W" W* R! \1 l, {! Q<p>为了保持简单,我们先定义以下两个类型:</p>/ }' D2 A3 p% S; Q. x
<pre><code class="language-c#">// 定义新增Post的参数# O7 L" f6 [9 d6 c2 r% c4 c
public record AddPostInput(string Title, string Author);, v* j9 ^6 t' G4 f
; s" ?: f, v7 j O* V+ }// 定义新增Post的返回对象% ^" o3 q% p; D& ?
public record AddPostPayload(Post Post);
6 `8 w; N; A- I; A+ `</code></pre>
8 ` ^5 p; g, I+ d0 Y, R" ?9 X/ v<p>新建<code>Mutation.cs</code>用来定义相关接口:</p>& s. }7 {3 h$ ?; W4 `1 s+ [
<ul>3 `( r5 L$ v% |) w7 r$ G
<li><code>Mutation.cs</code></li>* |. I% q; h0 \6 ]3 J m- z* A6 ]
</ul>0 r4 t7 o( z* U& E, ^, q/ ~
<pre><code class="language-c#">namespace PostGraphi.Api.GraphQL;( \3 k* [0 Z. Q2 X+ ]/ d
! w- U" c/ p: P% ~public class Mutation
2 R; o, d* G. x! H, j. N{
4 J! L* T% b/ f# A6 W public async Task<AddPostPayload> AddPostAsync(AddPostInput input, [Service] IRepository<Post> repository)
. i2 e6 i& T; w5 n$ s9 e/ H! K8 f' _ {# k% S/ j3 p$ X# @2 U
return new AddPostPayload(await repository.AddAsync(new Post
6 P) r$ h+ x4 j k {* z, u* D* [" |( N/ o
Title = input.Title,
$ G) e/ ]3 k/ r4 C" D9 Z- D Author = input.Author
& H* X" T _5 C5 B7 X5 i }));: {; ?% E7 }* W
}% _% ~# g t# g/ B
}; Y/ [4 k+ E7 h
* R* e* {) K. v {9 J5 w( X
</code></pre>5 J; w- Y! E5 G
<p>最后在注入服务的地方进行配置:</p>7 u0 }& Z% c8 l3 O8 p
<ul>" d. A+ o" ^2 N, g; z. J$ F
<li><code>ProgramExtensions.cs</code></li>
/ Z# n( u+ e' G4 q) @</ul>
/ ^& Q& q! U. _' r7 f<pre><code class="language-c#">builder.Services
1 Z! p4 ]- D5 B .AddGraphQLServer() D( b7 S2 w. B2 W
.SetPagingOptions(new PagingOptions' Y* m4 _; G% u. A9 @% p$ P5 ^
{
9 c; `2 F% j/ \ MaxPageSize = 50,
/ C( N, N( v" m: s5 P' M: h* g IncludeTotalCount = true% q0 K5 h1 e' e4 m5 Q+ H( |
})
9 f- U0 r3 a3 m8 V .AddFiltering()
9 C; s$ _: H& b% s2 m- z5 ?4 k .AddProjections()1 M' K0 q! w, q6 d3 o# t6 Q6 r( i
.AddSorting()
$ {$ p1 @2 ^( J4 z4 P0 {5 Q4 ? .AddQueryType<Query>()- @, f P+ A$ d& @% ?5 c2 W
.AddMutationType<Mutation>()
# [) J$ J" v( Y .AddMutationConventions(new MutationConventionOptions
% p5 J5 r0 D/ B" L {# }% r) g% g2 L
ApplyToAllMutations = true,7 i6 Y0 M) u3 [' a6 {& I
InputArgumentName = "input",
6 a5 W8 c5 M9 E- g& @5 J! s% K InputTypeNamePattern = "{MutationName}Input",
7 B: D: P6 z: W$ H PayloadTypeNamePattern = "{MutationName}Payload",* ? x3 ]* G W7 w! J
PayloadErrorTypeNamePattern = "{MutationName}Error",9 }$ A% x$ \& c! }7 L
PayloadErrorsFieldName = "errors"% B2 s, z; m- T* x( E, `" H
})9 t$ z/ h1 |& ^7 W; s
.AddType<PostType>();
) w6 A, Z- b/ D</code></pre>
4 h# c6 z+ e8 ?) V<p>这样就实现了新增Post的需求,下面我们来验证一下。</p>
0 D* d) y8 R) f5 J$ X) Q<h2 id="验证">验证</h2>
# v0 c6 L0 q* j' _: m) o4 Y L+ o<p>启动<code>Api</code>项目,调用接口:</p>7 B! Z9 e4 R& l% q+ b' i' k
<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104544617-1400586374.png" ></p>
/ x$ r5 d4 k& ^2 }0 O<p>终端的日志输出如下:</p>5 C. F8 F: j0 d0 M$ q3 i' t( k
<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']
; y4 ~2 s; R% } ~5 ^: {INSERT INTO "Posts" ("Id", "Abstraction", "Author", "Content", "Created", "CreatedBy", "LastModified", "LastModifiedBy", "Link", "PublishedAt", "Title")0 h1 |) r4 g0 f) a$ o9 w2 A5 C
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10);, L8 ^+ ?+ [" {
[10:45:15 INF] Executed endpoint 'Hot Chocolate GraphQL Pipeline'
3 T# N) `, O- h. t0 }</code></pre>
# S; e6 H% h8 C, g& h* _0 J<p>可以看到新建的Post已经存储到数据库中了,我们可以通过查询接口来获取详情:</p>
' T, ^! m* W+ B1 h* {: [<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104851825-1533915064.png" ></p>9 h! ?' c# u3 D
<h2 id="总结">总结</h2>4 n& w" j' Z" e% g
<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>
6 h2 `) E) P' a5 B3 |3 X<p>在下一篇文章中,我们通过<code>Mutation</code>对已有数据进行更新。</p>
* Z/ `: u3 o, w; k! ~9 \ I
: n+ R; _- @' ~$ S |
|