Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates the custom sample to .NET 5 #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/AspNetCoreCustom/.babelrc

This file was deleted.

15 changes: 3 additions & 12 deletions src/AspNetCoreCustom/Example/Example.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>latest</LangVersion>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GraphQL.Server.Transports.AspNetCore.SystemTextJson" Version="4.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\StarWars\StarWars.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="public\" />
<PackageReference Include="GraphQL.SystemTextJson" Version="4.5.0" />
<PackageReference Include="GraphQL.Server.Ui.Playground" Version="5.0.2" />
</ItemGroup>

</Project>
28 changes: 8 additions & 20 deletions src/AspNetCoreCustom/Example/GraphQLMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using GraphQL;
using GraphQL.Instrumentation;
using GraphQL.NewtonsoftJson;
using GraphQL.SystemTextJson;
using GraphQL.Types;
using GraphQL.Validation;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using StarWars;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;

namespace Example
Expand All @@ -20,6 +19,7 @@ public class GraphQLMiddleware
private readonly GraphQLSettings _settings;
private readonly IDocumentExecuter _executer;
private readonly IDocumentWriter _writer;
private readonly JsonSerializerOptions _serializerOptions;

public GraphQLMiddleware(
RequestDelegate next,
Expand All @@ -31,6 +31,9 @@ public GraphQLMiddleware(
_settings = settings;
_executer = executer;
_writer = writer;

_serializerOptions = new JsonSerializerOptions();
_serializerOptions.Converters.Add(new InputsConverter());
}

public async Task Invoke(HttpContext context, ISchema schema)
Expand All @@ -52,21 +55,16 @@ private bool IsGraphQLRequest(HttpContext context)

private async Task ExecuteAsync(HttpContext context, ISchema schema)
{
var request = Deserialize<GraphQLRequest>(context.Request.Body);
var request = await JsonSerializer.DeserializeAsync<GraphQLRequest>(context.Request.BodyReader.AsStream(), _serializerOptions);

var result = await _executer.ExecuteAsync(_ =>
{
_.Schema = schema;
_.Query = request?.Query;
_.OperationName = request?.OperationName;
_.Inputs = request?.Variables.ToInputs();
_.Inputs = request?.Variables;
_.UserContext = _settings.BuildUserContext?.Invoke(context);
_.ValidationRules = DocumentValidator.CoreRules.Concat(new [] { new InputValidationRule() });
_.EnableMetrics = _settings.EnableMetrics;
if (_settings.EnableMetrics)
{
_.FieldMiddleware.Use<InstrumentFieldsMiddleware>();
}
});

await WriteResponseAsync(context, result);
Expand All @@ -79,15 +77,5 @@ private async Task WriteResponseAsync(HttpContext context, ExecutionResult resul

await _writer.WriteAsync(context.Response.Body, result);
}

public static T Deserialize<T>(Stream s)
{
using (var reader = new StreamReader(s))
using (var jsonReader = new JsonTextReader(reader))
{
var ser = new JsonSerializer();
return ser.Deserialize<T>(jsonReader);
}
}
}
}
8 changes: 6 additions & 2 deletions src/AspNetCoreCustom/Example/GraphQLRequest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using Newtonsoft.Json.Linq;
using GraphQL;
using System.Text.Json.Serialization;

namespace Example
{
public class GraphQLRequest
{
[JsonPropertyName("operationName")]
public string OperationName { get; set; }

[JsonPropertyName("query")]
public string Query { get; set; }

public JObject Variables { get; set; }
[JsonPropertyName("variables")]
public GraphQL.Inputs Variables { get; set; }
}
}
16 changes: 11 additions & 5 deletions src/AspNetCoreCustom/Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ namespace Example
{
public class Program
{
public static Task Main(string[] args) => Host
.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder => builder.UseStartup<Startup>().UseWebRoot(Path.Combine(Directory.GetCurrentDirectory(), "public")))
.Build()
.RunAsync();
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
13 changes: 13 additions & 0 deletions src/AspNetCoreCustom/Example/Schema.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using GraphQL;

namespace Example
{
[GraphQLMetadata("Query")]
public class QueryType
{
public string Hello()
{
return "World";
}
}
}
32 changes: 15 additions & 17 deletions src/AspNetCoreCustom/Example/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
using GraphQL;
using GraphQL.Server.Ui.Playground;
using GraphQL.Types;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using StarWars;
using StarWars.Types;

namespace Example
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(builder => builder.AddConsole());
services.AddHttpContextAccessor();

services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
services.AddSingleton<IDocumentWriter, GraphQL.SystemTextJson.DocumentWriter>();

services.AddSingleton<StarWarsData>();
services.AddSingleton<StarWarsQuery>();
services.AddSingleton<StarWarsMutation>();
services.AddSingleton<HumanType>();
services.AddSingleton<HumanInputType>();
services.AddSingleton<DroidType>();
services.AddSingleton<CharacterInterface>();
services.AddSingleton<EpisodeEnum>();
services.AddSingleton<ISchema, StarWarsSchema>();

services.AddLogging(builder => builder.AddConsole());
services.AddHttpContextAccessor();
services.AddSingleton<ISchema>(s =>
{
return Schema.For(@"
type Query {
hello: String
}
",
_ => _.Types.Include<QueryType>());
});
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();

app.UseGraphQLPlayground(new PlaygroundOptions { GraphQLEndPoint = "/api/graphql" });

app.UseMiddleware<GraphQLMiddleware>(new GraphQLSettings
{
Path = "/api/graphql",
Expand All @@ -45,9 +46,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
},
EnableMetrics = true
});

app.UseDefaultFiles();
app.UseStaticFiles();
}
}
}
12 changes: 0 additions & 12 deletions src/AspNetCoreCustom/Example/public/index.html

This file was deleted.

6 changes: 2 additions & 4 deletions src/AspNetCoreCustom/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# ASP.NET Core GraphQL Example

```
> npm install
> npm run webpack
> npm start
> browse to http://localhost:3000
> ./run.sh
> browse to http://localhost:3000/ui/playground
```
10 changes: 0 additions & 10 deletions src/AspNetCoreCustom/app/app.css

This file was deleted.

16 changes: 0 additions & 16 deletions src/AspNetCoreCustom/app/app.js

This file was deleted.

Loading