飞雪团队

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 14178|回复: 0

使用Hot Chocolate和.NET 6构建GraphQL应用(8) —— 实现Mutate添加数据 ...

[复制链接]

8427

主题

8515

帖子

2万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
27611
发表于 2022-2-12 14:35:41 | 显示全部楼层 |阅读模式
! W8 v! M  `7 f' Y% c
<h2 id="系列导航">系列导航</h2>
7 |" g0 ?3 t7 ?3 \1 H" O<p><a  href="https://www.cnblogs.com/code4nothing/p/graphql-net6-0.html">使用Hot Chocolate和.NET 6构建GraphQL应用文章索引</a></p>  M3 r% {9 Q0 F5 n& x
<h2 id="需求">需求</h2>. f# Z2 H6 s2 H
<p>在讨论完GraphQL中的查询需求后,这篇文章我们将演示如何实现GraphQL中的数据添加任务。</p>. e; u/ {9 P& m' V3 e7 _, ]
<h2 id="思路">思路</h2>
3 c5 L, P8 w0 {0 U; ]+ ^5 n<p>在GraphQL中,对数据进行查询使用<code>query</code>,而对于修改数据则需要使用<code>mutation</code>,包括新增和修改数据。Hot Chocolate在使用Mutation的逻辑上和使用Query的基本一致,但是需要根据需要定义用于创建或者更新的数据对象,所以我们直接进行实现。</p>
3 X& j: s; p$ J) ^<h2 id="实现">实现</h2>
1 N! h7 F2 {( _' _5 C) W" F4 U<p>为了保持简单,我们先定义以下两个类型:</p>5 [6 B6 ~6 T) X+ x: r& R6 @
<pre><code class="language-c#">// 定义新增Post的参数
: l) \6 ^, }& g" U6 N( fpublic record AddPostInput(string Title, string Author);
/ W5 C' c3 @' L+ A) k0 f2 A2 A6 F) M: }( K
// 定义新增Post的返回对象
' N: R! B3 M# r! q2 \* N6 L6 q; A/ Npublic record AddPostPayload(Post Post);
  f1 a" R$ Q( p: x( n5 J/ E7 h</code></pre>
1 O* M% ]  G0 V9 U<p>新建<code>Mutation.cs</code>用来定义相关接口:</p>& M9 u/ Y0 i2 S, z; y5 N
<ul>. d3 U* i  s7 ]3 t7 }
<li><code>Mutation.cs</code></li>- D/ Y0 V5 p! f  G- v. o
</ul>
9 X# ~1 _. l3 j5 C6 n6 e( N) l<pre><code class="language-c#">namespace PostGraphi.Api.GraphQL;0 P' H3 J5 U2 M3 J. ^1 {' u& Q
5 N- I% ^- c3 |1 ^0 ^' m: f
public class Mutation
5 G% _# x3 B. a) N( f* ?0 z. b{
  V$ B. J' o; N% x+ ~4 j    public async Task&lt;AddPostPayload&gt; AddPostAsync(AddPostInput input, [Service] IRepository&lt;Post&gt; repository); t: d8 ]% [+ T
    {4 O: T6 ^+ O, }$ a+ _' v( v
        return new AddPostPayload(await repository.AddAsync(new Post
) x+ k& {0 H8 |0 v        {
, c7 S; Y1 ~0 ~& L+ j! C; `$ j7 }            Title = input.Title,8 c( K. F4 H$ s% e% [7 y
            Author = input.Author
3 k7 {! Z5 ^- O2 X6 q        }));
5 S. g" B' H3 B* a3 Z3 a) X& y    }
# t$ r) }6 ~% X; S; p. o+ d) c; @}) v1 p" e) U' M4 L; G. N

