Skip to content

Commit

Permalink
Resolved issue in SchemaParser that prevented default values parsing …
Browse files Browse the repository at this point in the history
…in Input Objects (#7895)
  • Loading branch information
danielreynolds1 authored Jan 3, 2025
1 parent 55d4ebf commit 6ec7760
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ private static void ExtendInputObjectType(
var field = new InputFieldDefinition(fieldNode.Name.Value);
field.Description = fieldNode.Description?.Value;
field.Type = schema.Types.ResolveType(fieldNode.Type);
field.DefaultValue = fieldNode.DefaultValue;

BuildDirectiveCollection(schema, field.Directives, fieldNode.Directives);

Expand Down
96 changes: 95 additions & 1 deletion src/HotChocolate/Skimmed/test/Skimmed.Tests/SchemaParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;
using HotChocolate.Language;
using HotChocolate.Skimmed.Serialization;
using HotChocolate.Types;
using DirectiveLocation = HotChocolate.Types.DirectiveLocation;

namespace HotChocolate.Skimmed;

Expand Down Expand Up @@ -161,4 +162,97 @@ directive @skip("Custom argument description" ifCustom: String! @custom) on ENUM
Assert.True(argument.Directives.ContainsName("custom"));
Assert.Equal(DirectiveLocation.EnumValue, directive.Locations);
}

[Fact]
public void Parse_Input_Object_With_Default_Value()
{
// arrange
var sdl =
"""
input BookFilter {
genre: Genre = FANTASY
}
enum Genre {
FANTASY
SCIENCE_FICTION
}
""";

// act
var schema = SchemaParser.Parse(Encoding.UTF8.GetBytes(sdl));
// assert
Assert.Collection(
schema.Types.OrderBy(t => t.Name),
type =>
{
var inputType = Assert.IsType<InputObjectTypeDefinition>(type);
Assert.Equal("BookFilter", inputType.Name);
var genreField = Assert.Single(inputType.Fields);
Assert.Equal("genre", genreField.Name);
Assert.IsType<EnumTypeDefinition>(genreField.Type);
Assert.NotNull(genreField.DefaultValue);
Assert.Equal("FANTASY", genreField.DefaultValue.Value);
},
type =>
{
var genreType = Assert.IsType<EnumTypeDefinition>(type);
Assert.Equal("Genre", genreType.Name);
});
}

[Fact]
public void Parse_Input_Object_With_Multiple_Default_Values()
{
// arrange
var sdl =
"""
input BookFilter {
genre: Genre = FANTASY
author: String = "Lorem ipsum"
}
enum Genre {
FANTASY
SCIENCE_FICTION
}
scalar String
""";

// act
var schema = SchemaParser.Parse(Encoding.UTF8.GetBytes(sdl));
// assert
Assert.Collection(
schema.Types.OrderBy(t => t.Name),
type =>
{
var inputType = Assert.IsType<InputObjectTypeDefinition>(type);
Assert.Equal("BookFilter", inputType.Name);
Assert.Collection(inputType.Fields.OrderBy(f => f.Name),
authorField =>
{
Assert.Equal("author", authorField.Name);
var fieldType = Assert.IsType<ScalarTypeDefinition>(authorField.Type);
Assert.Equal("String", fieldType.Name);
Assert.NotNull(authorField.DefaultValue);
Assert.Equal("Lorem ipsum", authorField.DefaultValue.Value);
},
genreField =>
{
Assert.Equal("genre", genreField.Name);
Assert.IsType<EnumTypeDefinition>(genreField.Type);
Assert.NotNull(genreField.DefaultValue);
Assert.Equal("FANTASY", genreField.DefaultValue.Value);
});
},
type =>
{
var genreType = Assert.IsType<EnumTypeDefinition>(type);
Assert.Equal("Genre", genreType.Name);
},
type =>
{
var stringType = Assert.IsType<ScalarTypeDefinition>(type);
Assert.Equal("String", stringType.Name);
});
}
}

0 comments on commit 6ec7760

Please sign in to comment.