Skip to content

Commit

Permalink
Merge pull request #205 from ChilliCream/next-version
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Jul 30, 2018
2 parents 24cbe64 + 31b2cbf commit cfeba2f
Show file tree
Hide file tree
Showing 218 changed files with 6,982 additions and 925 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,7 @@ We currently support the following parts of the current [draft spec](http://face

### Validation

- [ ] Validation

For a detailed view of which validation rule is currently implemented, have a look at our issues.
- [ ] [Validation](https://github.com/ChilliCream/hotchocolate/projects/3)

### Execution

Expand Down Expand Up @@ -271,8 +269,8 @@ Moreover, we are working on the following parts that are not defined in the spec

### Execution Engine

- [ ] Custom Context Objects
- [ ] Data Loader Integration / Batched Operations
- [x] Custom Context Objects
- [x] Data Loader Integration / Batched Operations

### Schema Creation

Expand Down
1 change: 1 addition & 0 deletions run-tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dotnet build src
dotnet test src/Runtime.Tests
dotnet test src/Language.Tests
dotnet test src/Core.Tests
dotnet test src/AspNetCore.Tests
1 change: 1 addition & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dotnet build src
dotnet test src/Runtime.Tests
dotnet test src/Language.Tests --no-build
dotnet test src/Core.Tests --no-build
dotnet test src/AspNetCore.Tests --no-build
10 changes: 10 additions & 0 deletions src/Abstractions/DataLoaderAttribute.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 DataLoaderAttribute
: Attribute
{
}
}
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
17 changes: 17 additions & 0 deletions src/Abstractions/IResolverResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace HotChocolate
{
public interface IResolverResult
{
string ErrorMessage { get; }

bool IsError { get; }

object Value { get; }
}

public interface IResolverResult<out TValue>
: IResolverResult
{
new TValue Value { get; }
}
}
31 changes: 31 additions & 0 deletions src/Abstractions/ResolverResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace HotChocolate
{
public readonly struct ResolverResult<TValue>
: IResolverResult<TValue>
{
public ResolverResult(string errorMessage)
{
Value = default;
ErrorMessage = errorMessage
?? throw new ArgumentNullException(nameof(errorMessage));
IsError = true;
}

public ResolverResult(TValue value)
{
Value = default;
ErrorMessage = null;
IsError = false;
}

public TValue Value { get; }

public string ErrorMessage { get; }

public bool IsError { get; }

object IResolverResult.Value => Value;
}
}
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
{
}
}
1 change: 0 additions & 1 deletion src/AspNetCore.Tests/AspNetCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>HotChocolate.AspNetCore.Tests</AssemblyName>
<RootNamespace>HotChocolate.AspNetCore</RootNamespace>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/AspNetCore.Tests/Helpers/TestServerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;

