Skip to content

Commit

Permalink
Merge pull request #23 from graphql-dotnet/helpers-and-logging
Browse files Browse the repository at this point in the history
Helper functions and logging
  • Loading branch information
tlil authored Feb 15, 2017
2 parents f858a9e + 87185fa commit 573b877
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 35 deletions.
32 changes: 26 additions & 6 deletions src/GraphQL.Conventions/Adapters/GraphTypeAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GraphQL.Conventions.Types;
using GraphQL.Conventions.Types.Descriptors;
using GraphQL.Conventions.Types.Resolution.Extensions;
using GraphQL.Conventions.Utilities;
using GraphQL.Types;

namespace GraphQL.Conventions.Adapters
{
public class GraphTypeAdapter : IGraphTypeAdapter<ISchema, IGraphType>
{
public static ILogger Logger { get; set; }

private readonly CachedRegistry<Type, IGraphType> _typeDescriptors = new CachedRegistry<Type, IGraphType>();

private readonly Dictionary<string, Type> _registeredScalarTypes = new Dictionary<string, Type>();

public ISchema DeriveSchema(GraphSchemaInfo schemaInfo)
{
Logger?.Trace($"Deriving Schema(Query={schemaInfo?.Query?.Name}, Mutation={schemaInfo?.Mutation?.Name})");
var types = schemaInfo
.Types
.Where(t => !t.IsIgnored && !t.Interfaces.Any(iface => iface.IsIgnored))
Expand Down Expand Up @@ -46,6 +47,7 @@ public ISchema DeriveSchema(GraphSchemaInfo schemaInfo)

public IGraphType DeriveType(GraphTypeInfo typeInfo)
{
Logger?.Trace($"Deriving Type={typeInfo?.Name}");
var primitiveType = GetPrimitiveType(typeInfo);
var graphType = primitiveType != null
? WrapNonNullableType(typeInfo, Activator.CreateInstance(primitiveType) as GraphType)
Expand All @@ -55,6 +57,7 @@ public IGraphType DeriveType(GraphTypeInfo typeInfo)

public void RegisterScalarType<TType>(string name)
{
Logger?.Trace($"Registering Scalar(Name={name}, Type={typeof(TType).Name}");
_registeredScalarTypes.Add(name, typeof(TType));
}

Expand All @@ -63,8 +66,10 @@ private IGraphType DeriveTypeFromTypeInfo(Type type)
var graphType = _typeDescriptors.GetEntity(type);
if (graphType != null)
{
Logger?.Trace($"Deriving representation for Type={type?.Name}");
return graphType;
}
Logger?.Trace($"Instantiating representation for Type={type?.Name}");
return (IGraphType)Activator.CreateInstance(type);
}

Expand All @@ -73,8 +78,10 @@ private IObjectGraphType DeriveOperationType(GraphTypeInfo typeInfo) =>
? DeriveType(typeInfo) as IObjectGraphType
: null;

private FieldType DeriveField(GraphFieldInfo fieldInfo) =>
new FieldType
private FieldType DeriveField(GraphFieldInfo fieldInfo)
{
Logger?.Trace($"Deriving Field={fieldInfo?.Name}");
return new FieldType
{
Name = fieldInfo.Name,
Description = fieldInfo.Description,
Expand All @@ -84,14 +91,18 @@ private FieldType DeriveField(GraphFieldInfo fieldInfo) =>
Arguments = new QueryArguments(fieldInfo.Arguments.Where(arg => !arg.IsInjected).Select(DeriveArgument)),
Resolver = new FieldResolver { FieldInfo = fieldInfo },
};
}

private QueryArgument DeriveArgument(GraphArgumentInfo argumentInfo) =>
new QueryArgument(GetType(argumentInfo.Type))
private QueryArgument DeriveArgument(GraphArgumentInfo argumentInfo)
{
Logger?.Trace($"Deriving Field={argumentInfo?.Name}");
return new QueryArgument(GetType(argumentInfo.Type))
{
Name = argumentInfo.Name,
Description = argumentInfo.Description,
DefaultValue = argumentInfo.DefaultValue,
};
}

private Type GetType(GraphTypeInfo typeInfo) => DeriveType(typeInfo).GetType();

Expand Down Expand Up @@ -121,11 +132,13 @@ private Type GetPrimitiveType(GraphTypeInfo typeInfo)
return typeof(Types.UriGraphType);
default:
Type type;
Logger?.Trace($"Deriving PrimitiveType={typeInfo?.Name}");
if (!string.IsNullOrWhiteSpace(typeInfo.Name) &&
_registeredScalarTypes.TryGetValue(typeInfo.Name, out type))
{
return type;
}
Logger?.Warning($"Failed deriving PrimitiveType={typeInfo?.Name}");
return null;
}
}
Expand All @@ -134,26 +147,32 @@ private IGraphType GetComplexType(GraphTypeInfo typeInfo)
{
if (typeInfo.IsInterfaceType)
{
Logger?.Trace($"Constructing InterfaceType={typeInfo?.Name}");
return ConstructInterfaceType(typeInfo);
}
else if (typeInfo.IsEnumerationType)
{
Logger?.Trace($"Constructing EnumerationType={typeInfo?.Name}");
return ConstructEnumerationType(typeInfo);
}
else if (typeInfo.IsUnionType)
{
Logger?.Trace($"Constructing UnionType={typeInfo?.Name}");
return ConstructUnionType(typeInfo);
}
else if (typeInfo.IsListType)
{
Logger?.Trace($"Constructing ListType={typeInfo?.Name}");
return ConstructListType(typeInfo);
}
else if (typeInfo.IsInputType)
{
Logger?.Trace($"Constructing InputType={typeInfo?.Name}");
return ConstructInputType(typeInfo);
}
else if (typeInfo.IsOutputType)
{
Logger?.Trace($"Constructing OutputType={typeInfo?.Name}");
return ConstructOutputType(typeInfo);
}
else
Expand All @@ -172,6 +191,7 @@ private IGraphType CacheType(GraphTypeInfo typeInfo, IGraphType graphType)

private TType CreateTypeInstance<TType>(Type type)
{
Logger?.Trace($"Creating TypeInstance={type?.Name}");
var obj = Activator.CreateInstance(type);
return obj is TType ? (TType)obj : default(TType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using GraphQL.Conventions.Attributes;
using GraphQL.Conventions.Attributes.Collectors;
using GraphQL.Conventions.Attributes.MetaData.Utilities;
using GraphQL.Conventions.Types;
using GraphQL.Conventions.Types.Descriptors;
using GraphQL.Conventions.Types.Resolution.Extensions;

Expand Down
11 changes: 8 additions & 3 deletions src/GraphQL.Conventions/Builders/SchemaConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ public TSchemaType Build<TSchema>() =>
Build(typeof(TSchema).GetTypeInfo());

public TSchemaType Build(params Type[] schemaTypes) =>
Build(schemaTypes.Select(type => type.GetTypeInfo()).ToArray());
Build(schemaTypes?.Select(type => type.GetTypeInfo()).ToArray());

public TSchemaType Build(params TypeInfo[] schemaTypes)
{
var schemaInfos = schemaTypes
var schemaInfos = schemaTypes?
.Select(_typeResolver.DeriveSchema)
.ToList();
.ToList() ?? new List<GraphSchemaInfo>();

var schemaInfo = schemaInfos.FirstOrDefault();
if (schemaInfo == null)
{
return null;
}

schemaInfo.TypeResolutionDelegate = TypeResolutionDelegate;

foreach (var additionalSchemaInfo in schemaInfos.Skip(1))
Expand Down
6 changes: 3 additions & 3 deletions src/GraphQL.Conventions/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
[assembly: AssemblyProduct("GraphQL.Conventions")]
[assembly: AssemblyCopyright("Copyright 2016-2017 Tommy Lillehagen. All rights reserved.")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.1.1.0")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyInformationalVersion("1.1.1.0")]
[assembly: CLSCompliant(false)]

[assembly: InternalsVisibleTo("GraphQL.Conventions.Tests")]
19 changes: 17 additions & 2 deletions src/GraphQL.Conventions/Types/Id.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ private Id(string typeName, string identifier, bool? serializeUsingColon = null)
? $"{typeName}:{identifier}"
: $"{typeName}{identifier}";
_encodedIdentifier = Types.Utilities.Identifier.Encode(_unencodedIdentifier);
if (string.IsNullOrWhiteSpace(identifier))
{
throw new ArgumentException($"Invalid blank identifier '{_unencodedIdentifier}'.");
}
if (string.IsNullOrWhiteSpace(_unencodedIdentifier))
{
throw new ArgumentException($"Unable to encode identifier '{_unencodedIdentifier}'.");
}
}

public Id(string encodedIdentifier)
Expand Down Expand Up @@ -56,7 +64,9 @@ public override string ToString() =>
_encodedIdentifier;

public bool IsIdentifierForType(Type type) =>
_unencodedIdentifier.StartsWith(GetTypeName(type));
_unencodedIdentifier.Contains(":")
? _unencodedIdentifier.StartsWith(GetTypeName(type) + ":")
: _unencodedIdentifier.StartsWith(GetTypeName(type));

public bool IsIdentifierForType<TType>() =>
IsIdentifierForType(typeof(TType));
Expand All @@ -69,7 +79,12 @@ public string IdentifierForType(Type type)
throw new ArgumentException(
$"Expected identifier of type '{typeName}' (unencoded identifier '{_unencodedIdentifier}').");
}
return _unencodedIdentifier.Remove(0, typeName.Length).TrimStart(':');
var underlyingIdentifier = _unencodedIdentifier.Remove(0, typeName.Length).TrimStart(':');
if (string.IsNullOrWhiteSpace(underlyingIdentifier))
{
throw new ArgumentException($"Invalid blank identifier '{_unencodedIdentifier}'.");
}
return underlyingIdentifier;
}

public string IdentifierForType<TType>() =>
Expand Down
2 changes: 0 additions & 2 deletions src/GraphQL.Conventions/Types/Resolution/ObjectReflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using GraphQL.Conventions.Execution;
using GraphQL.Conventions.Handlers;
using GraphQL.Conventions.Types.Descriptors;
using GraphQL.Conventions.Types.Resolution.Extensions;
using GraphQL.Conventions.Utilities;

namespace GraphQL.Conventions.Types.Resolution
{
Expand Down
33 changes: 33 additions & 0 deletions src/GraphQL.Conventions/Types/Utilities/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace GraphQL.Conventions
{
public static class EnumerableExtensions
{
public static IEnumerable<V> Select<U, V>(this NonNull<IEnumerable<U>> list, Func<U, V> selector) =>
list.Value.Select(selector);

public static IEnumerable<V> Select<U, V>(this NonNull<List<U>> list, Func<U, V> selector) =>
list.Value.Select(selector);

public static IEnumerable<U> Where<U>(this NonNull<IEnumerable<U>> list, Func<U, bool> predicate) =>
list.Value.Where(predicate);

public static IEnumerable<U> Where<U>(this NonNull<List<U>> list, Func<U, bool> predicate) =>
list.Value.Where(predicate);

public static U FirstOrDefault<U>(this NonNull<IEnumerable<U>> list, Func<U, bool> predicate) =>
list.Value.FirstOrDefault(predicate);

public static U FirstOrDefault<U>(this NonNull<List<U>> list, Func<U, bool> predicate) =>
list.Value.FirstOrDefault(predicate);

public static U First<U>(this NonNull<IEnumerable<U>> list, Func<U, bool> predicate) =>
list.Value.First(predicate);

public static U First<U>(this NonNull<List<U>> list, Func<U, bool> predicate) =>
list.Value.First(predicate);
}
}
19 changes: 19 additions & 0 deletions src/GraphQL.Conventions/Types/Utilities/Utilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Linq;

namespace GraphQL.Conventions
{
public static class Utilities
{
public static NonNull<IEnumerable<T>> NonNull<T>(IEnumerable<T> items) => items.ToList();

public static NonNull<List<T>> NonNull<T>(List<T> items) => items;

public static NonNull<string> NonNull(string str) => str;

public static Id Id(string encodedIdentifier) => new Id(encodedIdentifier);

public static Id Id<T>(string unencodedIdentifier, bool serializeUsingColon = true) =>
GraphQL.Conventions.Id.New<T>(unencodedIdentifier, serializeUsingColon);
}
}
4 changes: 2 additions & 2 deletions src/GraphQL.Conventions/Utilities/CachedRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Collections.Generic;
using System.Linq;

namespace GraphQL.Conventions.Utilities
namespace GraphQL.Conventions
{
public class CachedRegistry<TKey, TValue>
class CachedRegistry<TKey, TValue>
{
private object _lock = new object();

Expand Down
15 changes: 15 additions & 0 deletions src/GraphQL.Conventions/Utilities/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace GraphQL.Conventions
{
public interface ILogger
{
void Trace(string message);

void Info(string message);

void Warning(string message, Exception exception = null);

void Error(string message, Exception exception = null);
}
}
2 changes: 1 addition & 1 deletion src/GraphQL.Conventions/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.1.0-*",
"version": "1.1.1-*",
"description": "GraphQL Conventions for .NET",
"authors": [
"Tommy Lillehagen"
Expand Down
Loading

0 comments on commit 573b877

Please sign in to comment.