飞雪团队

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

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

[复制链接]

8115

主题

8203

帖子

2万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
26675
发表于 2022-2-12 14:35:41 | 显示全部楼层 |阅读模式

  m1 f4 F! F  i$ c- j# W1 U<h2 id="系列导航">系列导航</h2>- T4 a( z3 i3 k/ A
<p><a  href="https://www.cnblogs.com/code4nothing/p/graphql-net6-0.html">使用Hot Chocolate和.NET 6构建GraphQL应用文章索引</a></p>
. m! |  e$ h9 |<h2 id="需求">需求</h2>
% t5 Z0 w% s" i: A: W<p>在讨论完GraphQL中的查询需求后,这篇文章我们将演示如何实现GraphQL中的数据添加任务。</p>" y, J. c2 t* h% P6 N. ^) l. @" i
<h2 id="思路">思路</h2>
/ v/ ~7 M" W/ `' ^& I2 ^; Y<p>在GraphQL中,对数据进行查询使用<code>query</code>,而对于修改数据则需要使用<code>mutation</code>,包括新增和修改数据。Hot Chocolate在使用Mutation的逻辑上和使用Query的基本一致,但是需要根据需要定义用于创建或者更新的数据对象,所以我们直接进行实现。</p>
: R& a$ a9 E- u$ R<h2 id="实现">实现</h2>" H, w- g9 b& ]7 e+ S  C& u
<p>为了保持简单,我们先定义以下两个类型:</p>& K* d% ~$ W" Z8 E! d% J
<pre><code class="language-c#">// 定义新增Post的参数
9 S; Y7 z; Z, n5 E5 r% ~, apublic record AddPostInput(string Title, string Author);8 G6 Q; E* B  f1 e
$ i0 q5 \" J( @. C8 f
// 定义新增Post的返回对象
1 b0 }- S5 A6 @9 {9 ?public record AddPostPayload(Post Post);
# N' ^6 X8 \; M0 g! J</code></pre>
+ a( [% `6 W/ [- B<p>新建<code>Mutation.cs</code>用来定义相关接口:</p>
' g. R* R& `. D: w<ul>
# ?( w, E: \9 K6 U- l<li><code>Mutation.cs</code></li>+ n3 ?7 [- k- {$ Y' G, C
</ul>
( x8 I/ x' M+ ]* ^; L4 V# h( P<pre><code class="language-c#">namespace PostGraphi.Api.GraphQL;; w+ x+ j7 s2 V
) b1 X6 V, O6 _, E  Y
public class Mutation) z6 o& ~5 {& @" T# \
{
. @, F9 {2 H5 T/ O0 O    public async Task&lt;AddPostPayload&gt; AddPostAsync(AddPostInput input, [Service] IRepository&lt;Post&gt; repository)
/ N- y; x( }" _; z) Y5 K, e1 b    {
# g) x2 ?5 u. H- O        return new AddPostPayload(await repository.AddAsync(new Post
( y/ m. I) h$ G- d        {
- m& ]! F+ ]/ B1 E            Title = input.Title,5 ^$ P: s* t2 H" W+ ^* L
            Author = input.Author1 ]1 s/ J5 K* }2 w% `
        }));
# P0 j% s9 Q: j$ N% I    }
) Y: g7 f5 L6 J, l}
+ z7 ?4 i% t1 D. ^" R- e' j8 C9 H; P1 I- h1 X
</code></pre>% V& m$ E. E5 k' @( @$ N3 M
<p>最后在注入服务的地方进行配置:</p>  Y! ~7 ]7 A2 c6 c- h
<ul>
. S" Q1 S! ~- n<li><code>ProgramExtensions.cs</code></li>) G" v) y! h1 l) ?
</ul>
* ~. \6 i7 {& r<pre><code class="language-c#">builder.Services
* E% W) }3 Q0 j* Q, ]; T& k    .AddGraphQLServer()
. q% O9 l# V. A5 j    .SetPagingOptions(new PagingOptions
5 Z0 g# a% z* d* c; D    {3 B% b. H" x: y& O( {8 N
        MaxPageSize = 50,
  l& E% M' v9 f        IncludeTotalCount = true
8 r% F; i, p0 \: s' k    })
9 J& Z" C& M( `: j6 i    .AddFiltering()
5 b+ V- U$ P! W# f5 ^9 a    .AddProjections()
. K$ y* R' ~' o' V, A; h    .AddSorting()
8 N: |: z! C7 g( Z    .AddQueryType&lt;Query&gt;()
" C8 d. d, I+ U0 i* X) X    .AddMutationType&lt;Mutation&gt;()
% S  I) K# n  ]2 H) {9 H: |    .AddMutationConventions(new MutationConventionOptions
- H' i$ g2 L: C' ?    {
$ K/ @1 A  c5 d  I+ ?        ApplyToAllMutations = true,- e+ ?! B1 z% O, E0 w' A8 j
        InputArgumentName = "input",
+ |- e  r, `2 o& Q# ^/ S        InputTypeNamePattern = "{MutationName}Input",! c+ r% }! P" l4 S/ I
        PayloadTypeNamePattern = "{MutationName}Payload",
9 ?4 d" @+ |3 T1 b- f, b" I        PayloadErrorTypeNamePattern = "{MutationName}Error",. N" n- H' t8 V
        PayloadErrorsFieldName = "errors") c- h3 z. o  Y5 N2 c; L# t
    })
. G$ }0 u2 s+ Q& O8 B    .AddType&lt;PostType&gt;();
+ [9 C; X7 h# ?2 R5 V5 |4 P</code></pre>
$ A* s5 D$ p3 R  D0 `" X' \<p>这样就实现了新增Post的需求,下面我们来验证一下。</p>. P3 E/ t+ ]% O
<h2 id="验证">验证</h2>
5 M5 M% I1 b+ H4 L; V" d! D<p>启动<code>Api</code>项目,调用接口:</p>
9 K2 C& x; Y) i! P% ]- u<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104544617-1400586374.png" ></p>
) m& C  v) {( A( O<p>终端的日志输出如下:</p>
" V5 ?+ S8 B. o% R<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']3 L0 Z) G2 c- B& l* ?
INSERT INTO "Posts" ("Id", "Abstraction", "Author", "Content", "Created", "CreatedBy", "LastModified", "LastModifiedBy", "Link", "PublishedAt", "Title")& j. T9 @  N1 [, V6 R4 K# W
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10);
  O6 t" K1 a' q4 j% X  R- s[10:45:15 INF] Executed endpoint 'Hot Chocolate GraphQL Pipeline'
2 u; H8 n7 e+ C# A; O5 o</code></pre>
4 N8 F4 o& Z4 C- i6 c- a<p>可以看到新建的Post已经存储到数据库中了,我们可以通过查询接口来获取详情:</p>( v0 F) f# i6 v! Y" _2 q
<p><img src="https://img2022.cnblogs.com/blog/2487237/202202/2487237-20220211104851825-1533915064.png" ></p>
. X& o0 G: n9 B<h2 id="总结">总结</h2>/ d" |3 v) g% c0 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>" t+ U' P- D* ]6 G+ j
<p>在下一篇文章中,我们通过<code>Mutation</code>对已有数据进行更新。</p>: j) k# {4 R7 C! Z: d

$ x$ L5 l+ `9 A/ C& x
回复

使用道具 举报

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

本版积分规则

手机版|飞雪团队

GMT+8, 2025-12-20 15:03 , Processed in 0.065531 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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