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

Update JsonObjectTypeExtensions.cs #5649

Merged
merged 5 commits into from
Jan 20, 2023
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
165 changes: 130 additions & 35 deletions src/HotChocolate/Core/src/Types.Json/JsonObjectTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,26 @@ public static IObjectFieldDescriptor FromJson(

descriptor
.Extend()
.OnBeforeCompletion((ctx, def) =>
{
propertyName ??= def.Name;
var type = ctx.GetType<IType>(def.Type!);
var namedType = type.NamedType();

if (type.IsListType())
.OnBeforeCompletion(
(ctx, def) =>
{
throw ThrowHelper.CannotInferTypeFromJsonObj(ctx.Type.Name);
}
propertyName ??= def.Name;
var type = ctx.GetType<IType>(def.Type!);
var namedType = type.NamedType();

if (namedType is ScalarType scalarType)
{
InferResolver(ctx.Type, def, scalarType, propertyName);
return;
}
if (type.IsListType())
{
throw ThrowHelper.CannotInferTypeFromJsonObj(ctx.Type.Name);
}

throw ThrowHelper.CannotInferTypeFromJsonObj(ctx.Type.Name);
});
if (namedType is ScalarType scalarType)
{
InferResolver(ctx.Type, def, scalarType, propertyName);
return;
}

throw ThrowHelper.CannotInferTypeFromJsonObj(ctx.Type.Name);
});

return descriptor;
}
Expand Down Expand Up @@ -87,11 +88,12 @@ public static IObjectFieldDescriptor FromJson<TResult>(

descriptor
.Extend()
.OnBeforeCreate(def =>
{
def.ResultType = typeof(TResult);
def.PureResolver = ctx => resolve(ctx.Parent<JsonElement>());
});
.OnBeforeCreate(
def =>
{
def.ResultType = typeof(TResult);
def.PureResolver = ctx => resolve(ctx.Parent<JsonElement>());
});

return descriptor;
}
Expand All @@ -108,55 +110,148 @@ internal static void InferResolver(
case ScalarNames.String:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetString();
return;

case ScalarNames.Boolean:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetBoolean();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);

return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetBoolean();
};
return;

case ScalarNames.Short:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetInt16();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);

return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetInt16();
};
return;

case ScalarNames.Int:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetInt32();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);

return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetInt32();
};
return;

case ScalarNames.Long:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetInt64();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);

return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetUInt64();
};
return;

case ScalarNames.Float:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetDouble();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);

return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetDouble();
};
return;

case ScalarNames.Decimal:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetDecimal();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);

return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetDecimal();
};
return;

case ScalarNames.URL:
def.PureResolver = ctx => new Uri(ctx.GetProperty(propertyName)?.GetString()!);
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);

if (property is null or { ValueKind: JsonValueKind.Null })
{
return null;
}

return new Uri(property.Value.GetString()!);
};
return;

case ScalarNames.UUID:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetGuid();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);

return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetGuid();
};
return;

case ScalarNames.Byte:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetByte();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);

return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetByte();
};
return;

case ScalarNames.ByteArray:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetBytesFromBase64();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);

return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetBytesFromBase64();
};
return;

case ScalarNames.Date:
def.PureResolver = ctx =>
{
var value = ctx.GetProperty(propertyName)?.GetString();
var property = ctx.GetProperty(propertyName);

if (value is null)
if (property is null or { ValueKind: JsonValueKind.Null })
{
return null;
}

return DateTime.Parse(
value,
property.Value.GetString()!,
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal);
};
return;

case ScalarNames.DateTime:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetDateTimeOffset();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);

return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetDateTimeOffset();
};
return;

default:
throw ThrowHelper.CannotInferTypeFromJsonObj(type.Name);
}
Expand Down
Loading