Skip to content

Commit 79867e9

Browse files
committed
Add HTML Graph Type
1 parent 895af6d commit 79867e9

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Web;
2+
using GraphQL.Language.AST;
3+
using GraphQL.Types;
4+
5+
namespace Our.Umbraco.GraphQL.Adapters.Types
6+
{
7+
public class HtmlGraphType : ScalarGraphType
8+
{
9+
public HtmlGraphType()
10+
{
11+
Name = "HTML";
12+
Description = "A string containing HTML code.";
13+
}
14+
15+
public override object ParseLiteral(IValue value)
16+
{
17+
if (value is StringValue stringValue)
18+
return ParseValue(stringValue.Value);
19+
return null;
20+
}
21+
22+
public override object ParseValue(object value)
23+
{
24+
if(value is string stringValue)
25+
return new HtmlString(stringValue);
26+
27+
return null;
28+
}
29+
30+
public override object Serialize(object value)
31+
{
32+
if (value is IHtmlString htmlString)
33+
return htmlString.ToString();
34+
35+
return null;
36+
}
37+
}
38+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using System.Web;
56
using GraphQL;
67
using GraphQL.Types;
78
using Our.Umbraco.GraphQL.Adapters.Types.Relay;
@@ -33,6 +34,7 @@ public TypeRegistry()
3334
Add<DateTime, DateTimeGraphType>();
3435
Add<DateTimeOffset, DateTimeOffsetGraphType>();
3536
Add<TimeSpan, TimeSpanMillisecondsGraphType>();
37+
Add<IHtmlString, HtmlGraphType>();
3638
Add<Uri, UriGraphType>();
3739
Add<Id, IdGraphType>();
3840
Add<PageInfo, PageInfoGraphType>();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Web;
3+
using FluentAssertions;
4+
using GraphQL.Language.AST;
5+
using Our.Umbraco.GraphQL.Adapters.Types;
6+
using Our.Umbraco.GraphQL.Types;
7+
using Xunit;
8+
9+
namespace Our.Umbraco.GraphQL.Tests.Adapters.Types
10+
{
11+
public class HtmlGraphTypeTests
12+
{
13+
[Fact]
14+
public void Serialize_WithHtmlString_ReturnsValueToString()
15+
{
16+
var idGraphType = new HtmlGraphType();
17+
var value = "<div>Some HTML</div>";
18+
var htmlString = new HtmlString(value);
19+
20+
var serialized = idGraphType.Serialize(htmlString);
21+
22+
serialized.Should().BeOfType<string>().Which.Should().Be(value);
23+
}
24+
25+
[Fact]
26+
public void Serialize_WithNull_ReturnsNull()
27+
{
28+
var idGraphType = new HtmlGraphType();
29+
30+
var serialized = idGraphType.Serialize(null);
31+
32+
serialized.Should().BeNull();
33+
}
34+
35+
[Fact]
36+
public void ParseValue_WithValue_ReturnsHtmlString()
37+
{
38+
var idGraphType = new HtmlGraphType();
39+
var value = "<div>Some HTML</div>";
40+
41+
var parsed = idGraphType.ParseValue(value);
42+
43+
parsed.Should().BeAssignableTo<IHtmlString>().Which.ToString().Should().Be(value);
44+
}
45+
46+
[Fact]
47+
public void ParseValue_WithNull_ReturnsNull()
48+
{
49+
var idGraphType = new HtmlGraphType();
50+
51+
var parsed = idGraphType.ParseValue(null);
52+
53+
parsed.Should().BeNull();
54+
}
55+
56+
[Fact]
57+
public void ParseLiteral_WithStringValue_ReturnsHtmlString()
58+
{
59+
var idGraphType = new HtmlGraphType();
60+
var value = "<div>Some HTML</div>";
61+
62+
var parsed = idGraphType.ParseLiteral(new StringValue(value));
63+
64+
parsed.Should().BeAssignableTo<IHtmlString>().Which.ToString().Should().Be(value);
65+
}
66+
67+
[Fact]
68+
public void ParseLiteral_WithNull_ReturnsNull()
69+
{
70+
var idGraphType = new HtmlGraphType();
71+
72+
var parsed = idGraphType.ParseLiteral(null);
73+
74+
parsed.Should().BeNull();
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)