Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get default value from constructor when type is a record. (#2428)
Co-authored-by: Michael Staib <michael@chillicream.com>
- Loading branch information
1 parent
d7eba9d
commit 1b34d9a
Showing
3 changed files
with
136 additions
and
56 deletions.
There are no files selected for viewing
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
120 changes: 71 additions & 49 deletions
120
src/HotChocolate/Core/test/Types.Records.Tests/RecordsTests.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 |
---|---|---|
@@ -1,49 +1,71 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using HotChocolate.Types.Relay; | ||
using static HotChocolate.Tests.TestHelper; | ||
using System.Threading.Tasks; | ||
using HotChocolate.Tests; | ||
using Snapshooter.Xunit; | ||
using Xunit; | ||
using HotChocolate.Execution; | ||
|
||
namespace HotChocolate.Types | ||
{ | ||
public class RecordsTests | ||
{ | ||
[Fact] | ||
public async Task Records_Clone_Member_Is_Removed() | ||
{ | ||
Snapshot.FullName(); | ||
|
||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.Services | ||
.BuildServiceProvider() | ||
.GetSchemaAsync() | ||
.MatchSnapshotAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task Relay_Id_Middleware_Is_Correctly_Applied() | ||
{ | ||
Snapshot.FullName(); | ||
|
||
await ExpectValid | ||
( | ||
@"{ person { id name } }", | ||
b => b.AddQueryType<Query>() | ||
) | ||
.MatchSnapshotAsync(); ; | ||
} | ||
|
||
public class Query | ||
{ | ||
public Person GetPerson() => new Person(1, "Michael"); | ||
} | ||
|
||
public record Person([ID] int Id, string Name); | ||
} | ||
} | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using HotChocolate.Types.Relay; | ||
using static HotChocolate.Tests.TestHelper; | ||
using System.Threading.Tasks; | ||
using HotChocolate.Tests; | ||
using Snapshooter.Xunit; | ||
using Xunit; | ||
using HotChocolate.Execution; | ||
|
||
namespace HotChocolate.Types | ||
{ | ||
public class RecordsTests | ||
{ | ||
[Fact] | ||
public async Task Records_Clone_Member_Is_Removed() | ||
{ | ||
Snapshot.FullName(); | ||
|
||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.Services | ||
.BuildServiceProvider() | ||
.GetSchemaAsync() | ||
.MatchSnapshotAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task Records_Default_Value_Is_Taken_From_Ctor() | ||
{ | ||
Snapshot.FullName(); | ||
|
||
await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddQueryType<Query2>() | ||
.Services | ||
.BuildServiceProvider() | ||
.GetSchemaAsync() | ||
.MatchSnapshotAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task Relay_Id_Middleware_Is_Correctly_Applied() | ||
{ | ||
Snapshot.FullName(); | ||
|
||
await ExpectValid | ||
( | ||
@"{ person { id name } }", | ||
b => b.AddQueryType<Query>() | ||
) | ||
.MatchSnapshotAsync(); | ||
} | ||
|
||
public class Query | ||
{ | ||
public Person GetPerson() => new Person(1, "Michael"); | ||
} | ||
|
||
public record Person([ID] int Id, string Name); | ||
|
||
public class Query2 | ||
{ | ||
public DefaultValueTest GetPerson(DefaultValueTest? defaultValueTest) => | ||
new DefaultValueTest(1, "Test"); | ||
} | ||
|
||
public record DefaultValueTest([ID] int Id, string Name = "ShouldBeDefaultValue"); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...es.Records.Tests/__snapshots__/RecordsTests.Records_Default_Value_Is_Taken_From_Ctor.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,23 @@ | ||
schema { | ||
query: Query2 | ||
} | ||
|
||
type DefaultValueTest { | ||
id: ID! | ||
name: String! | ||
} | ||
|
||
type Query2 { | ||
person(defaultValueTest: DefaultValueTestInput): DefaultValueTest! | ||
} | ||
|
||
input DefaultValueTestInput { | ||
id: ID! | ||
name: String! = "ShouldBeDefaultValue" | ||
} | ||
|
||
"The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `\"4\"`) or integer (such as `4`) input value will be accepted as an ID." | ||
scalar ID | ||
|
||
"The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text." | ||
scalar String |