Skip to content

Commit

Permalink
Fix SG generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pekkah committed Jan 17, 2024
1 parent b2ef73c commit b6ad1fc
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion samples/GraphQL.Samples.SG.Arguments/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static int[] RangeWithInputObject([FromArguments]QueryOptions options)


[InputType]
public class QueryOptions
public partial class QueryOptions
{
public int Start { get; set; } = 0;

Expand Down
2 changes: 1 addition & 1 deletion samples/GraphQL.Samples.SG.Namespace/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class World
}

[InputType]
public class HelloInput
public partial class HelloInput
{
public string Name { get; set; }
}
Expand Down
11 changes: 9 additions & 2 deletions src/GraphQL.Server.SourceGenerators/InputTypeEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ public static string TrySetProperty(string fieldName, string name, string type)
// {{name}} is an scalar type
if (argumentValue.TryGetValue("{{fieldName}}", out var {{fieldName}}Value))
{
{{name}} = {{fieldName}}Value as {{type}};
if ({{fieldName}}Value is null)
{
{{name}} = default;
}
else
{
{{name}} = ({{type}}){{fieldName}}Value;
}
}
""";

Expand All @@ -69,7 +76,7 @@ public static string TrySetPropertyObjectValue(string fieldName, string name, st
{
if ({{fieldName}}Value is null)
{
{{name}} = null;
{{name}} = default;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,27 @@ public void Parse(IReadOnlyDictionary<string, object?> argumentValue)
// Id is an scalar type
if (argumentValue.TryGetValue("id", out var idValue))
{
Id = idValue as string;
if (idValue is null)
{
Id = default;
}
else
{
Id = (string)idValue;
}
}

// Content is an scalar type
if (argumentValue.TryGetValue("content", out var contentValue))
{
Content = contentValue as string;
if (contentValue is null)
{
Content = default;
}
else
{
Content = (string)contentValue;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Parse(IReadOnlyDictionary<string, object?> argumentValue)
{
if (contentValue is null)
{
Content = null;
Content = default;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Parse(IReadOnlyDictionary<string, object?> argumentValue)
{
if (contentValue is null)
{
Content = null;
Content = default;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ public void Parse(IReadOnlyDictionary<string, object?> argumentValue)
// Content is an scalar type
if (argumentValue.TryGetValue("content", out var contentValue))
{
Content = contentValue as int;
if (contentValue is null)
{
Content = default;
}
else
{
Content = (int)contentValue;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ public void Parse(IReadOnlyDictionary<string, object?> argumentValue)
// Content is an scalar type
if (argumentValue.TryGetValue("content", out var contentValue))
{
Content = contentValue as string;
if (contentValue is null)
{
Content = default;
}
else
{
Content = (string)contentValue;
}
}
}
}
Expand Down

0 comments on commit b6ad1fc

Please sign in to comment.