Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added input formatter tests #2557

Merged
merged 2 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"two": "ABC2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"two_input": "ABC2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"one": "ABC"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"one_input": "ABC"
}
}