Skip to content

Commit

Permalink
Merge pull request #26 from graphql-dotnet/schema-printer-and-web-ext…
Browse files Browse the repository at this point in the history
…ensions

Fix schema printer, add web extensions, improve attribute registration
  • Loading branch information
tlil authored Feb 17, 2017
2 parents 54db9b7 + 8e128be commit 4ca40f2
Show file tree
Hide file tree
Showing 13 changed files with 432 additions and 24 deletions.
2 changes: 1 addition & 1 deletion deps/graphql-dotnet
20 changes: 20 additions & 0 deletions src/GraphQL.Conventions/Adapters/Engine/GraphQLEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ public GraphQLEngine WithSubscription<TSubscription>()
return this;
}

public GraphQLEngine WithAttributesFromAssembly(System.Type assemblyType)
{
_typeResolver.RegisterAttributesInAssembly(assemblyType);
return this;
}

public GraphQLEngine WithAttributesFromAssembly<TAssemblyType>()
{
return WithAttributesFromAssembly(typeof(TAssemblyType));
}

public GraphQLEngine WithAttributesFromAssemblies(IEnumerable<System.Type> assemblyTypes)
{
foreach (var assemblyType in assemblyTypes)
{
WithAttributesFromAssembly(assemblyType);
}
return this;
}

