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

Fix custom scalar value-type support #3988

Merged
merged 8 commits into from
Dec 25, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public RuntimeTypeInfo(string name, string @namespace, bool isValueType = false)

public string Namespace { get; }

public string FullName =>
Namespace == "global::"
? Namespace + Name
: Namespace + "." + Name;

public string NamespaceWithoutGlobal =>
Namespace.Replace("global::", string.Empty);

Expand Down
30 changes: 22 additions & 8 deletions src/StrawberryShake/CodeGeneration/src/CodeGeneration/TypeInfos.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Diagnostics;
using StrawberryShake.CodeGeneration.Analyzers.Types;
using static StrawberryShake.CodeGeneration.TypeNames;

Expand Down Expand Up @@ -103,14 +104,27 @@ public class TypeInfos
{ TimeSpanSerializer, new RuntimeTypeInfo(TimeSpanSerializer) }
};

public RuntimeTypeInfo GetOrCreate(string fullTypeName, bool valueType = false) =>
_infos.TryGetValue(fullTypeName, out RuntimeTypeInfo? typeInfo)
? typeInfo
: new(fullTypeName, valueType);
public RuntimeTypeInfo GetOrAdd(string fullTypeName, bool valueType = false) =>
GetOrAdd(fullTypeName, () => new(fullTypeName, valueType));

public RuntimeTypeInfo TryCreate(RuntimeTypeDirective runtimeType) =>
_infos.TryGetValue(runtimeType.Name, out RuntimeTypeInfo? typeInfo)
? typeInfo
: new(runtimeType.Name, runtimeType.ValueType ?? false);
public RuntimeTypeInfo GetOrAdd(RuntimeTypeDirective runtimeType) =>
GetOrAdd(runtimeType.Name, () => new(runtimeType.Name, runtimeType.ValueType ?? false));

private RuntimeTypeInfo GetOrAdd(string fullTypeName, System.Func<RuntimeTypeInfo> factory)
{
if (!fullTypeName.StartsWith("global::"))
{
fullTypeName = "global::" + fullTypeName;
}

if (!_infos.TryGetValue(fullTypeName, out RuntimeTypeInfo? typeInfo))
{
typeInfo = factory();
Debug.Assert(typeInfo.FullName == fullTypeName, $"Expected generated type '{typeInfo.FullName}' to equal '{fullTypeName}'.");
_infos.Add(fullTypeName, typeInfo);
}

return typeInfo;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static RuntimeTypeInfo GetOrCreateTypeInfo(
this ISchema schema,
string typeName,
bool valueType = false) =>
((TypeInfos)schema.ContextData[_typeInfosKey]!).GetOrCreate(typeName, valueType);
((TypeInfos)schema.ContextData[_typeInfosKey]!).GetOrAdd(typeName, valueType);

private static void CollectScalarInfos(
IEnumerable<ScalarTypeExtensionNode> scalarTypeExtensions,
Expand Down Expand Up @@ -308,7 +308,7 @@ private static void TryRegister(TypeInfos typeInfos, RuntimeTypeDirective? runti
{
if (runtimeType is not null)
{
typeInfos.TryCreate(runtimeType);
typeInfos.GetOrAdd(runtimeType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ public void Custom_Scalar_With_RuntimeType() =>
"extend scalar Email @runtimeType(name: \"global::System.Int32\")",
"extend schema @key(fields: \"id\")");

[Fact]
public void Custom_Scalar_With_RuntimeType_ValueType_AsInput() =>
// Using System.Index here because it exists and is not part of the default TypeInfos
AssertResult(
"query SetPerson($email: Email!) { person { setEmail(email: $email) } }",
"type Query { person: Person }",
"type Person { setEmail(email: Email!): Email! }",
"scalar Email",
"extend scalar Email @runtimeType(name: \"global::System.Index\", valueType: true)");

[Fact]
public void Custom_Scalar_With_Unknown_RuntimeType() =>
AssertResult(
Expand Down
Loading