Skip to content

Commit

Permalink
v7.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Abraham committed Sep 10, 2023
1 parent 22837ba commit 47e29e0
Show file tree
Hide file tree
Showing 51 changed files with 1,616 additions and 1,466 deletions.
2 changes: 1 addition & 1 deletion src/TypeCache.GraphQL/Data/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static ObjectGraphType CreateGraphType(string name, IGraphType dataGraphT
graphType.AddField(new()
{
Name = nameof(Connection<T>.TotalCount),
Type = typeof(IntGraphType),
Type = typeof(GraphQLNumberType<int>),
Resolver = new FuncFieldResolver<Connection<T>, int?>(context => context.Source.TotalCount)
});

Expand Down
3 changes: 2 additions & 1 deletion src/TypeCache.GraphQL/Data/Edge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GraphQL.Types;
using TypeCache.GraphQL.Attributes;
using TypeCache.GraphQL.Extensions;
using TypeCache.GraphQL.Types;
using static System.FormattableString;

namespace TypeCache.GraphQL.Data;
Expand All @@ -25,7 +26,7 @@ public static ObjectGraphType CreateGraphType(string name, IGraphType dataGraphT
graphType.AddField(new()
{
Name = nameof(Edge<T>.Cursor),
Type = typeof(IntGraphType),
Type = typeof(GraphQLNumberType<int>),
Resolver = new FuncFieldResolver<Edge<T>, int>(context => context.Source.Cursor)
});
graphType.AddField(new()
Expand Down
53 changes: 30 additions & 23 deletions src/TypeCache.GraphQL/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
// Copyright (c) 2021 Samuel Abraham

using System;
using GraphQL.Types;
using System.Numerics;
using TypeCache.Extensions;
using TypeCache.GraphQL.Types;

namespace TypeCache.GraphQL.Extensions;

public static class EnumExtensions
{
public static Type? ToGraphType(this SystemType @this) => @this switch
public static Type? ToGraphType(this ScalarType @this) => @this switch
{
SystemType.String or SystemType.Char or SystemType.Range => typeof(StringGraphType),
SystemType.Uri => typeof(UriGraphType),
SystemType.Boolean => typeof(BooleanGraphType),
SystemType.SByte => typeof(SByteGraphType),
SystemType.Int16 => typeof(ShortGraphType),
SystemType.Int32 or SystemType.Index => typeof(IntGraphType),
SystemType.Int64 or SystemType.IntPtr => typeof(LongGraphType),
SystemType.Int128 or SystemType.UInt128 or SystemType.BigInteger => typeof(BigIntGraphType),
SystemType.Byte => typeof(ByteGraphType),
SystemType.UInt16 => typeof(UShortGraphType),
SystemType.UInt32 => typeof(UIntGraphType),
SystemType.UInt64 or SystemType.UIntPtr => typeof(ULongGraphType),
SystemType.Half => typeof(HalfGraphType),
SystemType.Single or SystemType.Double => typeof(FloatGraphType),
SystemType.Decimal => typeof(DecimalGraphType),
SystemType.DateOnly => typeof(DateOnlyGraphType),
SystemType.DateTime => typeof(DateTimeGraphType),
SystemType.DateTimeOffset => typeof(DateTimeOffsetGraphType),
SystemType.TimeOnly => typeof(TimeOnlyGraphType),
SystemType.TimeSpan => typeof(TimeSpanSecondsGraphType),
SystemType.Guid => typeof(GuidGraphType),
ScalarType.Boolean => typeof(GraphQLBooleanType),
ScalarType.Byte => typeof(GraphQLNumberType<byte>),
ScalarType.Decimal => typeof(GraphQLNumberType<decimal>),
ScalarType.DateOnly => typeof(GraphQLStringType<DateOnly>),
ScalarType.DateTime => typeof(GraphQLStringType<DateTime>),
ScalarType.DateTimeOffset => typeof(GraphQLStringType<DateTimeOffset>),
ScalarType.Guid => typeof(GraphQLStringType<Guid>),
ScalarType.Half => typeof(GraphQLNumberType<Half>),
ScalarType.Int16 => typeof(GraphQLNumberType<short>),
ScalarType.Int32 or ScalarType.Index => typeof(GraphQLNumberType<int>),
ScalarType.Int64 => typeof(GraphQLNumberType<long>),
ScalarType.IntPtr => typeof(GraphQLNumberType<nint>),
ScalarType.Int128 => typeof(GraphQLNumberType<Int128>),
ScalarType.UInt128 => typeof(GraphQLNumberType<UInt128>),
ScalarType.BigInteger => typeof(GraphQLNumberType<BigInteger>),
ScalarType.UInt16 => typeof(GraphQLNumberType<ushort>),
ScalarType.UInt32 => typeof(GraphQLNumberType<uint>),
ScalarType.UInt64 => typeof(GraphQLNumberType<ulong>),
ScalarType.UIntPtr => typeof(GraphQLNumberType<UIntPtr>),
ScalarType.SByte => typeof(GraphQLNumberType<sbyte>),
ScalarType.Single => typeof(GraphQLNumberType<float>),
ScalarType.Double => typeof(GraphQLNumberType<char>),
ScalarType.Char => typeof(GraphQLStringType<char>),
ScalarType.String => typeof(GraphQLStringType),
ScalarType.TimeOnly => typeof(GraphQLStringType<TimeOnly>),
ScalarType.TimeSpan => typeof(GraphQLStringType<TimeSpan>),
ScalarType.Uri => typeof(GraphQLUriType),
_ => null
};
}
46 changes: 22 additions & 24 deletions src/TypeCache.GraphQL/Extensions/GraphQLExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using GraphQL;
using GraphQL.Resolvers;
using GraphQL.Types;
using GraphQLParser.AST;
using TypeCache.Extensions;
using TypeCache.GraphQL.Data;
using TypeCache.GraphQL.Resolvers;
Expand Down Expand Up @@ -93,7 +92,9 @@ public static Type ToGraphQLInterfaceType(this Type @this)
/// </summary>
[MethodImpl(AggressiveInlining), DebuggerHidden]
public static Type ToGraphQLObjectType(this Type @this)
=> typeof(GraphQLObjectType<>).MakeGenericType(@this);
=> @this.IsValueType && @this.IsNullable()
? typeof(GraphQLObjectType<>).MakeGenericType(@this.GenericTypeArguments[0])
: typeof(GraphQLObjectType<>).MakeGenericType(@this);

public static Type ToGraphQLType(this ParameterInfo @this)
{
Expand All @@ -119,35 +120,32 @@ public static Type ToGraphQLType(this Type @this, bool isInputType)
(objectType is ObjectType.Delegate).AssertFalse();
(objectType is ObjectType.Object).AssertFalse();

if (objectType is ObjectType.Dictionary || objectType is ObjectType.ReadOnlyDictionary)
var collectionType = @this.GetCollectionType();
if (collectionType.IsDictionary())
return typeof(KeyValuePair<,>).MakeGenericType(@this.GenericTypeArguments).ToGraphQLType(isInputType).ToNonNullGraphType().ToListGraphType();

if (@this.IsEnum)
return @this.ToGraphQLEnumType();

var systemType = @this.GetSystemType();
if (systemType is SystemType.Task || systemType is SystemType.ValueTask)
if (objectType is ObjectType.Task || objectType is ObjectType.ValueTask)
return @this.IsGenericType
? @this.GenericTypeArguments.First()!.ToGraphQLType(false)
: throw new ArgumentOutOfRangeException(nameof(@this), Invariant($"{nameof(Task)} and {nameof(ValueTask)} are not allowed as GraphQL types."));

if (systemType is SystemType.Nullable)
return @this.GenericTypeArguments.First()!.ToGraphQLType(isInputType);

var systemGraphType = systemType.ToGraphType();
if (systemGraphType is not null)
return systemGraphType;
var scalarGraphType = @this.GetDataType().ToGraphType();
if (scalarGraphType is not null)
return scalarGraphType;

if (@this.HasElementType)
{
var elementType = @this.GetElementType()!.ToGraphQLType(isInputType);
if (elementType.IsValueType && elementType.GetSystemType() is not SystemType.Nullable)
if (elementType.IsValueType && !elementType.Is(typeof(Nullable<>)))
elementType = elementType.ToNonNullGraphType();

return elementType.ToListGraphType();
}

if (@this.IsGenericType && @this.IsOrImplements(typeof(IEnumerable<>)))
if (@this.Is(typeof(IEnumerable<>)) || @this.Implements(typeof(IEnumerable<>)))
return @this.GenericTypeArguments.First()!.ToGraphQLType(isInputType).ToListGraphType();

if (@this.IsInterface)
Expand Down Expand Up @@ -195,20 +193,20 @@ public static FieldType ToFieldType(this PropertyInfo @this)
arguments.Add("null", type, description: "Return this value instead of null.");

if (@this.PropertyType.IsAssignableTo<IFormattable>())
arguments.Add<StringGraphType>("format", description: "Use .NET format specifiers to format the data.");
arguments.Add<GraphQLStringType>("format", description: "Use .NET format specifiers to format the data.");

if (type.Is<DateTimeGraphType>() || type.Is<NonNullGraphType<DateTimeGraphType>>())
arguments.Add<StringGraphType>("timeZone", description: Invariant($"{typeof(TimeZoneInfo).Namespace}.{nameof(TimeZoneInfo)}.{nameof(TimeZoneInfo.ConvertTimeBySystemTimeZoneId)}(value, [..., ...] | [UTC, ...])"));
else if (type.Is<DateTimeOffsetGraphType>() || type.Is<NonNullGraphType<DateTimeOffsetGraphType>>())
arguments.Add<StringGraphType>("timeZone", description: Invariant($"{typeof(TimeZoneInfo).Namespace}.{nameof(TimeZoneInfo)}.{nameof(TimeZoneInfo.ConvertTimeBySystemTimeZoneId)}(value, ...)"));
else if (type.Is<StringGraphType>() || type.Is<NonNullGraphType<StringGraphType>>())
if (type.Is<GraphQLStringType<DateTime>>() || type.Is<NonNullGraphType<GraphQLStringType<DateTime>>>())
arguments.Add<GraphQLStringType>("timeZone", description: Invariant($"{typeof(TimeZoneInfo).Namespace}.{nameof(TimeZoneInfo)}.{nameof(TimeZoneInfo.ConvertTimeBySystemTimeZoneId)}(value, [..., ...] | [UTC, ...])"));
else if (type.Is<GraphQLStringType<DateTimeOffset>>() || type.Is<NonNullGraphType<GraphQLStringType<DateTimeOffset>>>())
arguments.Add<GraphQLStringType>("timeZone", description: Invariant($"{typeof(TimeZoneInfo).Namespace}.{nameof(TimeZoneInfo)}.{nameof(TimeZoneInfo.ConvertTimeBySystemTimeZoneId)}(value, ...)"));
else if (type.Is<GraphQLStringType>() || type.Is<NonNullGraphType<GraphQLStringType>>())
{
arguments.Add<GraphQLEnumType<StringCase>>("case", description: "Convert string value to upper or lower case.");
arguments.Add<IntGraphType>("length", description: "Exclude the rest of the string value if it exceeds this length.");
arguments.Add<StringGraphType>("match", description: "Returns the matching result based on the specified regular expression pattern, null if no match.");
arguments.Add<StringGraphType>("trim", description: Invariant($"{typeof(string).Namespace}.{nameof(String)}.{nameof(string.Trim)}(value)"));
arguments.Add<StringGraphType>("trimEnd", description: Invariant($"{typeof(string).Namespace}.{nameof(String)}.{nameof(string.TrimEnd)}(value)"));
arguments.Add<StringGraphType>("trimStart", description: Invariant($"{typeof(string).Namespace}.{nameof(String)}.{nameof(string.TrimStart)}(value)"));
arguments.Add<GraphQLNumberType<int>>("length", description: "Exclude the rest of the string value if it exceeds this length.");
arguments.Add<GraphQLStringType>("match", description: "Returns the matching result based on the specified regular expression pattern, null if no match.");
arguments.Add<GraphQLStringType>("trim", description: Invariant($"{typeof(string).Namespace}.{nameof(String)}.{nameof(string.Trim)}(value)"));
arguments.Add<GraphQLStringType>("trimEnd", description: Invariant($"{typeof(string).Namespace}.{nameof(String)}.{nameof(string.TrimEnd)}(value)"));
arguments.Add<GraphQLStringType>("trimStart", description: Invariant($"{typeof(string).Namespace}.{nameof(String)}.{nameof(string.TrimStart)}(value)"));
}

return new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.AccessControl;
using GraphQL;
using GraphQLParser.AST;
using TypeCache.Data;
Expand Down Expand Up @@ -63,7 +62,7 @@ public static DataTable GetArgumentAsDataTable(this IResolveFieldContext @this,
{
_ when parameterInfo.GraphQLIgnore() => null,
_ when parameterInfo.ParameterType.Is<IResolveFieldContext>() => @this,
_ when parameterInfo.ParameterType.Is(sourceType) && !parameterInfo.ParameterType.Is<object>() => @this.Source,
_ when parameterInfo.ParameterType.Is(sourceType!) && !parameterInfo.ParameterType.Is<object>() => @this.Source,
IDictionary<string, object?> dictionary when !parameterInfo.ParameterType.Is<IDictionary<string, object?>>() =>
dictionary.MapTo(parameterInfo.ParameterType.Create()!),
_ => argument
Expand Down Expand Up @@ -100,7 +99,7 @@ _ when parameterInfo.ParameterType.Is<MATCH[]>() => keys.ToArray(),
public static IDictionary<string, object?> GetInputs(this IResolveFieldContext @this)
{
var dictionary = new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
if (!@this.Operation.SelectionSet.Selections.OfType<GraphQLField>().TryFirst(out var root) || root.Arguments?.Any() is not true)
if (!@this.Operation.SelectionSet.Selections.TryFirst<GraphQLField>(out var root) || root.Arguments?.Any() is not true)
return dictionary;

foreach (var argument in root.Arguments)
Expand Down Expand Up @@ -147,11 +146,11 @@ private static void AddInputs(this IDictionary<string, object?> @this, string pa
private static object? GetScalarValue(this GraphQLValue @this)
=> @this switch
{
GraphQLBooleanValue booleanValue => booleanValue.BoolValue,
GraphQLEnumValue enumValue => enumValue.Name,
GraphQLFloatValue floatValue => double.Parse(floatValue.Value.Span),
GraphQLIntValue intValue => int.Parse(intValue.Value.Span),
GraphQLStringValue stringValue => new string(stringValue.Value.Span),
GraphQLBooleanValue value => value.BoolValue,
GraphQLEnumValue value => value.Name,
GraphQLFloatValue value => decimal.Parse(value.Value.Span),
GraphQLIntValue value => long.Parse(value.Value.Span),
GraphQLStringValue value => new string(value.Value.Span),
_ => null
};

Expand Down
Loading

0 comments on commit 47e29e0

Please sign in to comment.