Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bc73cad

Browse filesBrowse files
Support for UShort as a Scalar (#76)
* added support for ushort as a scalar
1 parent 30870a3 commit bc73cad
Copy full SHA for bc73cad

File tree

Expand file treeCollapse file tree

6 files changed

+66
-0
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+66
-0
lines changed

‎src/graphql-aspnet/Constants.cs

Copy file name to clipboardExpand all lines: src/graphql-aspnet/Constants.cs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public static class ScalarNames
203203
public const string URI = "Uri";
204204
public const string ID = "ID";
205205
public const string SHORT = "Short";
206+
public const string USHORT = "UShort";
206207

207208
#if NET6_0_OR_GREATER
208209
public const string DATEONLY = "DateOnly";

‎src/graphql-aspnet/Defaults/DefaultScalarTypeProvider.cs

Copy file name to clipboardExpand all lines: src/graphql-aspnet/Defaults/DefaultScalarTypeProvider.cs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public DefaultScalarTypeProvider()
5858
this.RegisterScalar(typeof(UriScalarType));
5959
this.RegisterScalar(typeof(GraphIdScalarType));
6060
this.RegisterScalar(typeof(ShortScalarType));
61+
this.RegisterScalar(typeof(UShortScalarType));
6162

6263
#if NET6_0_OR_GREATER
6364
this.RegisterScalar(typeof(DateOnlyScalarType));

‎src/graphql-aspnet/Response/BaseResponseWriter.cs

Copy file name to clipboardExpand all lines: src/graphql-aspnet/Response/BaseResponseWriter.cs
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ protected virtual void WriteLeafValue(Utf8JsonWriter writer, object value, bool
213213
writer.WriteNumberValue(sh);
214214
break;
215215

216+
case ushort ush:
217+
writer.WriteNumberValue(ush);
218+
break;
219+
216220
#if NET6_0_OR_GREATER
217221
case DateOnly dateOnly:
218222
this.WritePreEncodedStringValue(writer, dateOnly.ToRfc3339String());
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// *************************************************************
2+
// project: graphql-aspnet
3+
// --
4+
// repo: https://github.com/graphql-aspnet
5+
// docs: https://graphql-aspnet.github.io
6+
// --
7+
// License: MIT
8+
// *************************************************************
9+
10+
namespace GraphQL.AspNet.Schemas.TypeSystem.Scalars
11+
{
12+
using System;
13+
using System.Diagnostics;
14+
using GraphQL.AspNet.Common;
15+
using GraphQL.AspNet.Execution.Exceptions;
16+
using GraphQL.AspNet.Parsing.SyntaxNodes;
17+
18+
/// <summary>
19+
/// A graph type representing a 16-bit unsigned integer.
20+
/// </summary>
21+
[DebuggerDisplay("SCALAR: {Name}")]
22+
public sealed class UShortScalarType : BaseScalarType
23+
{
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="UShortScalarType"/> class.
26+
/// </summary>
27+
public UShortScalarType()
28+
: base(Constants.ScalarNames.USHORT, typeof(ushort))
29+
{
30+
this.Description = $"A 16-bit unsigned integer. (Min: {ushort.MinValue}, Max: {ushort.MaxValue})";
31+
this.OtherKnownTypes = new TypeCollection(typeof(ushort?));
32+
}
33+
34+
/// <inheritdoc />
35+
public override TypeCollection OtherKnownTypes { get; }
36+
37+
/// <inheritdoc />
38+
public override ScalarValueType ValueType => ScalarValueType.Number;
39+
40+
/// <inheritdoc />
41+
public override object Resolve(ReadOnlySpan<char> data)
42+
{
43+
if (ushort.TryParse(data.ToString(), out var i))
44+
return i;
45+
46+
throw new UnresolvedValueException(data, typeof(ushort));
47+
}
48+
}
49+
}

‎src/tests/graphql-aspnet-tests/Schemas/QueryLanguageGeneratorTests.cs

Copy file name to clipboardExpand all lines: src/tests/graphql-aspnet-tests/Schemas/QueryLanguageGeneratorTests.cs
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ private static void SetupTestData()
6666
_testValues.Add(((short)-5000, "-5000"));
6767
_testValues.Add(((short)0, "0"));
6868

69+
_testValues.Add(((ushort)5, "5"));
70+
_testValues.Add(((ushort)5000, "5000"));
71+
_testValues.Add(((ushort)0, "0"));
72+
6973
_testValues.Add((5L, "5"));
7074
_testValues.Add((-5L, "-5"));
7175
_testValues.Add((5000000L, "5000000"));

‎src/tests/graphql-aspnet-tests/Schemas/ScalarTests.cs

Copy file name to clipboardExpand all lines: src/tests/graphql-aspnet-tests/Schemas/ScalarTests.cs
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ public class ScalarTests
9999
new object[] { typeof(ShortScalarType), "\"1\"", null, true },
100100
new object[] { typeof(ShortScalarType), "\"abc\"", null, true },
101101

102+
new object[] { typeof(UShortScalarType), "0", 0, false },
103+
new object[] { typeof(UShortScalarType), "1", 1, false },
104+
new object[] { typeof(UShortScalarType), "10000", 10000, false },
105+
new object[] { typeof(UShortScalarType), "-1", null, true },
106+
new object[] { typeof(UShortScalarType), "\"1\"", null, true },
107+
new object[] { typeof(UShortScalarType), "\"abc\"", null, true },
108+
102109
#if NET6_0_OR_GREATER
103110
new object[] { typeof(DateOnlyScalarType), "\"2021-11-12\"", new DateOnly(2021, 11, 12), false },
104111
new object[] { typeof(DateOnlyScalarType), "\"2021-10-21T11:12:13+00:00\"", new DateOnly(2021, 10, 21), false },

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.