Skip to content

Commit

Permalink
Add HTML Graph Type
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmusjp committed Sep 30, 2019
1 parent 895af6d commit 79867e9
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Our.Umbraco.GraphQL/Adapters/Types/HtmlGraphType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Web;
using GraphQL.Language.AST;
using GraphQL.Types;

namespace Our.Umbraco.GraphQL.Adapters.Types
{
public class HtmlGraphType : ScalarGraphType
{
public HtmlGraphType()
{
Name = "HTML";
Description = "A string containing HTML code.";
}

public override object ParseLiteral(IValue value)
{
if (value is StringValue stringValue)
return ParseValue(stringValue.Value);
return null;
}

public override object ParseValue(object value)
{
if(value is string stringValue)
return new HtmlString(stringValue);

return null;
}

public override object Serialize(object value)
{
if (value is IHtmlString htmlString)
return htmlString.ToString();

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using GraphQL;
using GraphQL.Types;
using Our.Umbraco.GraphQL.Adapters.Types.Relay;
Expand Down Expand Up @@ -33,6 +34,7 @@ public TypeRegistry()
Add<DateTime, DateTimeGraphType>();
Add<DateTimeOffset, DateTimeOffsetGraphType>();
Add<TimeSpan, TimeSpanMillisecondsGraphType>();
Add<IHtmlString, HtmlGraphType>();
Add<Uri, UriGraphType>();
Add<Id, IdGraphType>();
Add<PageInfo, PageInfoGraphType>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Web;
using FluentAssertions;
using GraphQL.Language.AST;
using Our.Umbraco.GraphQL.Adapters.Types;
using Our.Umbraco.GraphQL.Types;
using Xunit;

namespace Our.Umbraco.GraphQL.Tests.Adapters.Types
{
public class HtmlGraphTypeTests
{
[Fact]
public void Serialize_WithHtmlString_ReturnsValueToString()
{
var idGraphType = new HtmlGraphType();
var value = "<div>Some HTML</div>";
var htmlString = new HtmlString(value);

var serialized = idGraphType.Serialize(htmlString);

serialized.Should().BeOfType<string>().Which.Should().Be(value);
}

[Fact]
public void Serialize_WithNull_ReturnsNull()
{
var idGraphType = new HtmlGraphType();

var serialized = idGraphType.Serialize(null);

serialized.Should().BeNull();
}

[Fact]
public void ParseValue_WithValue_ReturnsHtmlString()
{
var idGraphType = new HtmlGraphType();
var value = "<div>Some HTML</div>";

var parsed = idGraphType.ParseValue(value);

parsed.Should().BeAssignableTo<IHtmlString>().Which.ToString().Should().Be(value);
}

[Fact]
public void ParseValue_WithNull_ReturnsNull()
{
var idGraphType = new HtmlGraphType();

var parsed = idGraphType.ParseValue(null);

parsed.Should().BeNull();
}

[Fact]
public void ParseLiteral_WithStringValue_ReturnsHtmlString()
{
var idGraphType = new HtmlGraphType();
var value = "<div>Some HTML</div>";

var parsed = idGraphType.ParseLiteral(new StringValue(value));

parsed.Should().BeAssignableTo<IHtmlString>().Which.ToString().Should().Be(value);
}

[Fact]
public void ParseLiteral_WithNull_ReturnsNull()
{
var idGraphType = new HtmlGraphType();

var parsed = idGraphType.ParseLiteral(null);

parsed.Should().BeNull();
}
}
}

0 comments on commit 79867e9

Please sign in to comment.