|
* Q4 Z% o8 j0 Q" o9 z<h2 id="系列导航">系列导航</h2>
" y- [9 T& v9 A/ @<p><a href="https://www.cnblogs.com/code4nothing/p/graphql-net6-0.html">使用Hot Chocolate和.NET 6构建GraphQL应用文章索引</a></p>
( C, }0 S. I6 R8 T0 X9 w0 `5 q<h2 id="需求">需求</h2>
, X( Z9 D: ]& q8 \. i5 n<p>在讨论完GraphQL中的查询需求后,这篇文章我们将演示如何实现GraphQL中的数据添加任务。</p>. _' i4 q+ s3 ^8 r1 U' [
<h2 id="思路">思路</h2>4 z- `3 H/ f1 |$ g# K9 Q4 X7 A! q
<p>在GraphQL中,对数据进行查询使用<code>query</code>,而对于修改数据则需要使用<code>mutation</code>,包括新增和修改数据。Hot Chocolate在使用Mutation的逻辑上和使用Query的基本一致,但是需要根据需要定义用于创建或者更新的数据对象,所以我们直接进行实现。</p>
/ x! ~ v' V: {1 l) T2 D5 P) p<h2 id="实现">实现</h2>
~3 ^) ]' G) S O9 J<p>为了保持简单,我们先定义以下两个类型:</p>
: @' Q) n+ s) V3 b( P% e6 |9 r9 c7 w<pre><code class="language-c#">// 定义新增Post的参数1 r% o* g( E' W+ `5 O7 p5 w: a
public record AddPostInput(string Title, string Author);
; p! z, ^# T( q5 T' Z7 g( g( N3 P5 o# X
// 定义新增Post的返回对象 ?6 X7 c6 g* ~" `% T
public record AddPostPayload(Post Post);2 ]1 ^& H1 V! v( n
</code></pre>0 Z- I: l7 W) W! l- ?
<p>新建<code>Mutation.cs</code>用来定义相关接口:</p>$ z J- J' _9 j% w9 J8 t4 ~/ L
<ul>' J# P; Z `+ y1 U
<li><code>Mutation.cs</code></li>- o6 Q4 D: V% a3 q( y" j% p2 n
</ul>9 A! o, p4 f" X! ?. m+ u5 A
<pre><code class="language-c#">namespace PostGraphi.Api.GraphQL;7 K4 g) w9 M s* M' c7 }
& ?; R* E5 `* hpublic class Mutation
& m( k% A/ V) @$ y3 L{
, `% H9 U" d) ]8 E2 C2 \ public async Task<AddPostPayload> AddPostAsync(AddPostInput input, [Service] IRepository<Post> repository)" C3 |2 k% {; s7 x1 J& J
{
9 ^' o" X4 d. `* a return new AddPostPayload(await repository.AddAsync(new Post8 W; B9 d$ ^4 v5 ~8 n
{7 I' ^6 v$ w4 h" ]" y
Title = input.Title,
# R; L5 K5 L! O! x. Q5 P Author = input.Author1 y/ |; g" Q3 A) g
}));4 D2 D) m3 B" j: x! x
}; M m1 e# g, ?3 Q8 a
}
4 D/ i1 X. G& g! i% o. `* M& q) e" y9 M: x9 Q
</code></pre>( a; {2 v8 h/ u5 C* z7 @. ?
<p>最后在注入服务的地方进行配置:</p>
5 |" Z, y6 g; l+ k" F* @0 K& m y<ul>
& h, m& N" `9 m9 v; A+ n) z<li><code>ProgramExtensions.cs</code></li>, S7 O& A& H+ d
</ul>
# s2 J1 B, }( Z' {7 j0 @' X. R. h5 e% R<pre><code class="language-c#">builder.Services
7 H% b# d% s$ Z5 C+ a, k .AddGraphQLServer()
% `+ G1 b6 K0 _6 E4 l$ P8 B* V .SetPagingOptions(new PagingOptions2 b0 \, X! D x; Y! j S; |. i+ u
{: J1 O4 r4 T, u9 n* G
MaxPageSize = 50,
* d) a- Y% B: C9 v# O" n IncludeTotalCount = true; D( D( K4 c: L, R
})9 t5 g7 |7 B* _% A8 ]
.AddFiltering()" h# {9 R: k! Q* l/ L! H
.AddProjections()
+ C3 G5 p2 n- x( |5 r$ b3 k .AddSorting()% W; d3 V: u! k6 b/ G A
.AddQueryType<Query>()
2 f6 A0 T* e8 `0 V y) ^3 f. \- d .AddMutationType<Mutation>()$ b* E; j7 B# z U# d* B# n/ @3 j
.AddMutationConventions(new MutationConventionOptions
/ n8 E6 r+ Z) x1 h. i {
' O/ G8 w7 v1 p ApplyToAllMutations = true,
& |' v7 a" X4 ?% @+ H InputArgumentName = "input",
- S2 G- m% W4 ?# Q InputTypeNamePattern = "{MutationName}Input",
8 l7 l1 `$ J$ s8 v+ X PayloadTypeNamePattern = "{MutationName}Payload",
. P: r( {/ @& x6 k! O5 `: } PayloadErrorTypeNamePattern = "{MutationName}Error",
& {5 p# |2 w) f: U& ?4 v/ {& B PayloadErrorsFieldName = "errors"9 ?- o" {1 B' W7 L8 x( c% n$ G+ ^
})8 S. g* Z! {3 F& F0 x: @. [7 N
.AddType<PostType>();
3 M' z- G8 N5 G! {! u2 m0 N. E: H' D</code></pre>
+ g( {5 f% w% H& E# ?) [2 z<p>这样就实现了新增Post的需求,下面我们来验证一下。</p>
1 S K* P4 }+ z0 r# r/ v<h2 id="验证">验证</h2>& }5 A8 o; q; {# b3 \0 J1 y# e# ?
<p>启动<code>Api</code>项目,调用接口:</p>
4 `& X1 ]7 i% Z/ z o/ {<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104544617-1400586374.png" ></p>
" d/ Y$ R+ J3 g) `; S, J2 t) _<p>终端的日志输出如下:</p>: V: }" g* d4 P& 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'] v- M! a- ^, W) a7 J K9 a
INSERT INTO "Posts" ("Id", "Abstraction", "Author", "Content", "Created", "CreatedBy", "LastModified", "LastModifiedBy", "Link", "PublishedAt", "Title")8 A4 r) O6 |( b
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10);
5 g& f3 r7 P5 f1 s Y[10:45:15 INF] Executed endpoint 'Hot Chocolate GraphQL Pipeline'
7 M0 x$ p* X6 b7 I, G& c</code></pre>
, r8 y+ v( h2 W5 i* s<p>可以看到新建的Post已经存储到数据库中了,我们可以通过查询接口来获取详情:</p>) a* O! g" }. ~ @
<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104851825-1533915064.png" ></p>4 R; [; k4 L; @
<h2 id="总结">总结</h2>% Y3 U i7 I+ u' q& ]
<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>, k% d# y! f, Q: q. g S
<p>在下一篇文章中,我们通过<code>Mutation</code>对已有数据进行更新。</p>
6 U9 B+ k5 B6 r1 U7 E! b* b
& I0 N) H, L: X! V% w8 Y |
|