-
-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
222 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\GraphQL\GraphQL.csproj" /> | ||
<ProjectReference Include="..\GraphQL.StarWars\GraphQL.StarWars.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="wwwroot\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using GraphQL.Http; | ||
using Microsoft.AspNetCore.Http; | ||
using Newtonsoft.Json; | ||
|
||
namespace GraphQL.GraphiQLCore | ||
{ | ||
public class GraphQLMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly GraphQLSettings _settings; | ||
private readonly IDocumentExecuter _executer; | ||
private readonly IDocumentWriter _writer; | ||
|
||
public GraphQLMiddleware( | ||
RequestDelegate next, | ||
GraphQLSettings settings, | ||
IDocumentExecuter executer, | ||
IDocumentWriter writer) | ||
{ | ||
_next = next; | ||
_settings = settings; | ||
_executer = executer; | ||
_writer = writer; | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
if (!IsGraphQLRequest(context)) | ||
{ | ||
await _next(context).ConfigureAwait(false); | ||
return; | ||
} | ||
|
||
await ExecuteAsync(context); | ||
} | ||
|
||
private bool IsGraphQLRequest(HttpContext context) | ||
{ | ||
return context.Request.Path.StartsWithSegments(_settings.Path) | ||
&& string.Equals(context.Request.Method, "POST", StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
private async Task ExecuteAsync(HttpContext context) | ||
{ | ||
string body; | ||
using (var streamReader = new StreamReader(context.Request.Body)) | ||
{ | ||
body = await streamReader.ReadToEndAsync().ConfigureAwait(true); | ||
} | ||
|
||
var request = JsonConvert.DeserializeObject<GraphQLRequest>(body); | ||
|
||
var result = await _executer.ExecuteAsync(_ => | ||
{ | ||
_.Schema = _settings.Schema; | ||
_.Query = request.Query; | ||
_.OperationName = request.OperationName; | ||
_.Inputs = request.Variables.ToInputs(); | ||
}); | ||
|
||
await WriteResponseAsync(context, result); | ||
} | ||
|
||
private async Task WriteResponseAsync(HttpContext context, ExecutionResult result) | ||
{ | ||
var json = _writer.Write(result); | ||
|
||
context.Response.ContentType = "application/json"; | ||
context.Response.StatusCode = result.Errors?.Any() == true ? (int)HttpStatusCode.BadRequest : (int)HttpStatusCode.OK; | ||
|
||
await context.Response.WriteAsync(json); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace GraphQL.GraphiQLCore | ||
{ | ||
public class GraphQLRequest | ||
{ | ||
public string OperationName { get; set; } | ||
public string Query { get; set; } | ||
public string Variables { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using GraphQL.Types; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace GraphQL.GraphiQLCore | ||
{ | ||
public class GraphQLSettings | ||
{ | ||
public PathString Path { get; set; } = "/graphql"; | ||
public ISchema Schema { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.IO; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
namespace GraphQL.GraphiQLCore | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var host = new WebHostBuilder() | ||
.UseKestrel() | ||
.UseContentRoot(Directory.GetCurrentDirectory()) | ||
.UseIISIntegration() | ||
.UseStartup<Startup>() | ||
.Build(); | ||
|
||
host.Run(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:49816/", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"GraphQL.GraphiQLCore": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "http://localhost:49817" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using GraphQL.Http; | ||
using GraphQL.StarWars; | ||
using GraphQL.StarWars.Types; | ||
using GraphQL.Types; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace GraphQL.GraphiQLCore | ||
{ | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddSingleton<IDocumentExecuter, DocumentExecuter>(); | ||
services.AddSingleton<IDocumentWriter, DocumentWriter>(); | ||
|
||
services.AddSingleton<StarWarsData>(); | ||
services.AddSingleton<StarWarsQuery>(); | ||
services.AddSingleton<HumanType>(); | ||
services.AddSingleton<DroidType>(); | ||
services.AddSingleton<CharacterInterface>(); | ||
services.AddSingleton<ISchema>(s => new StarWarsSchema(type => (GraphType) s.GetService(type))); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | ||
{ | ||
loggerFactory.AddConsole(); | ||
|
||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
app.UseMiddleware<GraphQLMiddleware>(new GraphQLSettings | ||
{ | ||
Schema = app.ApplicationServices.GetService<ISchema>() | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters