Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHilus committed May 12, 2017
2 parents 9f56436 + cc4de93 commit 409bd5b
Show file tree
Hide file tree
Showing 17 changed files with 344 additions and 34 deletions.
14 changes: 14 additions & 0 deletions src/Nitrolize.Tests/Integration/Schema/EntityA.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;

namespace Nitrolize.Tests.Integration.Schema
{
public class EntityA
{
public Guid Id { get; set; }

public string Name { get; set; }

public List<EntityB> Entities { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/Nitrolize.Tests/Integration/Schema/EntityB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Nitrolize.Tests.Integration.Schema
{
public class EntityB
{
public Guid Id { get; set; }

public double Value { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/Nitrolize.Tests/Integration/Schema/Mutation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Nitrolize.Types.Base;

namespace Nitrolize.Tests.Integration.Schema
{
public class Mutation : MutationBase
{
}
}
14 changes: 14 additions & 0 deletions src/Nitrolize.Tests/Integration/Schema/Query.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using GraphQL.Types;
using Nitrolize.Extensions;

namespace Nitrolize.Tests.Integration.Schema
{
public class Query : ObjectGraphType
{
public Query()
{
this.Field<ViewerType>("viewer", resolve: context => new Viewer())
.RequiresAuthentication(false);
}
}
}
11 changes: 11 additions & 0 deletions src/Nitrolize.Tests/Integration/Schema/Schema.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Nitrolize.Tests.Integration.Schema
{
public class Schema : GraphQL.Types.Schema
{
public Schema()
{
this.Query = new Query();
this.Mutation = new Mutation();
}
}
}
15 changes: 15 additions & 0 deletions src/Nitrolize.Tests/Integration/Schema/Viewer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Nitrolize.Identification;
using System;

namespace Nitrolize.Tests.Integration.Schema
{
public class Viewer
{
public string Id { get; set; }

public Viewer()
{
this.Id = GlobalId.ToGlobalId("Viewer", Guid.NewGuid().ToString());
}
}
}
56 changes: 56 additions & 0 deletions src/Nitrolize.Tests/Integration/Schema/ViewerType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Nitrolize.Convenience.Attributes;
using Nitrolize.Convenience.Delegates;
using Nitrolize.Models;
using Nitrolize.Types.Base;
using System;
using System.Collections.Generic;

namespace Nitrolize.Tests.Integration.Schema
{
public class ViewerType : ViewerTypeBase
{
[Field(IsAuthenticationRequired = false)]
public Field<EntityA, Guid> EntityA => (context, id) =>
{
var entity = new EntityA
{
Id = new Guid(),
Name = "The Entity A",
Entities = new List<EntityB>()
{
new EntityB { Id = new Guid(), Value = 1.1 },
new EntityB { Id = new Guid(), Value = 2.2 }
}
};
return entity;
};

[List(IsAuthenticationRequired = false)]
public ListField<EntityA> EntityList => (context) =>
{
var list = new List<EntityA>()
{
new EntityA { Id = new Guid(), Name = "No1" },
new EntityA { Id = new Guid(), Name = "No2" }
};

return list;
};

[Connection(IsAuthenticationRequired = false)]
public ConnectionField<EntityA> EntityConnection => (context, parameters) =>
{
var list = new List<EntityA>()
{
new EntityA { Id = new Guid(), Name = "No1" },
new EntityA { Id = new Guid(), Name = "No2" }
};
var connection = new Connection<EntityA, Guid>(list);

connection.PageInfo.HasPreviousPage = false;
connection.PageInfo.HasNextPage = true;

return connection;
};
}
}
127 changes: 127 additions & 0 deletions src/Nitrolize.Tests/Integration/ViewerTypeSpecification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using FluentAssertions;
using GraphQL;
using GraphQL.Http;
using GraphQL.Instrumentation;
using GraphQL.Validation.Complexity;
using Machine.Fakes;
using Machine.Specifications;
using Nitrolize.Types.Base;
using TestSchema = Nitrolize.Tests.Integration.Schema;

namespace Nitrolize.Tests.Schema
{
[Subject(typeof(ViewerTypeBase))]
public class ViewerTypeSpecification : WithSubject<TestSchema.ViewerType>
{
protected static IDocumentExecuter DocumentExecuter = new DocumentExecuter();
protected static IDocumentWriter DocumentWriter = new DocumentWriter(true);
protected static dynamic Result;

protected static object Execute(string query)
{
var result = DocumentExecuter.ExecuteAsync(_ =>
{
_.Schema = new TestSchema.Schema();
_.Query = query;
_.OperationName = null;
_.Inputs = new Inputs();

_.ComplexityConfiguration = new ComplexityConfiguration { MaxDepth = 15 };
_.FieldMiddleware.Use<InstrumentFieldsMiddleware>();
_.UserContext = null;
_.ValidationRules = null;
}).Await().AsTask.Result;

return result.Data;
}
}

public class When_querying_a_field : ViewerTypeSpecification
{
protected static string Query = @"
{
viewer {
entityA(id: ""VXNlciNmOTM2OGNlNC0wNjhkLTQxN2ItYmZiZi0wMDdkMzEyYTA4ZmM="") {
id
name
}
}
}";

Because of = () => Result = Execute(Query);

It should_return_a_property = () => {
var name = (string)Result["viewer"]["entityA"]["name"];
name.Should().Be("The Entity A");
};
}

public class When_querying_a_list : ViewerTypeSpecification
{
protected static string Query = @"
{
viewer {
entityList {
id
name
}
}
}
";

Because of = () => Result = Execute(Query);

It should_return_items = () => ((object)Result["viewer"]["entityList"]).Should().NotBeNull();

It should_return_first_item_name = () => ((object)Result["viewer"]["entityList"][0]["name"]).Should().Be("No1");

It should_return_second_item_name = () => ((object)Result["viewer"]["entityList"][1]["name"]).Should().Be("No2");
}

public class When_querying_a_connection : ViewerTypeSpecification
{
protected static string Query = @"
{
viewer {
entityConnection(first: 100) {
edges {
cursor
node {
id
name
}
}
pageInfo {
startCursor
endCursor
hasPreviousPage
hasNextPage
}
}
}
}
";

Because of = () => Result = Execute(Query);

It should_return_page_info = () => ((object)Result["viewer"]["entityConnection"]["pageInfo"]).Should().NotBeNull();

It should_return_page_info_start_cursor = () => ((object)Result["viewer"]["entityConnection"]["pageInfo"]["startCursor"]).Should().NotBeNull();

It should_return_page_info_end_cursor = () => ((object)Result["viewer"]["entityConnection"]["pageInfo"]["endCursor"]).Should().NotBeNull();

It should_return_page_info_hasPreviousPage = () => ((object)Result["viewer"]["entityConnection"]["pageInfo"]["hasPreviousPage"]).Should().Be(false);

It should_return_page_info_hasNextPage = () => ((object)Result["viewer"]["entityConnection"]["pageInfo"]["hasNextPage"]).Should().Be(true);

It should_return_edges = () => ((object)Result["viewer"]["entityConnection"]["edges"]).Should().NotBeNull();

It should_return_first_edge_cursor = () => ((object)Result["viewer"]["entityConnection"]["edges"][0]["cursor"]).Should().NotBeNull();

It should_return_first_edge_node_name = () => ((object)Result["viewer"]["entityConnection"]["edges"][0]["node"]["name"]).Should().Be("No1");

It should_return_second_edge_cursor = () => ((object)Result["viewer"]["entityConnection"]["edges"][1]["cursor"]).Should().NotBeNull();

It should_return_second_edge_node_name = () => ((object)Result["viewer"]["entityConnection"]["edges"][1]["node"]["name"]).Should().Be("No2");
}
}
6 changes: 3 additions & 3 deletions src/Nitrolize.Tests/TypeExtensionsSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class ExampleClass

It should_have_set_a_correct_name = () => Result.Name.Should().Be("GenericClassExampleClass");

It should_derive_from_original_type = () => Result.IsSubclassOf(typeof(GenericClass<ExampleClass>)).Should().BeTrue();
It should_derive_from_original_type = () => Result.GetTypeInfo().IsSubclassOf(typeof(GenericClass<ExampleClass>)).Should().BeTrue();
}

public abstract class GettingIdSpecification : TypeExtensionsSpecification
Expand Down Expand Up @@ -178,9 +178,9 @@ public class InputModel : Model

It should_convert_ids_of_list_items = () => Result.GetProperty("Items").PropertyType.GetGenericArguments()[0].GetProperty("Id").PropertyType.Name.Should().Be("String");

It should_have_created_class_attribute = () => Result.GetCustomAttributes().Should().NotBeEmpty();
It should_have_created_class_attribute = () => Result.GetTypeInfo().GetCustomAttributes().Should().NotBeEmpty();

It should_have_stored_original_id_type_in_class_attribute = () => Result.GetCustomAttribute<TypeDescriptionProviderAttribute>().TypeName.Should().Contain("Guid");
It should_have_stored_original_id_type_in_class_attribute = () => Result.GetTypeInfo().GetCustomAttribute<TypeDescriptionProviderAttribute>().TypeName.Should().Contain("Guid");

It should_have_omitted_id_property = () => ResultWithoutId.GetProperty("Id").Should().BeNull();
}
Expand Down
24 changes: 23 additions & 1 deletion src/Nitrolize.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,35 @@
"Machine.Fakes.Moq": "2.8.0",
"Machine.Specifications": "0.11.0",
"NETStandard.Library": "1.6.1",
"Nitrolize": "0.1.2"
"Nitrolize": "0.2.0-*",
"System.Reflection": "4.3.0"
},
"frameworks": {
"net461": {
"imports": "dnxcore50"
},
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
],
"dependencies": {
"Microsoft.Extensions.DependencyModel": "1.1.1",
"System.ComponentModel.TypeConverter": "4.3.0",
"System.Reflection.Emit": "4.3.0"
},
"buildOptions": {
"define": [
"NETCOREAPP1_0"
]
}
}
},

"runtimes": {
"win10-x64": {}
},

"configurations": {
"NuGet": {}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Nitrolize/Convenience/Attributes/ListAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Nitrolize.Convenience.Attributes
{
/// <summary>
/// Speficies that the following property is a relay compatible connection.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class ListAttribute : AuthenticationRequiredAttributeBase
{
public ListAttribute()
{
}
}
}
11 changes: 11 additions & 0 deletions src/Nitrolize/Convenience/Delegates/ListField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using GraphQL.Types;

namespace Nitrolize.Convenience.Delegates
{
/// <summary>
/// Delegate for list fields.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="context">The ResolveFieldContext.</param>
public delegate object ListField<TEntity>(ResolveFieldContext<object> context);
}
2 changes: 1 addition & 1 deletion src/Nitrolize/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private static T CloneProperties<T>(object subject, T clone, bool omitIdProperty
return clone;
}

public static IEnumerable<PropertyInfo> GetPropertiesWithAttribute<TAttribute>(this Object subject) where TAttribute : Attribute
public static IEnumerable<PropertyInfo> GetPropertiesWithAttribute<TAttribute>(this object subject) where TAttribute : Attribute
{
return subject.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(TAttribute), false).Any());
}
Expand Down
21 changes: 0 additions & 21 deletions src/Nitrolize/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ public static Type MapToGraphType(this Type type)

// handle object types
return typeof(ObjectType<>).MakeGenericType(type);
// throw new NotImplementedException($"There is no graph type mapping implemented for {type.Name}.");
}

public static bool IsSimpleType(this Type type)
Expand Down Expand Up @@ -418,26 +417,6 @@ public static bool IsGenericType(this Type type)
return type.IsGenericType;
#endif
}

//#if NETCOREAPP1_0
// public static MethodInfo[] GetMethods(this Type type)
// {
// return type.GetTypeInfo().GetMethods();
// }

// public static MethodInfo GetMethod(this Type type, string name)
// {
// return type.GetTypeInfo().GetMethod(name);
// }

// public static Type GetInterface(this Type type, string name)
// {
// return type.GetTypeInfo().GetInterface(name);
// }

//#endif


#endregion
}
}
Loading

0 comments on commit 409bd5b

Please # to comment.