Simpler class for string based scalars

This commit is contained in:
Jordan Dominion
2024-10-20 14:54:48 -04:00
parent 3bbd1142d9
commit 8cd94ed1ae
2 changed files with 39 additions and 20 deletions
@@ -1,14 +1,11 @@
using System;
using HotChocolate.Language;
using HotChocolate.Types;
namespace Tgstation.Server.Host.GraphQL.Scalars
{
/// <summary>
/// A <see cref="ScalarType{TRuntimeType, TLiteral}"/> for encoded JSON Web Tokens.
/// A <see cref="StringScalarType"/> for encoded JSON Web Tokens.
/// </summary>
public sealed class JwtType : ScalarType<string, StringValueNode>
public sealed class JwtType : StringScalarType
{
/// <summary>
/// Initializes a new instance of the <see cref="JwtType"/> class.
@@ -19,20 +16,5 @@ namespace Tgstation.Server.Host.GraphQL.Scalars
Description = "Represents an encoded JSON Web Token";
SpecifiedBy = new Uri("https://datatracker.ietf.org/doc/html/rfc7519");
}
/// <inheritdoc />
public override IValueNode ParseResult(object? resultValue)
=> ParseValue(resultValue);
/// <inheritdoc />
protected override string ParseLiteral(StringValueNode valueSyntax)
{
ArgumentNullException.ThrowIfNull(valueSyntax);
return valueSyntax.Value;
}
/// <inheritdoc />
protected override StringValueNode ParseValue(string runtimeValue)
=> new(runtimeValue);
}
}
@@ -0,0 +1,37 @@
using System;
using HotChocolate.Language;
using HotChocolate.Types;
namespace Tgstation.Server.Host.GraphQL.Scalars
{
/// <summary>
/// A <see cref="ScalarType{TRuntimeType, TLiteral}"/> for specialized <see cref="string"/> types.
/// </summary>
public abstract class StringScalarType : ScalarType<string, StringValueNode>
{
/// <summary>
/// Initializes a new instance of the <see cref="StringScalarType"/> class.
/// </summary>
/// <param name="name">The name of the GraphQL scalar type.</param>
public StringScalarType(string name)
: base(name)
{
}
/// <inheritdoc />
public override IValueNode ParseResult(object? resultValue)
=> ParseValue(resultValue);
/// <inheritdoc />
protected override string ParseLiteral(StringValueNode valueSyntax)
{
ArgumentNullException.ThrowIfNull(valueSyntax);
return valueSyntax.Value;
}
/// <inheritdoc />
protected override StringValueNode ParseValue(string runtimeValue)
=> new(runtimeValue);
}
}