Skip to content

Commit 34e43d0

Browse files
author
github-actions
committed
Documentation update
1 parent e4feac2 commit 34e43d0

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

docs/guides/serialization/index.html

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,32 +166,36 @@ <h1 id="serialization-of-a-graphql-response" style="position:relative;"><a href=
166166
constructor an instance of <code class="language-text">IErrorInfoProvider</code> (see <a href="#error-serialization">Error Serialization</a> below).
167167
The converter can be registered within an instance of <code class="language-text">JsonSerializerOptions</code> so that serializing an
168168
<code class="language-text">ExecutionResult</code> produces the proper output.</p>
169-
<p>To assist, a <code class="language-text">DocumentWriter</code> class is provided with a single method, <code class="language-text">WriteAsync</code>, which
170-
handles constructing the options, registering the converter, and serializing a specified
171-
<code class="language-text">ExecutionResult</code> to a data stream. This class is designed to be registered as a singleton
169+
<p>GraphQL.NET provides two interfaces for serialization:</p>
170+
<ul>
171+
<li><code class="language-text">IGraphQLSerializer</code> - For serializing to/from streams</li>
172+
<li><code class="language-text">IGraphQLTextSerializer</code> - Extends <code class="language-text">IGraphQLSerializer</code> with methods for serializing to/from strings</li>
173+
</ul>
174+
<p>These interfaces are implemented by the <code class="language-text">GraphQLSerializer</code> class in the <code class="language-text">GraphQL.SystemTextJson</code> package and
175+
a similar class in the <code class="language-text">GraphQL.NewtonsoftJson</code> package. These classes are designed to be registered as singletons
172176
within your dependency injection framework, if applicable.</p>
173177
<div class="gatsby-highlight" data-language="csharp"><pre class="language-csharp"><code class="language-csharp"><span class="token comment">// Manually construct an instance</span>
174-
<span class="token class-name"><span class="token keyword">var</span></span> documentWriter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token constructor-invocation class-name">DocumentWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
178+
<span class="token class-name"><span class="token keyword">var</span></span> serializer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token constructor-invocation class-name">GraphQLSerializer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
175179

