|
|
9 E% q! o& b. Y% \5 L
<h2 id="系列导航">系列导航</h2>* D! E( B' o, H7 x! {, K. t
<p><a href="https://www.cnblogs.com/code4nothing/p/graphql-net6-0.html">使用Hot Chocolate和.NET 6构建GraphQL应用文章索引</a></p>+ L1 H/ }7 r' e+ e. ~* c
<h2 id="需求">需求</h2>$ d" _2 M3 ?; t+ D* T8 b+ B- `7 }
<p>在讨论完GraphQL中的查询需求后,这篇文章我们将演示如何实现GraphQL中的数据添加任务。</p>
: k2 A- d. A0 O$ j7 ^! ]<h2 id="思路">思路</h2>2 ?6 c1 k9 `6 I. V
<p>在GraphQL中,对数据进行查询使用<code>query</code>,而对于修改数据则需要使用<code>mutation</code>,包括新增和修改数据。Hot Chocolate在使用Mutation的逻辑上和使用Query的基本一致,但是需要根据需要定义用于创建或者更新的数据对象,所以我们直接进行实现。</p>
+ y' n( h/ ]: S9 c9 a6 L/ d- V<h2 id="实现">实现</h2>% ^) M4 M: t3 @/ x! o& l& ~$ S
<p>为了保持简单,我们先定义以下两个类型:</p>
4 D7 y/ T3 P0 Q0 R' O3 B<pre><code class="language-c#">// 定义新增Post的参数5 S; w. n* e6 o: e: N9 X
public record AddPostInput(string Title, string Author);
5 n( K6 i8 e6 {% i. \( b9 n( j4 X6 H+ `# r
// 定义新增Post的返回对象/ t& A5 ?' e* |* F/ G
public record AddPostPayload(Post Post);
6 Z- p& r, f0 y' V, R</code></pre>
/ ?' s2 g5 C4 p0 F8 a& C! ?6 J<p>新建<code>Mutation.cs</code>用来定义相关接口:</p>' l& c4 c2 j, c: Y
<ul>
% K! o, @% n8 e4 R6 p) V) ` W<li><code>Mutation.cs</code></li>
* Q- _0 S1 b. a& e' ~3 k</ul>
6 N3 G" q* z: {" c9 }+ z% f<pre><code class="language-c#">namespace PostGraphi.Api.GraphQL;, {* s+ Y% j- n8 U: B! y$ I0 M
" h3 P# T; o# r
public class Mutation
7 P7 g+ W& v i, q* i{
8 q& c" W3 j/ S( f7 A public async Task<AddPostPayload> AddPostAsync(AddPostInput input, [Service] IRepository<Post> repository)
* {0 l5 n e; W! c* {' v {4 Y6 U v1 k! `8 _) }6 ]# l2 ~
return new AddPostPayload(await repository.AddAsync(new Post* f& x( @% b+ w: r3 U! }! `! A
{% l$ l! n: y# M' m5 b* M
Title = input.Title,+ }- |* q# \( k% t' w D) r1 _
Author = input.Author
6 D* ^( `* e0 n) Z7 E }));
3 W; K+ K6 w9 B+ f8 b( s' x }' @4 w# m3 b" w
}
# @) K& ?3 B3 q7 I. N* k
4 e3 T$ c- N3 X& S: ^9 q! V</code></pre># J; b4 H8 p' r) v% \* b4 \
<p>最后在注入服务的地方进行配置:</p>
% Y7 Q4 w2 q/ c3 `* q<ul>
3 S8 X# \1 q/ Q$ K1 o$ q0 P<li><code>ProgramExtensions.cs</code></li>4 w$ b8 F' q/ G, {: U- z9 c
</ul> L) G f! f) K0 ?
<pre><code class="language-c#">builder.Services8 t% w0 s' _$ m# W8 ]) C* g) b
.AddGraphQLServer(); X9 r$ I! U* w" J) R% l, e
.SetPagingOptions(new PagingOptions
# z: T% q7 |! G0 `3 G$ ~6 V {
* w' q$ d0 w" N/ s7 z. U MaxPageSize = 50,
2 A+ m9 q+ s$ P IncludeTotalCount = true8 |: Y) ~( ]; r$ A/ Z
})0 o) a2 e0 A& \, P" W% w
.AddFiltering()1 ^! U2 t3 f5 G- w, Z
.AddProjections()! }! y$ D2 H6 {6 p% [. Y
.AddSorting()
! r, y( {2 {+ a3 h3 m2 D .AddQueryType<Query>()
# Z( [. I: I; n0 | .AddMutationType<Mutation>()
6 a7 t& w5 K- F# R9 M i0 G* W( w .AddMutationConventions(new MutationConventionOptions
/ X, l% c$ h; k {
7 S9 g& h V/ }3 K ApplyToAllMutations = true,4 o& p1 l! _6 X/ ?4 ]" `6 M; E
InputArgumentName = "input",! h; n5 y1 q# i: l
InputTypeNamePattern = "{MutationName}Input",+ R9 }/ d/ v r. t6 F$ S8 d i
PayloadTypeNamePattern = "{MutationName}Payload",1 z; |2 L7 b: p' N% a
PayloadErrorTypeNamePattern = "{MutationName}Error",) }# j$ ]; O5 }) W& d4 Q
PayloadErrorsFieldName = "errors"
: m1 `3 v7 w! }+ n! F })" D- c+ J' s. k% _/ S Y- [
.AddType<PostType>();
, T, Q& ?, j9 ~2 [, H5 e* S</code></pre>
8 t6 {' o* M) ^+ \<p>这样就实现了新增Post的需求,下面我们来验证一下。</p>
6 b3 a, |, G" V- `. r4 x( z<h2 id="验证">验证</h2>
$ d- b0 Q6 z/ N: B ^<p>启动<code>Api</code>项目,调用接口:</p>7 H0 B4 P* `1 U* o% i+ ^
<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104544617-1400586374.png" ></p>" |% B U& |( W9 m' L+ F
<p>终端的日志输出如下:</p># F0 r! K* l, ]% {
<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']
( d7 D! \/ T+ M G2 w y8 OINSERT INTO "Posts" ("Id", "Abstraction", "Author", "Content", "Created", "CreatedBy", "LastModified", "LastModifiedBy", "Link", "PublishedAt", "Title")! E1 j# Z/ ~( ^7 ?7 K+ ~% ]
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10);+ M! w# _0 k" i& H8 F1 p7 s1 ]! V
[10:45:15 INF] Executed endpoint 'Hot Chocolate GraphQL Pipeline'+ y& }$ z& I: L
</code></pre>
9 H3 f+ ^' |0 m9 P' Z% V' U<p>可以看到新建的Post已经存储到数据库中了,我们可以通过查询接口来获取详情:</p>
: f; d$ o( r' d<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104851825-1533915064.png" ></p>5 X/ j6 g* A L! R5 t0 h; W, f8 h
<h2 id="总结">总结</h2>' |; ^! I& R* v, m: O: ?
<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>/ w; e: M @) g. q( y
<p>在下一篇文章中,我们通过<code>Mutation</code>对已有数据进行更新。</p>3 Q+ @- g1 W% @
) a8 ~+ A5 p7 J- K/ r8 S
|
|