Skip to content

Commit

Permalink
Added input formatter tests (#2557)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Nov 10, 2020
1 parent cba5bfe commit 8720169
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
@@ -0,0 +1,140 @@
using System.Threading.Tasks;
using HotChocolate.Execution;
using HotChocolate.Tests;
using Snapshooter.Xunit;
using Xunit;

namespace HotChocolate.Types
{
public class InputValueFormatterTests
{
[Fact]
public async Task Add_Input_Formatter_To_Argument()
{
Snapshot.FullName();

await SchemaBuilder.New()
.AddQueryType<QueryType>()
.Create()
.MakeExecutable()
.ExecuteAsync("{ one(arg: \"abc\") }")
.MatchSnapshotAsync();
}

[Fact]
public async Task Add_Chained_Input_Formatter_To_Argument()
{
Snapshot.FullName();

await SchemaBuilder.New()
.AddQueryType<QueryType>()
.Create()
.MakeExecutable()
.ExecuteAsync("{ two(arg: \"abc\") }")
.MatchSnapshotAsync();
}

[Fact]
public async Task Add_Input_Formatter_To_Field()
{
Snapshot.FullName();

await SchemaBuilder.New()
.AddQueryType<QueryType>()
.Create()
.MakeExecutable()
.ExecuteAsync("{ one_input(arg: { bar: \"abc\" }) }")
.MatchSnapshotAsync();
}

[Fact]
public async Task Add_Chained_Input_Formatter_To_Field()
{
Snapshot.FullName();

await SchemaBuilder.New()
.AddQueryType<QueryType>()
.Create()
.MakeExecutable()
.ExecuteAsync("{ two_input(arg: { baz: \"abc\" }) }")
.MatchSnapshotAsync();
}

public class QueryType : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
{
descriptor.Name("Query");

descriptor.Field("one")
.Argument("arg", a => a.Type<StringType>()
.Extend()
.OnBeforeCreate(d => d.Formatters.Add(new UpperCaseInputValueFormatter())))
.Type<StringType>()
.Resolve(c => c.ArgumentValue<string>("arg"));

descriptor.Field("two")
.Argument("arg", a => a.Type<StringType>()
.Extend()
.OnBeforeCreate(d =>
{
d.Formatters.Add(new UpperCaseInputValueFormatter());
d.Formatters.Add(new AddTwoInputValueFormatter());
}))
.Type<StringType>()
.Resolve(c => c.ArgumentValue<string>("arg"));

descriptor.Field("one_input")
.Argument("arg", a => a.Type<FooInputType>())
.Type<StringType>()
.Resolve(c => c.ArgumentValue<Foo>("arg").Bar);

descriptor.Field("two_input")
.Argument("arg", a => a.Type<FooInputType>())
.Type<StringType>()
.Resolve(c => c.ArgumentValue<Foo>("arg").Baz);
}
}

public class FooInputType : InputObjectType<Foo>
{
protected override void Configure(IInputObjectTypeDescriptor<Foo> descriptor)
{
descriptor.Field(t => t.Bar)
.Extend()
.OnBeforeCreate(d => d.Formatters.Add(new UpperCaseInputValueFormatter()));

descriptor.Field(t => t.Baz)
.Extend()
.OnBeforeCreate(d =>
{
d.Formatters.Add(new UpperCaseInputValueFormatter());
d.Formatters.Add(new AddTwoInputValueFormatter());
});
}
}

public class Foo
{
public string Bar { get; set; }

public string Baz { get; set; }
}

public class UpperCaseInputValueFormatter : IInputValueFormatter
{
public object OnAfterDeserialize(object runtimeValue)
{
return runtimeValue is string s ? s.ToUpperInvariant() : runtimeValue;
}
}

public class AddTwoInputValueFormatter : IInputValueFormatter
{
public object OnAfterDeserialize(object runtimeValue)
{
return runtimeValue is string s ? s + "2" : runtimeValue;
}
}
}
}
@@ -0,0 +1,5 @@
{
"data": {
"two": "ABC2"
}
}
@@ -0,0 +1,5 @@
{
"data": {
"two_input": "ABC2"
}
}
@@ -0,0 +1,5 @@
{
"data": {
"one": "ABC"
}
}
@@ -0,0 +1,5 @@
{
"data": {
"one_input": "ABC"
}
}

0 comments on commit 8720169

Please sign in to comment.