namespace HotChocolate.AspNetCore
{
Expand All @@ -16,6 +17,7 @@ public TestServer Create(Action<ISchemaConfiguration> configure, string route)
.Configure(app => app.UseGraphQL(route))
.ConfigureServices(services =>
{
services.AddScoped<TestService>();
services.AddGraphQL(configure);
});

Expand Down
54 changes: 53 additions & 1 deletion src/AspNetCore.Tests/QueryMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace HotChocolate.AspNetCore
{
public class QueryMiddlewareTests
: IClassFixture<TestServerFactory>
: IClassFixture<TestServerFactory>
{
public QueryMiddlewareTests(TestServerFactory testServerFactory)
{
Expand Down Expand Up @@ -167,6 +167,58 @@ query test($a: FooInput!) {
Assert.Equal(Snapshot.Current(), Snapshot.New(result));
}

[Fact]
public async Task HttpPost_WithScopedService()
{
// arrange
TestServer server = CreateTestServer();
QueryRequestDto request = new QueryRequestDto
{
Query = @"
{
sayHello
}"
};

// act
HttpResponseMessage message = await server.SendRequestAsync(request);

// assert
Assert.Equal(HttpStatusCode.OK, message.StatusCode);

string json = await message.Content.ReadAsStringAsync();
QueryResultDto result = JsonConvert.DeserializeObject<QueryResultDto>(json);
Assert.Null(result.Errors);
Assert.Equal(Snapshot.Current(), Snapshot.New(result));
}

[Fact]
public async Task HttpPost_WithHttpContext()
{
// arrange
TestServer server = CreateTestServer();
QueryRequestDto request = new QueryRequestDto
{
Query = @"
{
requestPath
requestPath2
requestPath3
}"
};

// act
HttpResponseMessage message = await server.SendRequestAsync(request);

// assert
Assert.Equal(HttpStatusCode.OK, message.StatusCode);

string json = await message.Content.ReadAsStringAsync();
QueryResultDto result = JsonConvert.DeserializeObject<QueryResultDto>(json);
Assert.Null(result.Errors);
Assert.Equal(Snapshot.Current(), Snapshot.New(result));
}

private TestServer CreateTestServer()
{
return TestServerFactory.Create(
Expand Down
35 changes: 35 additions & 0 deletions src/AspNetCore.Tests/Schema/Query.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
using System;
using HotChocolate.Resolvers;
using Microsoft.AspNetCore.Http;

namespace HotChocolate.AspNetCore
{
public class Query
{
private readonly TestService _service;
private readonly HttpContext _context;

public Query(TestService testService, HttpContext context)
{
_service = testService
?? throw new ArgumentNullException(nameof(testService));
_context = context
?? throw new ArgumentNullException(nameof(context));
}

public string SayHello()
{
return _service.GetGreetings();
}

public string GetRequestPath()
{
return _context.Request.Path;
}

public string GetRequestPath2(IResolverContext context)
{
return context.Service<HttpContext>().Request.Path;
}

public string GetRequestPath3([Service]HttpContext context)
{
return context.Request.Path;
}

public Foo GetBasic()
{
return new Foo
Expand Down
7 changes: 7 additions & 0 deletions src/AspNetCore.Tests/Schema/TestService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace HotChocolate.AspNetCore
{
public class TestService
{
public string GetGreetings() => "Hello World";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Data": {
"requestPath": "/",
"requestPath2": "/",
"requestPath3": "/"
},
"Errors": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Data": {
"sayHello": "Hello World"
},
"Errors": null
}
40 changes: 40 additions & 0 deletions src/AspNetCore/HttpServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace HotChocolate.AspNetCore
{
public class RequestServiceProvider
: IServiceProvider
{
private readonly IServiceProvider _root;
private readonly Dictionary<Type, object> _services;

public RequestServiceProvider(
IServiceProvider root,
IEnumerable<KeyValuePair<Type, object>> services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

_services = services.ToDictionary(t => t.Key, t => t.Value);
_root = root ?? throw new ArgumentNullException(nameof(root));
}

public object GetService(Type serviceType)
{
if (serviceType == null)
{
throw new ArgumentNullException(nameof(serviceType));
}

if (!_services.TryGetValue(serviceType, out object instance))
{
instance = _root.GetService(serviceType);
}
return instance;
}
}
}
15 changes: 13 additions & 2 deletions 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 = CreateRequestServices(context)
},
cancellationToken).ConfigureAwait(false);

Expand All @@ -74,7 +74,7 @@ private async Task WriteResponseAsync(
{
if (executionResult is IQueryExecutionResult queryResult)
{
// TODO : refactor this ...
// TODO : refactor this, we dont need this string...
string json = queryResult.ToJson();
byte[] buffer = Encoding.UTF8.GetBytes(json);
await response.Body.WriteAsync(buffer, 0, buffer.Length);
Expand Down Expand Up @@ -171,5 +171,16 @@ private IValueNode DeserializeVariableScalarValue(JValue value)
return new StringValueNode(value.Value<string>());
}
}

private IServiceProvider CreateRequestServices(HttpContext context)
{
Dictionary<Type, object> services = new Dictionary<Type, object>
{
{ typeof(HttpContext), context }
};

return new RequestServiceProvider(
context.RequestServices, services);
}
}
}
1 change: 0 additions & 1 deletion src/Benchmark.Tests/Benchmark.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>HotChocolate.Benchmark.Tests</RootNamespace>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Loading

0 comments on commit cfeba2f

Please sign in to comment.