176180
<span class="token comment">// Or register it within your DI framework (Microsoft DI sample below)</span>
177-
services<span class="token punctuation">.</span><span class="token generic-method"><span class="token function">AddSingleton</span><span class="token generic class-name"><span class="token punctuation">&lt;</span>IDocumentWriter<span class="token punctuation">,</span> DocumentWriter<span class="token punctuation">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
178-
<p>Here is an example of the <code class="language-text">DocumentWriter</code>'s use within the <code class="language-text">Harness</code> sample project:</p>
181+
services<span class="token punctuation">.</span><span class="token generic-method"><span class="token function">AddSingleton</span><span class="token generic class-name"><span class="token punctuation">&lt;</span>IGraphQLTextSerializer<span class="token punctuation">,</span> GraphQLSerializer<span class="token punctuation">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
182+
<p>Here is an example of the serializer's use within a middleware:</p>
179183
<div class="gatsby-highlight" data-language="csharp"><pre class="language-csharp"><code class="language-csharp"><span class="token keyword">private</span> <span class="token keyword">async</span> <span class="token return-type class-name">Task</span> <span class="token function">WriteResponseAsync</span><span class="token punctuation">(</span><span class="token class-name">HttpContext</span> context<span class="token punctuation">,</span> <span class="token class-name">ExecutionResult</span> result<span class="token punctuation">,</span> <span class="token class-name">CancellationToken</span> cancellationToken<span class="token punctuation">)</span>
180184
<span class="token punctuation">{</span>
181185
context<span class="token punctuation">.</span>Response<span class="token punctuation">.</span>ContentType <span class="token operator">=</span> <span class="token string">"application/json"</span><span class="token punctuation">;</span>
182186
context<span class="token punctuation">.</span>Response<span class="token punctuation">.</span>StatusCode <span class="token operator">=</span> <span class="token number">200</span><span class="token punctuation">;</span> <span class="token comment">// OK</span>
183187

184-
<span class="token keyword">await</span> _documentWriter<span class="token punctuation">.</span><span class="token function">WriteAsync</span><span class="token punctuation">(</span>context<span class="token punctuation">.</span>Response<span class="token punctuation">.</span>Body<span class="token punctuation">,</span> result<span class="token punctuation">,</span> cancellationToken<span class="token punctuation">)</span><span class="token punctuation">;</span>
188+
<span class="token keyword">await</span> _serializer<span class="token punctuation">.</span><span class="token function">WriteAsync</span><span class="token punctuation">(</span>context<span class="token punctuation">.</span>Response<span class="token punctuation">.</span>Body<span class="token punctuation">,</span> result<span class="token punctuation">,</span> cancellationToken<span class="token punctuation">)</span><span class="token punctuation">;</span>
185189
<span class="token punctuation">}</span></code></pre></div>
186-
<p>You can also write the result to a string with the <code class="language-text">WriteToStringAsync</code> extension method:</p>
187-
<div class="gatsby-highlight" data-language="csharp"><pre class="language-csharp"><code class="language-csharp"><span class="token class-name"><span class="token keyword">var</span></span> resultText <span class="token operator">=</span> <span class="token keyword">await</span> _documentWriter<span class="token punctuation">.</span><span class="token function">WriteToStringAsync</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
190+
<p>You can also write the result to a string with the <code class="language-text">Serialize</code> method:</p>
191+
<div class="gatsby-highlight" data-language="csharp"><pre class="language-csharp"><code class="language-csharp"><span class="token class-name"><span class="token keyword">var</span></span> resultText <span class="token operator">=</span> _serializer<span class="token punctuation">.</span><span class="token function">Serialize</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div>
188192
<h2 id="error-serialization" style="position:relative;"><a href="#error-serialization" aria-label="error serialization permalink" class="anchor before"><svg aria-hidden="true" focusable="false" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Error Serialization</h2>
189193
<p>The GraphQL spec allows for four properties to be returned within each
190-
error: <code class="language-text">message</code>, <code class="language-text">locations</code>, <code class="language-text">path</code>, and <code class="language-text">extensions</code>. The <code class="language-text">IDocumentWriter</code> implementations
194+
error: <code class="language-text">message</code>, <code class="language-text">locations</code>, <code class="language-text">path</code>, and <code class="language-text">extensions</code>. The <code class="language-text">IGraphQLSerializer</code> implementations
191195
provided for the <a href="https://www.nuget.org/packages/GraphQL.NewtonsoftJson"><code class="language-text">Newtonsoft.Json</code></a> and
192196
<a href="https://www.nuget.org/packages/GraphQL.SystemTextJson"><code class="language-text">System.Text.Json</code></a> packages allow you to control the
193197
serialization of <code class="language-text">ExecutionError</code>s into the resulting json data by providing an <code class="language-text">IErrorInfoProvider</code>
194-
to the constructor of the document writer. The <code class="language-text">ErrorInfoProvider</code> class (default implementation of
198+
to the constructor of the serializer. The <code class="language-text">ErrorInfoProvider</code> class (default implementation of
195199
<code class="language-text">IErrorInfoProvider</code>) contains 5 properties to control serialization behavior:</p>
196200
<ul>
197201
<li><code class="language-text">ExposeExceptionStackTrace</code> when enabled sets the <code class="language-text">message</code> property for errors to equal the</li>
@@ -218,9 +222,9 @@ <h2 id="error-serialization" style="position:relative;"><a href="#error-serializ
218222
<p>and <code class="language-text">data</code> (if enabled). This property defaults to <code class="language-text">true</code>.</p>
219223
<p>For example, to show the stack traces for unhandled errors during development, you might write code like this:</p>
220224
<div class="gatsby-highlight" data-language="csharp"><pre class="language-csharp"><code class="language-csharp"><span class="token preprocessor property">#<span class="token directive keyword">if</span> DEBUG</span>
221-
<span class="token class-name"><span class="token keyword">var</span></span> documentWriter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token constructor-invocation class-name">DocumentWriter</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token constructor-invocation class-name">ErrorInfoProvider</span><span class="token punctuation">(</span>options <span class="token operator">=></span> options<span class="token punctuation">.</span>ExposeExceptionStackTrace <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
225+
<span class="token class-name"><span class="token keyword">var</span></span> serializer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token constructor-invocation class-name">GraphQLSerializer</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token constructor-invocation class-name">ErrorInfoProvider</span><span class="token punctuation">(</span>options <span class="token operator">=></span> options<span class="token punctuation">.</span>ExposeExceptionStackTrace <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
222226
<span class="token preprocessor property">#<span class="token directive keyword">else</span></span>
223-
<span class="token class-name"><span class="token keyword">var</span></span> documentWriter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token constructor-invocation class-name">DocumentWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
227+
<span class="token class-name"><span class="token keyword">var</span></span> serializer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token constructor-invocation class-name">GraphQLSerializer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
224228
<span class="token preprocessor property">#<span class="token directive keyword">endif</span></span></code></pre></div>
225229
<p>You can also write your own implementation of <code class="language-text">IErrorInfoProvider</code>. For instance, you might want to override
226230
the numerical codes provided by GraphQL.NET for validation errors, reveal stack traces

0 commit comments

Comments
 (0)