Skip to content

Commit f5fbcf6

Browse files
committed
Add Id graph type
1 parent ec9b57d commit f5fbcf6

File tree

8 files changed

+414
-1
lines changed

8 files changed

+414
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Our.Umbraco.GraphQL.Adapters.Types
2+
{
3+
public class GuidGraphType : global::GraphQL.Types.GuidGraphType
4+
{
5+
public GuidGraphType()
6+
{
7+
Name = "Guid";
8+
Description = "Globally Unique Identifier.";
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using GraphQL.Language.AST;
2+
using Our.Umbraco.GraphQL.Types;
3+
4+
namespace Our.Umbraco.GraphQL.Adapters.Types
5+
{
6+
public class IdGraphType : global::GraphQL.Types.IdGraphType
7+
{
8+
public override object ParseValue(object value)
9+
{
10+
var parsed = base.ParseValue(value);
11+
return parsed == null ? (Id?)null : new Id(parsed.ToString());
12+
}
13+
public override object ParseLiteral(IValue value)
14+
{
15+
var parsed = base.ParseLiteral(value);
16+
return parsed == null ? (Id?)null : new Id(parsed.ToString());
17+
}
18+
}
19+
}

src/Our.Umbraco.GraphQL/Adapters/Types/Resolution/TypeRegistry.cs

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55
using GraphQL;
66
using GraphQL.Types;
7+
using Our.Umbraco.GraphQL.Types;
78

89
namespace Our.Umbraco.GraphQL.Adapters.Types.Resolution
910
{
@@ -31,6 +32,7 @@ public TypeRegistry()
3132
Add<DateTimeOffset, DateTimeOffsetGraphType>();
3233
Add<TimeSpan, TimeSpanMillisecondsGraphType>();
3334
Add<Uri, UriGraphType>();
35+
Add<Id, IdGraphType>();
3436
}
3537

3638
public void Add<TType, TGraphType>() where TGraphType : IGraphType

src/Our.Umbraco.GraphQL/Types/Id.cs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.ComponentModel;
3+
4+
namespace Our.Umbraco.GraphQL.Types
5+
{
6+
public struct Id
7+
{
8+
public Id(string value)
9+
{
10+
if (value == null) throw new ArgumentNullException(nameof(value));
11+
if (value == string.Empty)
12+
throw new ArgumentException($"{nameof(value)} cannot be empty.", nameof(value));
13+
14+
Value = value;
15+
}
16+
17+
public string Value { get; }
18+
19+
public T As<T>()
20+
{
21+
var converter = TypeDescriptor.GetConverter(typeof(T));
22+
if (converter.CanConvertFrom(typeof(string)))
23+
return (T)converter.ConvertFrom(Value);
24+
25+
return (T)Convert.ChangeType(Value, typeof(T));
26+
}
27+
28+
public bool Equals(Id other) => Value == other.Value;
29+
30+
public override bool Equals(object obj) => obj is Id other && Equals(other);
31+
32+
public override int GetHashCode() => Value.GetHashCode();
33+
34+
public override string ToString() => Value;
35+
36+
public static bool operator ==(Id left, Id right) => left.Equals(right);
37+
38+
public static bool operator !=(Id left, Id right) => left.Equals(right) == false;
39+
40+
public static implicit operator Id(string value) => new Id(value);
41+
42+
public static implicit operator string(Id id) => id.Value;
43+
44+
public static implicit operator Id?(string value) => string.IsNullOrEmpty(value) ? null : (Id?)new Id(value);
45+
46+
public static implicit operator string(Id? id) => id?.Value;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using FluentAssertions;
3+
using GraphQL.Language.AST;
4+
using Our.Umbraco.GraphQL.Adapters.Types;
5+
using Our.Umbraco.GraphQL.Types;
6+
using Xunit;
7+
8+
namespace Our.Umbraco.GraphQL.Tests.Adapters.Types
9+
{
10+
public class IdGraphTypeTests
11+
{
12+
[Fact]
13+
public void Serialize_WithId_ReturnsValueToString()
14+
{
15+
var idGraphType = new IdGraphType();
16+
var value = "05087385-5095-448D-AA90-4B9AD3C1CA3F";
17+
var id = new Id(value);
18+
19+
var serialized = idGraphType.Serialize(id);
20+
21+
serialized.Should().BeOfType<string>().Which.Should().Be(value);
22+
}
23+
24+
[Fact]
25+
public void Serialize_WithNull_ReturnsNull()
26+
{
27+
var idGraphType = new IdGraphType();
28+
29+
var serialized = idGraphType.Serialize(null);
30+
31+
serialized.Should().BeNull();
32+
}
33+
34+
[Fact]
35+
public void ParseValue_WithValue_ReturnsId()
36+
{
37+
var idGraphType = new IdGraphType();
38+
var value = "BF389740-3EEE-4F2E-BBB1-1C4758B5CE11";
39+
40+
var parsed = idGraphType.ParseValue(value);
41+
42+
parsed.Should().BeOfType<Id>().Which.Value.Should().Be(value);
43+
}
44+
45+
[Fact]
46+
public void ParseValue_WithNull_ReturnsNull()
47+
{
48+
var idGraphType = new IdGraphType();
49+
50+
var parsed = idGraphType.ParseValue(null);
51+
52+
parsed.Should().BeNull();
53+
}
54+
55+
[Fact]
56+
public void ParseLiteral_WithStringValue_ReturnsId()
57+
{
58+
var idGraphType = new IdGraphType();
59+
var value = "BDCE0B55-8D23-47A4-9C66-C94F7D6D8C2A";
60+
61+
var parsed = idGraphType.ParseLiteral(new StringValue(value));
62+
63+
parsed.Should().BeOfType<Id>().Which.Value.Should().Be(value);
64+
}
65+
66+
[Fact]
67+
public void ParseLiteral_WithIntValue_ReturnsId()
68+
{
69+
var idGraphType = new IdGraphType();
70+
var value = 43;
71+
72+
var parsed = idGraphType.ParseLiteral(new IntValue(value));
73+
74+
parsed.Should().BeOfType<Id>().Which.Value.Should().Be(value.ToString());
75+
}
76+
77+
[Fact]
78+
public void ParseLiteral_WithNull_ReturnsNull()
79+
{
80+
var idGraphType = new IdGraphType();
81+
82+
var parsed = idGraphType.ParseLiteral(null);
83+
84+
parsed.Should().BeNull();
85+
}
86+
}
87+
}

test/Our.Umbraco.GraphQL.Tests/Adapters/Types/Resolution/TypeRegistryTests.cs

+5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
using FluentAssertions;
44
using GraphQL.Types;
55
using Our.Umbraco.GraphQL.Adapters.Types.Resolution;
6+
using Our.Umbraco.GraphQL.Types;
67
using Xunit;
8+
using GuidGraphType = Our.Umbraco.GraphQL.Adapters.Types.GuidGraphType;
9+
using IdGraphType = Our.Umbraco.GraphQL.Adapters.Types.IdGraphType;
710

811
namespace Our.Umbraco.GraphQL.Tests.Adapters.Types.Resolution
912
{
@@ -70,6 +73,7 @@ public void Get_TypeIsNotAdded_ReturnsNull()
7073
[InlineData(typeof(DateTimeOffset), typeof(DateTimeOffsetGraphType))]
7174
[InlineData(typeof(TimeSpan), typeof(TimeSpanMillisecondsGraphType))]
7275
[InlineData(typeof(Uri), typeof(UriGraphType))]
76+
[InlineData(typeof(Id), typeof(IdGraphType))]
7377
public void Get_Type_ReturnsRegisteredType(Type type, Type graphType)
7478
{
7579
var typeRegistry = CreateSUT();
@@ -93,6 +97,7 @@ public void Get_Type_ReturnsRegisteredType(Type type, Type graphType)
9397
[InlineData(typeof(DateTime?), typeof(DateTimeGraphType))]
9498
[InlineData(typeof(DateTimeOffset?), typeof(DateTimeOffsetGraphType))]
9599
[InlineData(typeof(TimeSpan?), typeof(TimeSpanMillisecondsGraphType))]
100+
[InlineData(typeof(Id?), typeof(IdGraphType))]
96101
public void Get_NullableType_ReturnsRegisteredType(Type type, Type graphType)
97102
{
98103
var typeRegistry = CreateSUT();

test/Our.Umbraco.GraphQL.Tests/Our.Umbraco.GraphQL.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net472</TargetFramework>
4-
<WarningLevel>3</WarningLevel>
4+
<WarningLevel>2</WarningLevel>
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageReference Include="FluentAssertions" Version="5.8.0" />

0 commit comments

Comments
 (0)