Skip to content

Commit

Permalink
Data Loader Support (ChilliCream#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Jul 22, 2018
1 parent 793343f commit c242d42
Show file tree
Hide file tree
Showing 67 changed files with 1,490 additions and 551 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018
Copyright (c) 2018 ChilliCream (Michael & Rafael Staib)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

**Hot Chocolate is a GraphQL server for _.NET Core_ and _.NET Classic_**

_Hot Chocolate_ is a GraphQL server and parser implementation based on the current GraphQL [June 2018 specification](http://facebook.github.io/graphql/June2018/) defined by Facebook.
_Hot Chocolate_ is a GraphQL server and parser implementation based on the current GraphQL [June 2018 specification](http://facebook.github.io/graphql/June2018/) defined by Facebook.

We are currently in the process of closing some gaps and hope to finalise Version 1 by September. We have listed the implemented specification parts at the bottom of this readme.

Expand Down Expand Up @@ -269,7 +269,7 @@ Moreover, we are working on the following parts that are not defined in the spec
- [ ] Custom Schema Directives
- [ ] Custom Execution Directives

### Execution Engine
### Execution Engine

- [ ] Custom Context Objects
- [ ] Data Loader Integration / Batched Operations
Expand Down
2 changes: 1 addition & 1 deletion src/Abstractions/GraphQLLiteralParserAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace HotChocolate
{
[AttributeUsage(AttributeTargets.Class)]
public class GraphQLLiteralParserAttribute
public sealed class GraphQLLiteralParserAttribute
: Attribute
{
public GraphQLLiteralParserAttribute(Type type)
Expand Down
2 changes: 1 addition & 1 deletion src/Abstractions/GraphQLNameAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace HotChocolate
[AttributeUsage(AttributeTargets.Class
| AttributeTargets.Property
| AttributeTargets.Method)]
public class GraphQLNameAttribute
public sealed class GraphQLNameAttribute
: Attribute
{
public GraphQLNameAttribute(string name)
Expand Down
10 changes: 10 additions & 0 deletions src/Abstractions/ServiceAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace HotChocolate
{
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class ServiceAttribute
: Attribute
{
}
}
10 changes: 10 additions & 0 deletions src/Abstractions/StateAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace HotChocolate
{
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class StateAttribute
: Attribute
{
}
}
2 changes: 1 addition & 1 deletion src/AspNetCore/QueryMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private async Task HandleRequestAsync(
new Execution.QueryRequest(request.Query, request.OperationName)
{
VariableValues = DeserializeVariables(request.Variables),
InitialValue = null
Services = context.RequestServices
},
cancellationToken).ConfigureAwait(false);

Expand Down
26 changes: 11 additions & 15 deletions src/Core.Tests/Configuration/SchemaConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using HotChocolate.Internal;
using HotChocolate.Resolvers;
using HotChocolate.Runtime;
using HotChocolate.Types;
using Moq;
using Xunit;
Expand All @@ -16,8 +17,7 @@ public class SchemaConfigurationTests
public void BindResolverCollectionToObjectTypeImplicitly()
{
// arrange
ServiceManager serviceManager = new ServiceManager();
SchemaContext schemaContext = new SchemaContext(serviceManager);
SchemaContext schemaContext = new SchemaContext();

ObjectType dummyType = new ObjectType(d =>
{
Expand All @@ -30,7 +30,7 @@ public void BindResolverCollectionToObjectTypeImplicitly()

// act
SchemaConfiguration configuration = new SchemaConfiguration(
serviceManager.RegisterServiceProvider,
schemaContext.RegisterServiceProvider,
schemaContext.Types);
configuration.BindResolver<TestResolverCollectionA>().To<TestObjectA>();

Expand All @@ -55,8 +55,7 @@ public void BindResolverCollectionToObjectTypeExplicitly()
.Returns(new TestResolverCollectionA());
resolverContext.Setup(t => t.Argument<string>("a")).Returns("foo");

ServiceManager serviceManager = new ServiceManager();
SchemaContext schemaContext = new SchemaContext(serviceManager);
SchemaContext schemaContext = new SchemaContext();

ObjectType dummyType = new ObjectType(d =>
{
Expand All @@ -69,7 +68,7 @@ public void BindResolverCollectionToObjectTypeExplicitly()

// act
SchemaConfiguration configuration = new SchemaConfiguration(
serviceManager.RegisterServiceProvider,
schemaContext.RegisterServiceProvider,
schemaContext.Types);
configuration
.BindResolver<TestResolverCollectionA>(BindingBehavior.Explicit)
Expand Down Expand Up @@ -108,13 +107,12 @@ public void BindResolverCollectionToObjectTypeViaName()
d.Field("bar").Type<StringType>();
});

ServiceManager serviceManager = new ServiceManager();
SchemaContext schemaContext = new SchemaContext(serviceManager);
SchemaContext schemaContext = new SchemaContext();
schemaContext.Types.RegisterType(objectType);

// act
SchemaConfiguration configuration = new SchemaConfiguration(
serviceManager.RegisterServiceProvider,
schemaContext.RegisterServiceProvider,
schemaContext.Types);
configuration.BindType<TestObjectB>().To("Dummy");
configuration.BindResolver<TestResolverCollectionB>().To("Dummy")
Expand Down Expand Up @@ -148,13 +146,12 @@ public void DeriveResolverFromObjectTypeProperty()
d.Field("bar").Type<StringType>();
});

ServiceManager serviceManager = new ServiceManager();
SchemaContext schemaContext = new SchemaContext(serviceManager);
SchemaContext schemaContext = new SchemaContext();
schemaContext.Types.RegisterType(objectType);

// act
SchemaConfiguration configuration = new SchemaConfiguration(
serviceManager.RegisterServiceProvider,
schemaContext.RegisterServiceProvider,
schemaContext.Types);
configuration.BindType<TestObjectB>().To("Dummy");

Expand Down Expand Up @@ -186,13 +183,12 @@ public void DeriveResolverFromObjectTypeMethod()
d.Field("bar2").Type<StringType>();
});

ServiceManager serviceManager = new ServiceManager();
SchemaContext schemaContext = new SchemaContext(serviceManager);
SchemaContext schemaContext = new SchemaContext();
schemaContext.Types.RegisterType(objectType);

// act
SchemaConfiguration configuration = new SchemaConfiguration(
serviceManager.RegisterServiceProvider,
schemaContext.RegisterServiceProvider,
schemaContext.Types);
configuration.BindType<TestObjectB>().To("Dummy");

Expand Down
25 changes: 19 additions & 6 deletions src/Core.Tests/Execution/OperationRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Language;
using HotChocolate.Runtime;
using Xunit;

namespace HotChocolate.Execution
Expand All @@ -14,6 +15,11 @@ public async Task ResolveSimpleOneLevelQuery()
{
// arrange
Schema schema = CreateSchema();
var dataLoaderDescriptors =
new DataLoaderDescriptorCollection(schema.DataLoaders);
var dataLoaderState = new DataLoaderState(
schema.Services, dataLoaderDescriptors,
Enumerable.Empty<StateObjectCollection<string>>());
DocumentNode query = Parser.Default.Parse(@"
{
a
Expand All @@ -22,11 +28,11 @@ public async Task ResolveSimpleOneLevelQuery()
.OfType<OperationDefinitionNode>().FirstOrDefault();

// act
OperationExecuter operationRequest =
OperationExecuter operationExecuter =
new OperationExecuter(schema, query, operation);
IExecutionResult result = await operationRequest.ExecuteAsync(
new Dictionary<string, IValueNode>(),
null, CancellationToken.None);
IExecutionResult result = await operationExecuter.ExecuteAsync(
new OperationRequest(schema.Services, dataLoaderState),
CancellationToken.None);

// assert
Assert.NotNull(result);
Expand Down Expand Up @@ -54,16 +60,23 @@ public async Task ExecuteMutationSerially()
cnf.BindResolver(ctx => state = ctx.Argument<int>("newNumber"))
.To("Mutation", "changeTheNumber");
});
var dataLoaderDescriptors =
new DataLoaderDescriptorCollection(schema.DataLoaders);
var dataLoaderState = new DataLoaderState(
schema.Services, dataLoaderDescriptors,
Enumerable.Empty<StateObjectCollection<string>>());

DocumentNode query = Parser.Default.Parse(
FileResource.Open("MutationExecutionQuery.graphql"));
OperationDefinitionNode operation = query.Definitions
.OfType<OperationDefinitionNode>().FirstOrDefault();

// act
OperationExecuter operationRequest =
OperationExecuter operationExecuter =
new OperationExecuter(schema, query, operation);
IExecutionResult result = await operationRequest.ExecuteAsync();
IExecutionResult result = await operationExecuter.ExecuteAsync(
new OperationRequest(schema.Services, dataLoaderState),
CancellationToken.None);

// assert
Assert.Null(result.Errors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ type Query {
",
c =>
{
c.BindResolver<QueryA>().Resolve("hello").With(t => t.Hello);
c.BindResolver<QueryB>().Resolve("world").With(t => t.World);
c.BindResolver<QueryA>().To("Query")
.Resolve("hello").With(t => t.Hello);
c.BindResolver<QueryB>().To("Query")
.Resolve("world").With(t => t.World);
});
}

Expand Down
15 changes: 5 additions & 10 deletions src/Core.Tests/Types/ObjectTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ public class ObjectTypeTests
public void IntializeExplicitFieldWithImplicitResolver()
{
// arrange
var services = new ServiceManager();
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext(services);
var schemaContext = new SchemaContext();

// act
var fooType = new ObjectType<Foo>(
Expand All @@ -35,9 +34,8 @@ public void IntializeExplicitFieldWithImplicitResolver()
public void IntializeImpicitFieldWithImplicitResolver()
{
// arrange
var services = new ServiceManager();
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext(services);
var schemaContext = new SchemaContext();

// act
var fooType = new ObjectType<Foo>();
Expand All @@ -57,9 +55,8 @@ public void IntializeImpicitFieldWithImplicitResolver()
public void EnsureObjectTypeKindIsCorret()
{
// arrange
var services = new ServiceManager();
var errors = new List<SchemaError>();
var context = new SchemaContext(services);
var context = new SchemaContext();

// act
var someObject = new ObjectType<Foo>();
Expand Down Expand Up @@ -101,9 +98,8 @@ public void ObjectTypeWithDynamicField_TypeDeclarationOrderShouldNotMatter()
public void GenericObjectTypes()
{
// arrange
var services = new ServiceManager();
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext(services);
var schemaContext = new SchemaContext();

// act
var genericType = new ObjectType<GenericFoo<string>>();
Expand All @@ -121,9 +117,8 @@ public void GenericObjectTypes()
public void NestedGenericObjectTypes()
{
// arrange
var services = new ServiceManager();
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext(services);
var schemaContext = new SchemaContext();

// act
var genericType = new ObjectType<GenericFoo<GenericFoo<string>>>();
Expand Down
13 changes: 5 additions & 8 deletions src/Core.Tests/Types/TypeFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public void CreateObjectType()
"Simple", "a",
(c, r) => "hello");

var serviceManager = new ServiceManager();
var schemaContext = new SchemaContext(serviceManager);
var schemaContext = new SchemaContext();
schemaContext.Types.RegisterType(scalarType);
schemaContext.Resolvers.RegisterResolver(resolverBinding);

Expand Down Expand Up @@ -68,10 +67,9 @@ public void CreateUnion()
UnionTypeDefinitionNode unionTypeDefinition = document
.Definitions.OfType<UnionTypeDefinitionNode>().First();

var serviceManager = new ServiceManager();
var context = new SchemaContext(serviceManager);
var context = new SchemaContext();
var configuration = new SchemaConfiguration(
serviceManager.RegisterServiceProvider,
context.RegisterServiceProvider,
context.Types);
configuration.RegisterType(new ObjectType(d =>
d.Name("A").Field("a").Type<StringType>()));
Expand Down Expand Up @@ -102,10 +100,9 @@ public void CreateEnum()
EnumTypeDefinitionNode typeDefinition = document
.Definitions.OfType<EnumTypeDefinitionNode>().First();

var serviceManager = new ServiceManager();
var context = new SchemaContext(serviceManager);
var context = new SchemaContext();
var configuration = new SchemaConfiguration(
serviceManager.RegisterServiceProvider,
context.RegisterServiceProvider,
context.Types);

// act
Expand Down
1 change: 1 addition & 0 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<ItemGroup>
<ProjectReference Include="..\Types\Types.csproj" />
<ProjectReference Include="..\Runtime\Runtime.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit c242d42

Please sign in to comment.