Skip to content

Commit

Permalink
Remove Schema from execution options in favor of GetSchema (#201)
Browse files Browse the repository at this point in the history
* Remove Schema from execution options in favor of GetSchema
* Fix typo in doc
  • Loading branch information
pekkah authored May 9, 2019
1 parent 7046cbf commit c70fe96
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 20 deletions.
3 changes: 2 additions & 1 deletion dev/graphql.dev.chat.web/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using GraphQL.Server.Ui.Playground;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -39,7 +40,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddTankaExecutionOptions()
.Configure<ISchema>((options, schema) =>
{
options.Schema = schema;
options.GetSchema = query => new ValueTask<ISchema>(schema);
});

// signalr server
Expand Down
2 changes: 1 addition & 1 deletion docs/2-server/3-graphql-ws.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ configure this behavior with your own logic.

```csharp
services.AddTankaWebSocketServerWithTracing()
.Configure<IAuthenticationServies>(
.Configure<IAuthenticationService>(
(options, authentication) => options.AcceptAsync = async context =>
{
var token = context.Message.Payload.SelectToken("authToken");
Expand Down
9 changes: 7 additions & 2 deletions src/graphql.server/ExecutionOptions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using tanka.graphql.type;
using System;
using System.Threading.Tasks;
using tanka.graphql.requests;
using tanka.graphql.type;

namespace tanka.graphql.server
{
public class ExecutionOptions
{
public ISchema Schema { get; set; }
public Func<QueryRequest, ValueTask<ISchema>> GetSchema { get; set; }

//public ISchema Schema { get; set; }
}
}
5 changes: 3 additions & 2 deletions src/graphql.server/QueryStreamService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ public async Task<QueryStream> QueryAsync(
{
_logger.Query(query);
var serviceOptions = _optionsMonitor.CurrentValue;
var document = await Parser.ParseDocumentAsync(query.Query);
var document = Parser.ParseDocument(query.Query);
var schema = await serviceOptions.GetSchema(query);
var executionOptions = new graphql.ExecutionOptions
{
Schema = serviceOptions.Schema,
Schema = schema,
Document = document,
OperationName = query.OperationName,
VariableValues = query.Variables,
Expand Down
5 changes: 3 additions & 2 deletions src/graphql.server/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public static OptionsBuilder<GraphQLWSProtocolOptions> AddTankaWebSocketServerWi
public static OptionsBuilder<GraphQLWSProtocolOptions> AddTankaWebSocketServer(this IServiceCollection services)
{
services.AddSingleton<WebSocketServer>();
services.TryAddTransient<IProtocolHandler, GraphQLWSProtocol>();
services.TryAddTransient<IQueryStreamService, QueryStreamService>();
services.TryAddScoped<IProtocolHandler, GraphQLWSProtocol>();
services.TryAddScoped<IQueryStreamService, QueryStreamService>();
services.TryAddScoped<IMessageContextAccessor, MessageContextAccessor>();

return services.AddOptions<GraphQLWSProtocolOptions>();
}
Expand Down
2 changes: 1 addition & 1 deletion src/graphql.server/SignalRServerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static ISignalRServerBuilder AddTankaServerHub(
var services = builder.Services;

services.AddSignalR();
services.TryAddTransient<IQueryStreamService, QueryStreamService>();
services.TryAddScoped<IQueryStreamService, QueryStreamService>();

return builder;
}
Expand Down
4 changes: 4 additions & 0 deletions src/graphql.server/webSockets/GraphQLWSProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ public class GraphQLWSProtocol : IProtocolHandler
{
private readonly GraphQLWSProtocolOptions _options;
private readonly IQueryStreamService _queryStreamService;
private readonly IMessageContextAccessor _messageContextAccessor;
private readonly ILogger<GraphQLWSProtocol> _logger;
private readonly JsonSerializer _serializer;

public GraphQLWSProtocol(
IQueryStreamService queryStreamService,
IOptions<GraphQLWSProtocolOptions> options,
IMessageContextAccessor messageContextAccessor,
ILogger<GraphQLWSProtocol> logger)
{
_queryStreamService = queryStreamService;
_messageContextAccessor = messageContextAccessor;
_logger = logger;
_options = options.Value;
_serializer = JsonSerializer.CreateDefault(new JsonSerializerSettings
Expand All @@ -45,6 +48,7 @@ public ValueTask Handle(MessageContext context)
context.Message.Id,
context.Message.Type);

_messageContextAccessor.Context = context;
return context.Message.Type switch
{
MessageType.GQL_CONNECTION_INIT => HandleInitAsync(context),
Expand Down
12 changes: 12 additions & 0 deletions src/graphql.server/webSockets/IMessageContextAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace tanka.graphql.server.webSockets
{
public interface IMessageContextAccessor
{
MessageContext Context { get; set; }
}

public class MessageContextAccessor : IMessageContextAccessor
{
public MessageContext Context { get; set; }
}
}
5 changes: 1 addition & 4 deletions src/graphql.server/webSockets/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ public WebSocketServer(

public async Task ProcessRequestAsync(HttpContext context)
{
MessageServer messageServer = null;

try
{
_logger.LogInformation($"Processing WebSocket: {context.TraceIdentifier}");
var connection = new WebSocketPipe(_loggerFactory);
var protocol = context.RequestServices
.GetRequiredService<IProtocolHandler>();

messageServer = new SubscriptionServer(protocol);
MessageServer messageServer = new SubscriptionServer(protocol);

Clients.TryAdd(context.Connection, messageServer);
var run = messageServer.RunAsync(connection, context.RequestAborted);
Expand All @@ -43,7 +41,6 @@ public async Task ProcessRequestAsync(HttpContext context)
}
catch (Exception e)
{
messageServer?.Complete(e);
_logger.LogError(e, $"Processing websocket failed: {context.TraceIdentifier}");
throw;
}
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static GraphQLDocument ParseDocument(string document)
/// <returns></returns>
public static Task<GraphQLDocument> ParseDocumentAsync(string document)
{
return Task.Run(() =>
return Task.Factory.StartNew(() =>
{
var lexer = new Lexer();
var parser = new GraphQLParser.Parser(lexer);
Expand Down
2 changes: 1 addition & 1 deletion tests/graphql.server.tests.host/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type Subscription {

// web socket server
services.AddTankaExecutionOptions()
.Configure<ISchema>((options, schema) => options.Schema = schema);
.Configure<ISchema>((options, schema) => options.GetSchema = query => new ValueTask<ISchema>(schema));

services.AddTankaWebSocketServer();
services.AddSignalR(options => { options.EnableDetailedErrors = true; })
Expand Down
17 changes: 12 additions & 5 deletions tests/graphql.server.tests/webSockets/GraphQLWSProtocolFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class GraphQLWSProtocolFacts
{
private IOptions<GraphQLWSProtocolOptions> _options;
private NullLogger<GraphQLWSProtocol> _logger;
private MessageContextAccessor _accessor;

public GraphQLWSProtocolFacts()
{
_options = Options.Create(new GraphQLWSProtocolOptions());
_logger = new NullLogger<GraphQLWSProtocol>();
_accessor = new MessageContextAccessor();
}

protected ValueTask<OperationMessage> ReadWithTimeout(
Expand All @@ -34,13 +36,18 @@ protected ValueTask<OperationMessage> ReadWithTimeout(
return channel.Reader.ReadAsync(cts.Token);
}

public GraphQLWSProtocol CreateSut(IQueryStreamService queryStreamService)
{
return new GraphQLWSProtocol(queryStreamService, _options, _accessor, _logger);
}

[Fact]
public async Task Unknown()
{
/* Given */
var channel = Channel.CreateUnbounded<OperationMessage>();
var queryStreamService = Substitute.For<IQueryStreamService>();
var sut = new GraphQLWSProtocol(queryStreamService, _options, _logger);
var sut = CreateSut(queryStreamService);

var message = new OperationMessage
{
Expand All @@ -66,7 +73,7 @@ public async Task Init()
/* Given */
var channel = Channel.CreateUnbounded<OperationMessage>();
var queryStreamService = Substitute.For<IQueryStreamService>();
var sut = new GraphQLWSProtocol(queryStreamService, _options, _logger);
var sut = CreateSut(queryStreamService);

var message = new OperationMessage
{
Expand All @@ -92,7 +99,7 @@ public async Task Terminate()
/* Given */
var channel = Channel.CreateUnbounded<OperationMessage>();
var queryStreamService = Substitute.For<IQueryStreamService>();
var sut = new GraphQLWSProtocol(queryStreamService, _options, _logger);
var sut = CreateSut(queryStreamService);

var message = new OperationMessage
{
Expand Down Expand Up @@ -120,7 +127,7 @@ public async Task Start()
.ReturnsForAnyArgs(new QueryStream(queryStream));


var sut = new GraphQLWSProtocol(queryStreamService, _options, _logger);
var sut = CreateSut(queryStreamService);

var message = new OperationMessage
{
Expand Down Expand Up @@ -150,7 +157,7 @@ public async Task Stop()
queryStreamService.QueryAsync(null, default)
.ReturnsForAnyArgs(new QueryStream(queryStream));

var sut = new GraphQLWSProtocol(queryStreamService, _options, _logger);
var sut = CreateSut(queryStreamService);
var message = new OperationMessage
{
Id = "1",
Expand Down

0 comments on commit c70fe96

Please sign in to comment.