0 j' S9 A, V. O/ y</code></pre>1 F& Z& Z3 N" n/ D
<p>最后在注入服务的地方进行配置:</p>
* K5 ?6 d9 D" E# u2 J<ul>
6 U/ G4 t8 W% d( K) T<li><code>ProgramExtensions.cs</code></li>4 Y5 L( H9 I3 G/ L
</ul>
/ I* O2 n' ^5 Q& G1 I) ~! g<pre><code class="language-c#">builder.Services! |( o. U% w! O& R, h' |8 O
    .AddGraphQLServer()% {2 _, \/ ?) v* J1 x: k4 F/ z* @% |2 l
    .SetPagingOptions(new PagingOptions/ S9 w) p# C4 F( R. B
    {
! _3 Z" n3 z# \/ ^2 u' ~8 ?        MaxPageSize = 50,7 B& D5 m' V. q. J
        IncludeTotalCount = true; H" q3 G) @, V) v* x
    })7 N2 T$ \2 W' |% _. {( q
    .AddFiltering(). B2 T" u5 a2 P  _/ G
    .AddProjections()
) _- _' F% V0 W    .AddSorting()
' A# b# j/ c" W4 F% h$ x0 P    .AddQueryType&lt;Query&gt;()* D& L# J" [* @% X5 J  D1 j
    .AddMutationType&lt;Mutation&gt;()& d( Y8 W( H# {& W. O
    .AddMutationConventions(new MutationConventionOptions7 ~' b1 s5 J5 E0 R; ?0 P- i5 b
    {
8 y0 N" V6 g: ]  N: v7 Q        ApplyToAllMutations = true,
4 I2 u' g9 i( Q+ {/ `& s5 L: ~8 ]        InputArgumentName = "input",' j$ p# V: \. x' v0 h) T' E3 k) Y
        InputTypeNamePattern = "{MutationName}Input",
" E) U( Y! M( J        PayloadTypeNamePattern = "{MutationName}Payload",6 U$ |* d2 a! z; U1 ?/ \
        PayloadErrorTypeNamePattern = "{MutationName}Error",
  E1 c/ ^5 f" }# {* o! n        PayloadErrorsFieldName = "errors"7 {, _: I  {, Q, }& p! {! c
    })
7 \$ I- M! p4 Q6 b" X. v5 e% ^9 _8 y. T    .AddType&lt;PostType&gt;();
/ i+ b) c  e- A6 O</code></pre>6 ]8 T$ ~& e1 ?2 [( G3 x" W
<p>这样就实现了新增Post的需求,下面我们来验证一下。</p>. S1 K' b5 K9 p2 N$ ?; C& F
<h2 id="验证">验证</h2>3 k+ Y* G0 O  Q# V1 W7 V1 r5 M3 a
<p>启动<code>Api</code>项目,调用接口:</p>
' J2 X: f- N8 E3 o! |2 t2 R<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104544617-1400586374.png" ></p>
9 @. x9 X; }' R! k! K: h<p>终端的日志输出如下:</p>
* J3 c4 g! v- F<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']+ p4 p0 Y" H, D- ]9 u
INSERT INTO "Posts" ("Id", "Abstraction", "Author", "Content", "Created", "CreatedBy", "LastModified", "LastModifiedBy", "Link", "PublishedAt", "Title")5 @9 S0 d* g$ y* t, H
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10);
4 s# p  [, C& N5 y% i[10:45:15 INF] Executed endpoint 'Hot Chocolate GraphQL Pipeline'* r* o8 ^2 R' H3 M- e% t3 M
</code></pre>
. X8 f0 F2 f3 k' d0 |3 B" g& D<p>可以看到新建的Post已经存储到数据库中了,我们可以通过查询接口来获取详情:</p>
6 Z- N, d+ J: a. ^1 L<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104851825-1533915064.png" ></p>. I# g8 _; [4 u  e
<h2 id="总结">总结</h2>
  Z+ ^. j. T1 J; 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>! q9 Y8 I" n8 c8 T( ?) L  P$ l, |+ m3 |
<p>在下一篇文章中,我们通过<code>Mutation</code>对已有数据进行更新。</p>
. l: @: D6 |! c+ p+ a: b9 P5 n  f" J' a& p' |9 ~8 K1 E0 H1 a
回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|飞雪团队

GMT+8, 2026-3-27 13:49 , Processed in 0.061787 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表