Skip to content

Commit 7d780f7

Browse files
authored
Mark old IGraphQLBuilder extension methods obsolete and create new ones (#602)
1 parent 08b16aa commit 7d780f7

31 files changed

+421
-52
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77

88
[Oo]bj/
99
[Bb]in/
10+
11+
*.received.txt

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,14 @@ public void ConfigureServices(IServiceCollection services)
127127
// Add GraphQL services and configure options
128128
services
129129
.AddSingleton<IChat, Chat>()
130-
.AddSingleton<ChatSchema>()
131-
.AddGraphQL((options, provider) =>
130+
.AddSingleton<ChatSchema>();
131+
132+
MicrosoftDI.GraphQLBuilderExtensions.AddGraphQL(services)
133+
.AddServer(true)
134+
.ConfigureExecution(options =>
132135
{
133136
options.EnableMetrics = Environment.IsDevelopment();
134-
var logger = provider.GetRequiredService<ILogger<Startup>>();
137+
var logger = options.RequestServices.GetRequiredService<ILogger<Startup>>();
135138
options.UnhandledExceptionDelegate = ctx => logger.LogError("{Error} occurred", ctx.OriginalException.Message);
136139
})
137140
// Add required services for GraphQL request/response de/serialization
@@ -140,7 +143,7 @@ public void ConfigureServices(IServiceCollection services)
140143
.AddErrorInfoProvider(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment())
141144
.AddWebSockets() // Add required services for web socket support
142145
.AddDataLoader() // Add required services for DataLoader support
143-
.AddGraphTypes(typeof(ChatSchema)) // Add all IGraphType implementors in assembly which ChatSchema exists
146+
.AddGraphTypes(typeof(ChatSchema).Assembly) // Add all IGraphType implementors in assembly which ChatSchema exists
144147
}
145148

146149
public void Configure(IApplicationBuilder app)
@@ -177,11 +180,14 @@ public void ConfigureServices(IServiceCollection services)
177180
services
178181
.AddRouting()
179182
.AddSingleton<IChat, Chat>()
180-
.AddSingleton<ChatSchema>()
181-
.AddGraphQL((options, provider) =>
183+
.AddSingleton<ChatSchema>();
184+
185+
MicrosoftDI.GraphQLBuilderExtensions.AddGraphQL(services)
186+
.AddServer(true)
187+
.ConfigureExecution(options =>
182188
{
183189
options.EnableMetrics = Environment.IsDevelopment();
184-
var logger = provider.GetRequiredService<ILogger<Startup>>();
190+
var logger = options.RequestServices.GetRequiredService<ILogger<Startup>>();
185191
options.UnhandledExceptionDelegate = ctx => logger.LogError("{Error} occurred", ctx.OriginalException.Message);
186192
})
187193
// It is required when both GraphQL HTTP and GraphQL WebSockets middlewares are mapped to the same endpoint (by default 'graphql').
@@ -192,7 +198,7 @@ public void ConfigureServices(IServiceCollection services)
192198
.AddErrorInfoProvider(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment())
193199
.AddWebSockets() // Add required services for web socket support
194200
.AddDataLoader() // Add required services for DataLoader support
195-
.AddGraphTypes(typeof(ChatSchema)); // Add all IGraphType implementors in assembly which ChatSchema exists
201+
.AddGraphTypes(typeof(ChatSchema).Assembly); // Add all IGraphType implementors in assembly which ChatSchema exists
196202
}
197203

198204
public void Configure(IApplicationBuilder app)
@@ -202,7 +208,7 @@ public void Configure(IApplicationBuilder app)
202208

203209
// this is required for ASP.NET Core routing
204210
app.UseRouting();
205-
211+
206212
app.UseEndpoints(endpoints =>
207213
{
208214
// map websocket middleware for ChatSchema at default path /graphql

samples/Samples.Server/Samples.Server.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<ItemGroup>
1212
<PackageReference Include="Serilog.AspNetCore" Version="4.0.0" />
1313
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
14+
<PackageReference Include="GraphQL.MicrosoftDI" Version="4.6.1" />
1415
</ItemGroup>
1516

1617
<ItemGroup>

samples/Samples.Server/Startup.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections.Generic;
2+
using GraphQL.DataLoader;
3+
using GraphQL.Execution;
24
using GraphQL.Samples.Schemas.Chat;
35
using GraphQL.Server;
46
using GraphQL.Server.Ui.Altair;
@@ -29,20 +31,23 @@ public Startup(IConfiguration configuration, IWebHostEnvironment environment)
2931
// This method gets called by the runtime. Use this method to add services to the container.
3032
public void ConfigureServices(IServiceCollection services)
3133
{
32-
services
33-
.AddSingleton<IChat, Chat>()
34-
.AddSingleton<ChatSchema>()
35-
.AddGraphQL((options, provider) =>
34+
services.AddSingleton<IChat, Chat>();
35+
36+
MicrosoftDI.GraphQLBuilderExtensions.AddGraphQL(services)
37+
.AddServer(true)
38+
.AddSchema<ChatSchema>()
39+
.ConfigureExecution(options =>
3640
{
3741
options.EnableMetrics = Environment.IsDevelopment();
38-
var logger = provider.GetRequiredService<ILogger<Startup>>();
42+
var logger = options.RequestServices.GetRequiredService<ILogger<Startup>>();
3943
options.UnhandledExceptionDelegate = ctx => logger.LogError("{Error} occurred", ctx.OriginalException.Message);
4044
})
41-
.AddSystemTextJson(deserializerSettings => { }, serializerSettings => { })
42-
.AddErrorInfoProvider<CustomErrorInfoProvider>(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment())
45+
.AddSystemTextJson()
46+
.AddErrorInfoProvider<CustomErrorInfoProvider>()
47+
.Configure<ErrorInfoProviderOptions>(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment())
4348
.AddWebSockets()
4449
.AddDataLoader()
45-
.AddGraphTypes(typeof(ChatSchema));
50+
.AddGraphTypes(typeof(ChatSchema).Assembly);
4651
}
4752

4853
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

samples/Samples.Server/StartupWithRouting.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections.Generic;
2+
using GraphQL.DataLoader;
3+
using GraphQL.Execution;
24
using GraphQL.Samples.Schemas.Chat;
35
using GraphQL.Server;
46
using GraphQL.Server.Ui.Altair;
@@ -29,22 +31,25 @@ public StartupWithRouting(IConfiguration configuration, IWebHostEnvironment envi
2931
// This method gets called by the runtime. Use this method to add services to the container.
3032
public void ConfigureServices(IServiceCollection services)
3133
{
32-
services
33-
.AddRouting()
34-
.AddSingleton<IChat, Chat>()
35-
.AddSingleton<ChatSchema>()
36-
.AddGraphQL((options, provider) =>
34+
services.AddRouting();
35+
services.AddSingleton<IChat, Chat>();
36+
37+
MicrosoftDI.GraphQLBuilderExtensions.AddGraphQL(services)
38+
.AddServer(true)
39+
.AddSchema<ChatSchema>()
40+
.ConfigureExecution(options =>
3741
{
3842
options.EnableMetrics = Environment.IsDevelopment();
39-
var logger = provider.GetRequiredService<ILogger<Startup>>();
43+
var logger = options.RequestServices.GetRequiredService<ILogger<Startup>>();
4044
options.UnhandledExceptionDelegate = ctx => logger.LogError("{Error} occurred", ctx.OriginalException.Message);
4145
})
4246
.AddDefaultEndpointSelectorPolicy()
43-
.AddSystemTextJson(deserializerSettings => { }, serializerSettings => { })
44-
.AddErrorInfoProvider(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment())
47+
.AddSystemTextJson()
48+
.AddErrorInfoProvider<CustomErrorInfoProvider>()
49+
.Configure<ErrorInfoProviderOptions>(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment())
4550
.AddWebSockets()
4651
.AddDataLoader()
47-
.AddGraphTypes(typeof(ChatSchema));
52+
.AddGraphTypes(typeof(ChatSchema).Assembly);
4853
}
4954

5055
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

src/All/All.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
<ProjectReference Include="..\Ui.Playground\Ui.Playground.csproj" />
2222
<ProjectReference Include="..\Ui.Voyager\Ui.Voyager.csproj" />
2323

24-
<PackageReference Include="GraphQL.MemoryCache" Version="4.1.0" />
25-
<PackageReference Include="GraphQL.MicrosoftDI" Version="4.1.0" />
26-
<PackageReference Include="GraphQL.SystemReactive" Version="4.1.0" />
24+
<PackageReference Include="GraphQL.MemoryCache" Version="4.6.1" />
25+
<PackageReference Include="GraphQL.MicrosoftDI" Version="4.6.1" />
26+
<PackageReference Include="GraphQL.SystemReactive" Version="4.6.1" />
2727
</ItemGroup>
2828

2929
</Project>

src/Authorization.AspNetCore/GraphQLBuilderAuthorizationExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using GraphQL.Server.Authorization.AspNetCore;
35
using GraphQL.Validation;
@@ -14,6 +16,7 @@ public static class GraphQLBuilderAuthorizationExtensions
1416
/// </summary>
1517
/// <param name="builder">The GraphQL builder.</param>
1618
/// <returns>Reference to the passed <paramref name="builder"/>.</returns>
19+
[Obsolete]
1720
public static IGraphQLBuilder AddGraphQLAuthorization(this IGraphQLBuilder builder)
1821
=> builder.AddGraphQLAuthorization(options => { });
1922

@@ -23,6 +26,7 @@ public static IGraphQLBuilder AddGraphQLAuthorization(this IGraphQLBuilder build
2326
/// <param name="builder">The GraphQL builder.</param>
2427
/// <param name="options">An action delegate to configure the provided <see cref="AuthorizationOptions"/>.</param>
2528
/// <returns>Reference to the passed <paramref name="builder"/>.</returns>
29+
[Obsolete]
2630
public static IGraphQLBuilder AddGraphQLAuthorization(this IGraphQLBuilder builder, Action<AuthorizationOptions> options)
2731
{
2832
builder.Services.TryAddTransient<IClaimsPrincipalAccessor, DefaultClaimsPrincipalAccessor>();
@@ -33,5 +37,34 @@ public static IGraphQLBuilder AddGraphQLAuthorization(this IGraphQLBuilder build
3337

3438
return builder;
3539
}
40+
41+
/// <summary>
42+
/// Adds the GraphQL authorization.
43+
/// </summary>
44+
/// <param name="builder">The GraphQL builder.</param>
45+
/// <returns>Reference to the passed <paramref name="builder"/>.</returns>
46+
public static DI.IGraphQLBuilder AddGraphQLAuthorization(this DI.IGraphQLBuilder builder)
47+
=> builder.AddGraphQLAuthorization(_ => { });
48+
49+
/// <summary>
50+
/// Adds the GraphQL authorization.
51+
/// </summary>
52+
/// <param name="builder">The GraphQL builder.</param>
53+
/// <param name="configure">An action delegate to configure the provided <see cref="AuthorizationOptions"/>.</param>
54+
/// <returns>Reference to the passed <paramref name="builder"/>.</returns>
55+
public static DI.IGraphQLBuilder AddGraphQLAuthorization(this DI.IGraphQLBuilder builder, Action<AuthorizationOptions>? configure)
56+
{
57+
if (!(builder is IServiceCollection services))
58+
throw new NotSupportedException("This method only supports the MicrosoftDI implementation of IGraphQLBuilder.");
59+
services.TryAddTransient<IClaimsPrincipalAccessor, DefaultClaimsPrincipalAccessor>();
60+
services.AddHttpContextAccessor();
61+
if (configure != null)
62+
services.AddAuthorizationCore(configure);
63+
else
64+
services.AddAuthorizationCore();
65+
builder.AddValidationRule<AuthorizationValidationRule>();
66+
67+
return builder;
68+
}
3669
}
3770
}

src/Core/BasicGraphQLExecuter.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using GraphQL.Instrumentation;
6+
using GraphQL.Types;
7+
using Microsoft.Extensions.Options;
8+
9+
namespace GraphQL.Server
10+
{
11+
public class BasicGraphQLExecuter<TSchema> : IGraphQLExecuter<TSchema>
12+
where TSchema : ISchema
13+
{
14+
public TSchema Schema { get; }
15+
16+
private readonly IDocumentExecuter _documentExecuter;
17+
private readonly GraphQLOptions _options;
18+
19+
public BasicGraphQLExecuter(
20+
TSchema schema,
21+
IDocumentExecuter documentExecuter,
22+
IOptions<GraphQLOptions> options)
23+
{
24+
Schema = schema;
25+
26+
_documentExecuter = documentExecuter;
27+
_options = options.Value;
28+
}
29+
30+
public virtual async Task<ExecutionResult> ExecuteAsync(string operationName, string query, Inputs variables, IDictionary<string, object> context, IServiceProvider requestServices, CancellationToken cancellationToken = default)
31+
{
32+
var start = DateTime.UtcNow;
33+
34+
var options = GetOptions(operationName, query, variables, context, requestServices, cancellationToken);
35+
var result = await _documentExecuter.ExecuteAsync(options);
36+
37+
if (options.EnableMetrics)
38+
{
39+
result.EnrichWithApolloTracing(start);
40+
}
41+
42+
return result;
43+
}
44+
45+
protected virtual ExecutionOptions GetOptions(string operationName, string query, Inputs variables, IDictionary<string, object> context, IServiceProvider requestServices, CancellationToken cancellationToken)
46+
{
47+
var opts = new ExecutionOptions
48+
{
49+
Schema = Schema,
50+
OperationName = operationName,
51+
Query = query,
52+
Inputs = variables,
53+
UserContext = context,
54+
CancellationToken = cancellationToken,
55+
ComplexityConfiguration = _options.ComplexityConfiguration,
56+
EnableMetrics = _options.EnableMetrics,
57+
UnhandledExceptionDelegate = _options.UnhandledExceptionDelegate,
58+
MaxParallelExecutionCount = _options.MaxParallelExecutionCount,
59+
RequestServices = requestServices,
60+
};
61+
62+
return opts;
63+
}
64+
}
65+
}

src/Core/Core.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
<ItemGroup>
1111
<!--TODO: remove SystemReactive in v6 -->
12-
<PackageReference Include="GraphQL.SystemReactive" Version="4.1.0" />
13-
<PackageReference Include="GraphQL.DataLoader" Version="4.1.0" />
12+
<PackageReference Include="GraphQL.SystemReactive" Version="4.6.1" />
13+
<PackageReference Include="GraphQL.DataLoader" Version="4.6.1" />
1414
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.0" />
1515
</ItemGroup>
1616

src/Core/DefaultGraphQLExecuter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace GraphQL.Server
1313
{
14+
[Obsolete]
1415
public class DefaultGraphQLExecuter<TSchema> : IGraphQLExecuter<TSchema>
1516
where TSchema : ISchema
1617
{

0 commit comments

Comments
 (0)