Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cba5bfe
commit 8720169
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
src/HotChocolate/Core/test/Types.Tests/Types/InputValueFormatterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...Types/__snapshots__/InputValueFormatterTests.Add_Chained_Input_Formatter_To_Argument.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"data": { | ||
"two": "ABC2" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ts/Types/__snapshots__/InputValueFormatterTests.Add_Chained_Input_Formatter_To_Field.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"data": { | ||
"two_input": "ABC2" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...s.Tests/Types/__snapshots__/InputValueFormatterTests.Add_Input_Formatter_To_Argument.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"data": { | ||
"one": "ABC" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ypes.Tests/Types/__snapshots__/InputValueFormatterTests.Add_Input_Formatter_To_Field.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"data": { | ||
"one_input": "ABC" | ||
} | ||
} |