public static IServiceCollection AddQueryExecutor( this IServiceCollection services, IQueryExecutionOptionsAccessor options) { QueryExecutionBuilder.BuildDefault(services, options); return(services); }
public async Task RequestCustomContext() { // arrange var options = new Mock <IQueryExecutionOptionsAccessor>(); options .SetupGet(o => o.ExecutionTimeout) .Returns(TimeSpan.FromSeconds(30)); ISchema schema = CreateSchema(ExecutionScope.Request); IQueryExecuter executer = QueryExecutionBuilder .BuildDefault(schema, options.Object); // act var results = new List <IExecutionResult>(); results.Add(await executer.ExecuteAsync( new QueryRequest("{ a: a b: a }"))); results.Add(await executer.ExecuteAsync( new QueryRequest("{ a: a b: a }"))); results.Add(await executer.ExecuteAsync( new QueryRequest("{ a: a b: a }"))); results.Add(await executer.ExecuteAsync( new QueryRequest("{ a: a b: a }"))); // assert Assert.Collection(results, t => Assert.Null(t.Errors), t => Assert.Null(t.Errors), t => Assert.Null(t.Errors), t => Assert.Null(t.Errors)); results.Snapshot(); }
public void Schema_Is_Correctly_Set() { // arrange var serviceCollection = new ServiceCollection(); serviceCollection .AddStarWarsRepositories() .AddSingleton <IBatchQueryExecutor, BatchQueryExecutor>(); QueryExecutionBuilder.BuildDefault(serviceCollection); serviceCollection.AddSingleton <ISchema>(sp => SchemaBuilder.New() .AddStarWarsTypes() .AddExportDirectiveType() .AddServices(sp) .Create()); IServiceProvider services = serviceCollection.BuildServiceProvider(); // act var executor = services.GetService <IBatchQueryExecutor>(); // assert Assert.Equal( services.GetService <ISchema>(), executor.Schema); }
public static IServiceCollection AddGraphQLWitName( this IServiceCollection services, string schemaName, Func <IServiceProvider, ISchema> schemaFactory, IQueryExecutionOptionsAccessor options) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (schemaFactory == null) { throw new ArgumentNullException(nameof(schemaFactory)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } QueryExecutionBuilder.BuildDefault(services, schemaName, options); return(services.AddSchema(schemaName, schemaFactory) .AddBatchQueryExecutor(schemaName)); }
public static IServiceCollection AddGraphQLWithName( this IServiceCollection services, string schemaName, Action <ISchemaConfiguration> configure, IQueryExecutionOptionsAccessor options) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configure == null) { throw new ArgumentNullException(nameof(configure)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } QueryExecutionBuilder.BuildDefault(services, schemaName, options); return(services.AddSchema(schemaName, s => Schema.Create(c => { c.RegisterServiceProvider(s); configure(c); })) .AddBatchQueryExecutor(schemaName)); }
public static IServiceCollection AddGraphQL( this IServiceCollection services, string schemaSource, Action <ISchemaConfiguration> configure) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (string.IsNullOrEmpty(schemaSource)) { throw new ArgumentNullException(nameof(schemaSource)); } if (configure == null) { throw new ArgumentNullException(nameof(configure)); } QueryExecutionBuilder.BuildDefault(services); return(services.AddSchema(s => Schema.Create(schemaSource, c => { c.RegisterServiceProvider(s); configure(c); })) .AddSingleton <IBatchQueryExecutor, BatchQueryExecutor>()); }
public static IServiceCollection AddGraphQL( this IServiceCollection serviceCollection, string schemaSource, Action <ISchemaConfiguration> configure, IQueryExecutionOptionsAccessor options) { if (serviceCollection == null) { throw new ArgumentNullException(nameof(serviceCollection)); } if (string.IsNullOrEmpty(schemaSource)) { throw new ArgumentNullException(nameof(schemaSource)); } if (configure == null) { throw new ArgumentNullException(nameof(configure)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } QueryExecutionBuilder.BuildDefault(serviceCollection, options); return(serviceCollection.AddSchema(s => Schema.Create(schemaSource, c => { c.RegisterServiceProvider(s); configure(c); }))); }
public async Task Batch_Is_Null() { // arrange var serviceCollection = new ServiceCollection(); serviceCollection .AddStarWarsRepositories() .AddSingleton <IBatchQueryExecutor, BatchQueryExecutor>(); QueryExecutionBuilder.BuildDefault(serviceCollection); serviceCollection.AddSingleton <ISchema>(sp => SchemaBuilder.New() .AddStarWarsTypes() .AddExportDirectiveType() .AddServices(sp) .Create()); IServiceProvider services = serviceCollection.BuildServiceProvider(); var executor = services.GetService <IBatchQueryExecutor>(); // act Func <Task> action = () => executor.ExecuteAsync( null, CancellationToken.None); // assert await Assert.ThrowsAsync <ArgumentNullException>(action); }
public static IServiceCollection AddQueryExecutor( this IServiceCollection services, string schemaName) { QueryExecutionBuilder.BuildDefault(services); return(services); }
public async Task RequestDataLoader() { // arrange ISchema schema = CreateSchema(ExecutionScope.Request); IQueryExecutionOptionsAccessor options = CreateOptions(); IQueryExecuter executer = QueryExecutionBuilder .BuildDefault(schema, options); // act List <IExecutionResult> results = new List <IExecutionResult>(); results.Add(await executer.ExecuteAsync(new QueryRequest( @"{ a: withDataLoader(key: ""a"") b: withDataLoader(key: ""b"") }"))); results.Add(await executer.ExecuteAsync(new QueryRequest( @"{ a: withDataLoader(key: ""a"") }"))); results.Add(await executer.ExecuteAsync(new QueryRequest( @"{ c: withDataLoader(key: ""c"") }"))); results.Add(await executer.ExecuteAsync(new QueryRequest( "{ loads }"))); // assert Assert.Collection(results, t => Assert.Null(t.Errors), t => Assert.Null(t.Errors), t => Assert.Null(t.Errors), t => Assert.Null(t.Errors)); results.Snapshot(); }
public static IServiceCollection AddGraphQL( this IServiceCollection serviceCollection, Func <IServiceProvider, ISchema> schemaFactory) { if (serviceCollection == null) { throw new ArgumentNullException(nameof(serviceCollection)); } if (schemaFactory == null) { throw new ArgumentNullException(nameof(schemaFactory)); } QueryExecutionBuilder.BuildDefault(serviceCollection); return(serviceCollection.AddSchema(schemaFactory)); }
public async Task GlobalDataLoader() { // arrange ISchema schema = CreateSchema(ExecutionScope.Global); IQueryExecutionOptionsAccessor options = CreateOptions(); IQueryExecuter executer = QueryExecutionBuilder .BuildDefault(schema, options); // act List <IExecutionResult> results = new List <IExecutionResult>(); results.Add(await executer.ExecuteAsync(new QueryRequest( @"{ a: withDataLoader(key: ""a"") b: withDataLoader(key: ""b"") }"))); results.Add(await executer.ExecuteAsync(new QueryRequest( @"{ a: withDataLoader(key: ""a"") }"))); results.Add(await executer.ExecuteAsync(new QueryRequest( @"{ c: withDataLoader(key: ""c"") }"))); // assert Assert.Collection(results, t => Assert.Null(t.Errors), t => Assert.Null(t.Errors), t => Assert.Null(t.Errors)); results.Snapshot(); var keyLoads = new HashSet <string>(); var loads = (IQueryExecutionResult)await executer .ExecuteAsync(new QueryRequest("{ loads }")); foreach (object o in (IEnumerable <object>)loads.Data["loads"]) { string[] keys = o.ToString().Split(','); foreach (string key in keys) { Assert.True(keyLoads.Add(key)); } } }
public static IServiceCollection AddGraphQL( this IServiceCollection services, Func <IServiceProvider, ISchema> schemaFactory) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (schemaFactory == null) { throw new ArgumentNullException(nameof(schemaFactory)); } QueryExecutionBuilder.BuildDefault(services); return(services.AddSchema(schemaFactory) .AddSingleton <IBatchQueryExecutor, BatchQueryExecutor>()); }
public async Task EnsureQueryResultContainsExtensionTracing() { // arrange Schema schema = CreateSchema(); IQueryExecutor executor = QueryExecutionBuilder .BuildDefault(schema, new QueryExecutionOptions { TracingPreference = TracingPreference.Always }); var request = new QueryRequest("{ a }"); // act IExecutionResult result = await executor.ExecuteAsync(request); // assert Assert.NotEmpty(result.Extensions); Assert.True(result.Extensions.ContainsKey("tracing")); }
public static IServiceCollection AddGraphQLWithName( this IServiceCollection services, string schemaName, ISchema schema) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (schema == null) { throw new ArgumentNullException(nameof(schema)); } QueryExecutionBuilder.BuildDefault(services, schemaName); return(services.AddSchema(schemaName, schema) .AddBatchQueryExecutor(schemaName)); }
public static IServiceCollection AddGraphQL( this IServiceCollection serviceCollection, Action <ISchemaConfiguration> configure) { if (serviceCollection == null) { throw new ArgumentNullException(nameof(serviceCollection)); } if (configure == null) { throw new ArgumentNullException(nameof(configure)); } QueryExecutionBuilder.BuildDefault(serviceCollection); return(serviceCollection.AddSchema(s => Schema.Create(c => { c.RegisterServiceProvider(s); configure(c); }))); }
public async Task GlobalCustomContext() { // arrange ISchema schema = CreateSchema(ExecutionScope.Global); IQueryExecuter executer = QueryExecutionBuilder.BuildDefault(schema); // act var results = new List <IExecutionResult>(); results.Add(await executer.ExecuteAsync(new QueryRequest("{ a }"))); results.Add(await executer.ExecuteAsync(new QueryRequest("{ a }"))); results.Add(await executer.ExecuteAsync(new QueryRequest("{ a }"))); results.Add(await executer.ExecuteAsync(new QueryRequest("{ a }"))); // assert Assert.Collection(results, t => Assert.Null(t.Errors), t => Assert.Null(t.Errors), t => Assert.Null(t.Errors), t => Assert.Null(t.Errors)); results.Snapshot(); }
public static IServiceCollection AddGraphQL( this IServiceCollection serviceCollection, ISchema schema, IQueryExecutionOptionsAccessor options) { if (serviceCollection == null) { throw new ArgumentNullException(nameof(serviceCollection)); } if (schema == null) { throw new ArgumentNullException(nameof(schema)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } QueryExecutionBuilder.BuildDefault(serviceCollection, options); return(serviceCollection.AddSchema(schema)); }
public async Task ExtractRemoteQueryFromField() { // arrange string schema_a = @" type Query { foo: Foo } type Foo { name: String }"; string schema_b = @" type Query { bar: Bar } type Bar { name: String }"; string query = @" { foo @schema(name: ""a"") @delegate(path: ""foo"" operation: QUERY) { name @schema(name: ""a"") bar @schema(name: ""b"") @delegate(path: ""bar"" operation: QUERY) { name @schema(name: ""b"") } } }"; DocumentNode queryDocument = Parser.Default.Parse(query); FieldNode fieldSelection = queryDocument.Definitions .OfType <OperationDefinitionNode>().First() .SelectionSet.Selections.OfType <FieldNode>().First() .SelectionSet.Selections.OfType <FieldNode>().Last(); var schemas = new Dictionary <string, IQueryExecuter>(); schemas["a"] = QueryExecutionBuilder.BuildDefault( Schema.Create(schema_a, c => c.Use(next => context => { context.Result = "foo"; return(Task.CompletedTask); }))); schemas["b"] = QueryExecutionBuilder.BuildDefault( Schema.Create(schema_b, c => c.Use(next => context => { context.Result = "bar"; return(Task.CompletedTask); }))); var stitchingContext = new StitchingContext(schemas); var broker = new QueryBroker(); var directive = new Mock <IDirective>(); directive.Setup(t => t.ToObject <DelegateDirective>()) .Returns(new DelegateDirective()); var directiveContext = new Mock <IDirectiveContext>(); directiveContext.SetupGet(t => t.FieldSelection) .Returns(fieldSelection); directiveContext.SetupGet(t => t.Directive) .Returns(directive.Object); directiveContext.Setup(t => t.Service <IStitchingContext>()) .Returns(stitchingContext); // act IExecutionResult response = await broker.RedirectQueryAsync( directiveContext.Object); // assert response.Snapshot(); }
public async Task ExecuteExportObject() { // arrange var serviceCollection = new ServiceCollection(); serviceCollection.AddInMemorySubscriptionProvider(); serviceCollection .AddStarWarsRepositories() .AddSingleton <IBatchQueryExecutor, BatchQueryExecutor>(); QueryExecutionBuilder.BuildDefault(serviceCollection); serviceCollection.AddSingleton <ISchema>(sp => SchemaBuilder.New() .AddStarWarsTypes() .AddExportDirectiveType() .AddServices(sp) .Create()); IServiceProvider services = serviceCollection.BuildServiceProvider(); var executor = services.GetService <IBatchQueryExecutor>(); // act var batch = new List <IReadOnlyQueryRequest> { QueryRequestBuilder.New() .SetQuery( @" mutation firstReview { createReview( episode: NEWHOPE review: { commentary: ""foo"", stars: 4 }) @export(as: ""r"") { commentary stars } }") .Create(), QueryRequestBuilder.New() .SetQuery( @" mutation secondReview { createReview( episode: EMPIRE review: $r) { commentary stars } }") .Create() }; IResponseStream stream = await executor.ExecuteAsync(batch, CancellationToken.None); var results = new List <IReadOnlyQueryResult>(); await foreach (IReadOnlyQueryResult result in stream) { if (result != null) { results.Add(result); } } Assert.Collection(results, r => r.MatchSnapshot(new SnapshotNameExtension("1")), r => r.MatchSnapshot(new SnapshotNameExtension("2"))); }
public async Task ExecuteExportObjectList() { // arrange var serviceCollection = new ServiceCollection(); serviceCollection .AddSingleton <ISchema>(sp => SchemaBuilder.New() .AddServices(sp) .AddExportDirectiveType() .AddDocumentFromString( @" type Query { foo(f: [FooInput]) : [Foo] } type Foo { bar: String! } input FooInput { bar: String! } ") .AddResolver("Query", "foo", c => { var list = c.Argument <List <object> >("f"); if (list == null) { return(new List <object> { new Dictionary <string, object> { { "bar", "123" } } }); } else { list.Add(new Dictionary <string, object> { { "bar", "456" } }); return(list); } }) .Use(next => context => { object o = context.Parent <object>(); if (o is Dictionary <string, object> d && d.TryGetValue( context.ResponseName, out object v)) { context.Result = v; } return(next(context)); }) .Create()) .AddSingleton <IBatchQueryExecutor, BatchQueryExecutor>(); QueryExecutionBuilder.BuildDefault(serviceCollection); IServiceProvider services = serviceCollection.BuildServiceProvider(); var executor = services.GetService <IBatchQueryExecutor>(); // act var batch = new List <IReadOnlyQueryRequest> { QueryRequestBuilder.New() .SetQuery( @"{ foo @export(as: ""b"") { bar } }") .Create(), QueryRequestBuilder.New() .SetQuery( @"{ foo(f: $b) { bar } }") .Create() }; IResponseStream stream = await executor.ExecuteAsync(batch, CancellationToken.None); var results = new List <IReadOnlyQueryResult>(); await foreach (IReadOnlyQueryResult result in stream) { if (result != null) { results.Add(result); } } Assert.Collection(results, r => r.MatchSnapshot(new SnapshotNameExtension("1")), r => r.MatchSnapshot(new SnapshotNameExtension("2"))); }
public async Task Convert_List_To_Single_Value_With_Converters() { // arrange var serviceCollection = new ServiceCollection(); serviceCollection .AddSingleton <ISchema>(sp => SchemaBuilder.New() .AddServices(sp) .AddExportDirectiveType() .AddQueryType(d => { d.Name("Query"); d.Field("foo") .Argument("bar", a => a.Type <ListType <StringType> >()) .Type <ListType <StringType> >() .Resolver <List <string> >(c => { var list = c.Argument <List <string> >("bar"); list.Add("789"); return(list); }); d.Field("baz") .Argument("bar", a => a.Type <StringType>()) .Resolver(c => c.Argument <string>("bar")); }) .Create()) .AddSingleton <IBatchQueryExecutor, BatchQueryExecutor>(); QueryExecutionBuilder.BuildDefault(serviceCollection); IServiceProvider services = serviceCollection.BuildServiceProvider(); var executor = services.GetService <IBatchQueryExecutor>(); // act var batch = new List <IReadOnlyQueryRequest> { QueryRequestBuilder.New() .SetQuery( @"query foo1($b1: [String]) { foo(bar: $b1) @export(as: ""b2"") }") .AddVariableValue("b1", new[] { "123" }) .Create(), QueryRequestBuilder.New() .SetQuery( @"query foo2($b2: String) { baz(bar: $b2) }") .Create() }; IResponseStream stream = await executor.ExecuteAsync(batch, CancellationToken.None); var results = new List <IReadOnlyQueryResult>(); await foreach (IReadOnlyQueryResult result in stream) { if (result != null) { results.Add(result); } } Assert.Collection(results, r => r.MatchSnapshot(new SnapshotNameExtension("1")), r => r.MatchSnapshot(new SnapshotNameExtension("2"))); }
public async Task ExecuteExportScalar() { // arrange var serviceCollection = new ServiceCollection(); serviceCollection .AddStarWarsRepositories() .AddSingleton <IBatchQueryExecutor, BatchQueryExecutor>(); QueryExecutionBuilder.BuildDefault(serviceCollection); serviceCollection.AddSingleton <ISchema>(sp => SchemaBuilder.New() .AddStarWarsTypes() .AddExportDirectiveType() .AddServices(sp) .Create()); IServiceProvider services = serviceCollection.BuildServiceProvider(); var executor = services.GetService <IBatchQueryExecutor>(); // act var batch = new List <IReadOnlyQueryRequest> { QueryRequestBuilder.New() .SetQuery( @" query getHero { hero(episode: EMPIRE) { id @export } }") .Create(), QueryRequestBuilder.New() .SetQuery( @" query getHuman { human(id: $id) { name } }") .Create() }; IResponseStream stream = await executor.ExecuteAsync(batch, CancellationToken.None); var results = new List <IReadOnlyQueryResult>(); await foreach (IReadOnlyQueryResult result in stream) { if (result != null) { results.Add(result); } } Assert.Collection(results, r => r.MatchSnapshot(new SnapshotNameExtension("1")), r => r.MatchSnapshot(new SnapshotNameExtension("2"))); }
public async Task ExecuteExportLeafList() { // arrange var serviceCollection = new ServiceCollection(); serviceCollection .AddSingleton <ISchema>(sp => SchemaBuilder.New() .AddServices(sp) .AddExportDirectiveType() .AddQueryType(d => d.Name("Query") .Field("foo") .Argument("bar", a => a.Type <ListType <StringType> >()) .Type <ListType <StringType> >() .Resolver <List <string> >(c => { var list = c.Argument <List <string> >("bar"); if (list == null) { return(new List <string> { "123", "456" }); } else { list.Add("789"); return(list); } })) .Create()) .AddSingleton <IBatchQueryExecutor, BatchQueryExecutor>(); QueryExecutionBuilder.BuildDefault(serviceCollection); IServiceProvider services = serviceCollection.BuildServiceProvider(); var executor = services.GetService <IBatchQueryExecutor>(); // act var batch = new List <IReadOnlyQueryRequest> { QueryRequestBuilder.New() .SetQuery( @"{ foo @export(as: ""b"") }") .Create(), QueryRequestBuilder.New() .SetQuery( @"{ foo(bar: $b) }") .Create() }; IResponseStream stream = await executor.ExecuteAsync(batch, CancellationToken.None); var results = new List <IReadOnlyQueryResult>(); await foreach (IReadOnlyQueryResult result in stream) { if (result != null) { results.Add(result); } } Assert.Collection(results, r => r.MatchSnapshot(new SnapshotNameExtension("1")), r => r.MatchSnapshot(new SnapshotNameExtension("2"))); }
public static IQueryExecutor MakeExecutable( this ISchema schema, IQueryExecutionOptionsAccessor options) { return(QueryExecutionBuilder.BuildDefault(schema, options)); }
public static IQueryExecutor MakeExecutable( this ISchema schema) { return(QueryExecutionBuilder.BuildDefault(schema)); }
public async Task ExecuteQueryOnStitchedSchema() { // arrange string schema_a = @" type Query { foo: Foo } type Foo { name: String }"; string schema_b = @" type Query { bar: Bar } type Bar { name: String }"; string schema_stiched = @" type Query { foo: Foo @schema(name: ""a"") @delegate } type Foo { name: String @schema(name: ""a"") bar: Bar @schema(name: ""b"") @delegate } type Bar { name: String @schema(name: ""b"") }"; string query = @" { foo { name bar { name } } }"; DocumentNode queryDocument = Parser.Default.Parse(query); FieldNode fieldSelection = queryDocument.Definitions .OfType <OperationDefinitionNode>().First() .SelectionSet.Selections.OfType <FieldNode>().First() .SelectionSet.Selections.OfType <FieldNode>().Last(); var schemas = new Dictionary <string, IQueryExecuter>(); schemas["a"] = QueryExecutionBuilder.BuildDefault( Schema.Create(schema_a, c => c.Use(next => context => { context.Result = "foo"; return(Task.CompletedTask); }))); schemas["b"] = QueryExecutionBuilder.BuildDefault( Schema.Create(schema_b, c => c.Use(next => context => { context.Result = "bar"; return(Task.CompletedTask); }))); var services = new ServiceCollection(); services.AddSingleton <IStitchingContext>( new StitchingContext(schemas)); services.AddSingleton <IQueryBroker, QueryBroker>(); services.AddSingleton <ISchema>(sp => Schema.Create( schema_stiched, c => { c.RegisterServiceProvider(sp); c.UseStitching(); })); var schema = services.BuildServiceProvider().GetService <ISchema>(); IQueryExecuter executer = schema.MakeExecutable( t => t.UseDefaultPipeline().AddParser <AnnotationQueryParser>()); // act IExecutionResult result = await executer.ExecuteAsync(query); // assert result.Snapshot(); }