public GraphQLEngine BuildSchema(params System.Type[] types)
{
if (_schema == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

namespace GraphQL.Conventions.Attributes.Collectors
{
public class AttributeCollector
{
public static Type ApplicationType { get; set; }
}

public class AttributeCollector<TAttribute>
where TAttribute : IAttribute
{
Expand Down Expand Up @@ -53,10 +48,6 @@ public int CompareTo(WrappedAttribute other)
public AttributeCollector()
{
DiscoverDefaultAttributes();
if (AttributeCollector.ApplicationType != null)
{
DiscoverDefaultAttributes(AttributeCollector.ApplicationType);
}
}

public List<TAttribute> CollectAttributes(ICustomAttributeProvider obj)
Expand All @@ -73,7 +64,7 @@ protected void AddDefaultAttributes(params TAttribute[] defaultAttributes)
{
_defaultAttributes = new List<TAttribute>();
}
_defaultAttributes.AddRange(defaultAttributes);
_defaultAttributes.AddRange(defaultAttributes.Except(_defaultAttributes));
}

protected virtual IEnumerable<TAttribute> CollectCoreAttributes(ICustomAttributeProvider obj)
Expand All @@ -86,7 +77,7 @@ private IEnumerable<WrappedAttribute> CollectAttributesImplementation(ICustomAtt
.Union(FlattenAttributeCollection(_defaultAttributes))
.Union(GetAttributes(obj));

private void DiscoverDefaultAttributes(Type assemblyType = null)
internal void DiscoverDefaultAttributes(Type assemblyType = null)
{
if (assemblyType == null)
{
Expand All @@ -102,14 +93,6 @@ private void DiscoverDefaultAttributes(Type assemblyType = null)
AddDefaultAttributes(defaultAttributes);
}

private void DiscoverDefaultAttributes()
{
if (_defaultAttributes == null)
{
DiscoverDefaultAttributes(null);
}
}

private bool IsDefaultAttribute(Type type)
{
return type.GetTypeInfo()
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.2.0")]
[assembly: AssemblyFileVersion("1.1.2.0")]
[assembly: AssemblyInformationalVersion("1.1.2.0")]
[assembly: AssemblyVersion("1.1.3.0")]
[assembly: AssemblyFileVersion("1.1.3.0")]
[assembly: AssemblyInformationalVersion("1.1.3.0")]
[assembly: CLSCompliant(false)]

[assembly: InternalsVisibleTo("GraphQL.Conventions.Tests")]
6 changes: 6 additions & 0 deletions src/GraphQL.Conventions/Handlers/MetaDataAttributeHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Reflection;
using GraphQL.Conventions.Attributes;
Expand Down Expand Up @@ -39,5 +40,10 @@ public void DeriveMetaData(GraphEntityInfo entity, ICustomAttributeProvider obj)
fieldInfo.ExecutionFilters.AddRange(_executionFilterCollector.CollectAttributes(obj));
}
}

internal void DiscoverAndRegisterDefaultAttributesInAssembly(Type assemblyType)
{
_collector.DiscoverDefaultAttributes(assemblyType);
}
}
}
5 changes: 5 additions & 0 deletions src/GraphQL.Conventions/Types/Resolution/ObjectReflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public GraphEntityInfo ApplyAttributes(GraphEntityInfo entityInfo)
return entityInfo;
}

internal void DiscoverAndRegisterDefaultAttributesInAssembly(Type assemblyType)
{
_metaDataHandler.DiscoverAndRegisterDefaultAttributesInAssembly(assemblyType);
}

private void DeriveInterfaces(GraphTypeInfo type)
{
var typeInfo = GetTypeInfo(type);
Expand Down
5 changes: 5 additions & 0 deletions src/GraphQL.Conventions/Types/Resolution/TypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public TypeRegistration LookupType(TypeInfo typeInfo)
return null;
}

public void RegisterAttributesInAssembly(Type assemblyType)
{
_reflector.DiscoverAndRegisterDefaultAttributesInAssembly(assemblyType);
}

public IDependencyInjector DependencyInjector { get; set; }

public GraphSchemaInfo ActiveSchema { get; set; }
Expand Down
16 changes: 16 additions & 0 deletions src/GraphQL.Conventions/Web/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.IO;

namespace GraphQL.Conventions.Web
{
public static class Extensions
{
public static Request ToGraphQLRequest(this Stream stream) =>
Request.New(stream);

public static Response ToResponse(this ExecutionResult result, Request request, GraphQLEngine engine) =>
new Response(request, result, engine.SerializeResult(result));

public static string IdentifierForTypeOrNull<T>(this Id id) =>
id.IsIdentifierForType<T>() ? id.IdentifierForType<T>() : null;
}
}
11 changes: 11 additions & 0 deletions src/GraphQL.Conventions/Web/IRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;

namespace GraphQL.Conventions.Web
{
public interface IRequestHandler
{
Task<Response> ProcessRequest(Request request, IUserContext userContext);

string DescribeSchema(bool returnJson = false);
}
}
94 changes: 94 additions & 0 deletions src/GraphQL.Conventions/Web/Request.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace GraphQL.Conventions.Web
{
public class Request
{
static readonly IRequestDeserializer _requestDeserializer = new RequestDeserializer();

static readonly Regex _regexSuperfluousWhitespace =
new Regex(@"[ \t\r\n]{1,}", RegexOptions.Multiline | RegexOptions.Compiled);

readonly string _queryId;

readonly QueryInput _queryInput;

readonly Exception _exception;

public static Request New(Stream stream)
{
stream.Seek(0, SeekOrigin.Begin);
var requestBody = new StreamReader(stream).ReadToEnd();
return New(requestBody);
}

public static Request New(string requestBody)
{
try
{
var queryInput = _requestDeserializer.GetQueryFromRequestBody(requestBody);
return New(queryInput);
}
catch (Exception ex)
{
return InvalidInput(ex);
}
}

public static Request New(QueryInput queryInput)
{
return new Request(queryInput);
}

public static Request InvalidInput(Exception exception)
{
return new Request(exception);
}

Request()
{
_queryId = Guid.NewGuid().ToString();
}

Request(QueryInput queryInput)
: this()
{
_queryInput = queryInput;
}

Request(Exception exception)
: this()
{
_exception = exception;
}

public string QueryId => _queryId;

public string QueryString => _queryInput?.QueryString ?? string.Empty;

public Dictionary<string, object> Variables => _queryInput?.Variables;

public string OperationName => _queryInput?.OperationName;

public bool IsValid => _exception == null;

public Exception Error => _exception;

public string MinifiedQueryString => MinifyString(QueryString);

public string MinifiedVariablesString => MinifyString(JsonConvert.SerializeObject(Variables));

static string MinifyString(string input)
{
if (input == null)
{
return string.Empty;
}
return _regexSuperfluousWhitespace.Replace(input, @" ").Trim();
}
}
}
Loading

0 comments on commit 4ca40f2

Please sign in to comment.