Skip to content

Commit c5f288f

Browse files
authored
Add DoNotRegisterAttribute (graphql-dotnet#2810)
1 parent c8d82d3 commit c5f288f

File tree

7 files changed

+36
-4
lines changed

7 files changed

+36
-4
lines changed

docs2/site/docs/getting-started/dependency-injection.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ injection provider, or their `Dispose` methods will not be called. Any dependenc
163163

164164
You can also use the `services.AddGraphTypes()` extension method from the [server](https://github.com/graphql-dotnet/server)
165165
project to scan the calling assembly for classes that implement `IGraphType` and register them all as singletons
166-
within the service provider. For additional options and overloads of this method, see
167-
[GraphQLBuilderCoreExtensions.cs](https://github.com/graphql-dotnet/server/blob/master/src/Core/Extensions/GraphQLBuilderCoreExtensions.cs).
166+
within the service provider. Mark your class with `DoNotRegisterAttribute` if you want to skip registration.
167+
For additional options and overloads of this method, see [GraphQLBuilderCoreExtensions.cs](https://github.com/graphql-dotnet/server/blob/master/src/Core/Extensions/GraphQLBuilderCoreExtensions.cs).
168168

169169
## Nancy TinyIoCContainer
170170

src/GraphQL.ApiTests/net6/GraphQL.approved.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ namespace GraphQL
5454
{
5555
public DoNotMapClrTypeAttribute() { }
5656
}
57+
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.All, AllowMultiple=false, Inherited=true)]
58+
public sealed class DoNotRegisterAttribute : System.Attribute
59+
{
60+
public DoNotRegisterAttribute() { }
61+
}
5762
public class DocumentExecuter : GraphQL.IDocumentExecuter
5863
{
5964
public DocumentExecuter() { }

src/GraphQL.ApiTests/netstandard20+netstandard21/GraphQL.approved.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ namespace GraphQL
5454
{
5555
public DoNotMapClrTypeAttribute() { }
5656
}
57+
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.All, AllowMultiple=false, Inherited=true)]
58+
public sealed class DoNotRegisterAttribute : System.Attribute
59+
{
60+
public DoNotRegisterAttribute() { }
61+
}
5762
public class DocumentExecuter : GraphQL.IDocumentExecuter
5863
{
5964
public DocumentExecuter() { }

src/GraphQL.Tests/DI/GraphQLBuilderTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ public void AddErrorInfoProvider_Null()
666666
public void AddGraphTypes()
667667
{
668668
var typeList = new Type[] {
669+
typeof(MyGraphNotRegistered),
669670
typeof(MyGraph),
670671
typeof(MyScalar),
671672
typeof(IGraphType),
@@ -1411,6 +1412,11 @@ private class MyGraph : ObjectGraphType
14111412
{
14121413
}
14131414

1415+
[DoNotRegister]
1416+
private class MyGraphNotRegistered : ObjectGraphType
1417+
{
1418+
}
1419+
14141420
private class MyScalar : IntGraphType
14151421
{
14161422
}

src/GraphQL/AssemblyExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ internal static class AssemblyExtensions
6060
var clrType = baseType.GetGenericArguments()[0];
6161

6262
//as long as it's not of type 'object', register it
63-
if (clrType != typeof(object))
63+
if (clrType != typeof(object) && !clrType.IsDefined(typeof(DoNotMapClrTypeAttribute)))
6464
typeMappings.Add((clrType, graphType));
6565

6666
//skip to the next type

src/GraphQL/DoNotRegisterAttribute.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using GraphQL.Types;
3+
4+
namespace GraphQL
5+
{
6+
/// <summary>
7+
/// Indicates that <see cref="GraphQLBuilderExtensions.AddGraphTypes(DI.IGraphQLBuilder, System.Reflection.Assembly)"/>
8+
/// should skip this class when scanning an assembly for classes that implement <see cref="IGraphType"/>
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
11+
public sealed class DoNotRegisterAttribute : Attribute
12+
{
13+
}
14+
}

src/GraphQL/GraphQLBuilderExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ public static IGraphQLBuilder AddGraphTypes(this IGraphQLBuilder builder)
393393
/// the graph types will effectively be scoped graph types. If the schema is a singleton schema,
394394
/// the graph types will effectively be singleton graph types.
395395
/// <br/><br/>
396+
/// Skips classes where the class is marked with the <see cref="DoNotRegisterAttribute"/>.
397+
/// <br/><br/>
396398
/// Also registers <see cref="EnumerationGraphType{TEnum}"/>, <see cref="ConnectionType{TNodeType}"/>,
397399
/// <see cref="ConnectionType{TNodeType, TEdgeType}"/>, <see cref="EdgeType{TNodeType}"/>,
398400
/// <see cref="InputObjectGraphType{TSourceType}"/>, <see cref="AutoRegisteringInputObjectGraphType{TSourceType}"/>, and
@@ -409,7 +411,7 @@ public static IGraphQLBuilder AddGraphTypes(this IGraphQLBuilder builder, Assemb
409411
throw new ArgumentNullException(nameof(assembly));
410412

411413
foreach (var type in assembly.GetTypes()
412-
.Where(x => x.IsClass && !x.IsAbstract && typeof(IGraphType).IsAssignableFrom(x)))
414+
.Where(x => x.IsClass && !x.IsAbstract && typeof(IGraphType).IsAssignableFrom(x) && !x.IsDefined(typeof(DoNotRegisterAttribute))))
413415
{
414416
builder.TryRegister(type, type, ServiceLifetime.Transient);
415417
}

0 commit comments

Comments
 (0)