Skip to content

Commit

Permalink
Added AddObjectTypeExtension methods to IRequestExecutorBuilder (#7235)
Browse files Browse the repository at this point in the history
  • Loading branch information
glen-84 authored Jul 8, 2024
1 parent 5534565 commit 337f1e2
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,62 @@ public static IRequestExecutorBuilder AddTypeExtension<TExtension>(
return builder.ConfigureSchema(b => b.AddType<TExtension>());
}

/// <summary>
/// Adds an object type extension and applies the <paramref name="configure"/> delegate.
/// </summary>
/// <param name="builder">The GraphQL configuration builder.</param>
/// <param name="configure">A delegate to configure the type.</param>
/// <typeparam name="TExtension">The extension type.</typeparam>
/// <returns>The GraphQL configuration builder.</returns>
public static IRequestExecutorBuilder AddObjectTypeExtension<TExtension>(
this IRequestExecutorBuilder builder,
Action<IObjectTypeDescriptor<TExtension>> configure)
{
return builder.ConfigureSchema(
b => b.AddType(new ObjectTypeExtension<TExtension>(configure)));
}

/// <summary>
/// Adds an object type extension and applies an optional <paramref name="configure"/> delegate.
/// </summary>
/// <param name="builder">The GraphQL configuration builder.</param>
/// <param name="configure">A delegate to configure the type.</param>
/// <typeparam name="TExtension">The extension type.</typeparam>
/// <typeparam name="TExtends">The type to extend.</typeparam>
/// <returns>The GraphQL configuration builder.</returns>
public static IRequestExecutorBuilder AddObjectTypeExtension<TExtension, TExtends>(
this IRequestExecutorBuilder builder,
Action<IObjectTypeDescriptor<TExtension>>? configure = null)
{
return builder.ConfigureSchema(
b => b.AddType(new ObjectTypeExtension<TExtension>(d =>
{
d.ExtendsType<TExtends>();
configure?.Invoke(d);
})));
}

/// <summary>
/// Adds an object type extension and applies an optional <paramref name="configure"/> delegate.
/// </summary>
/// <param name="builder">The GraphQL configuration builder.</param>
/// <param name="objectTypeName">The name of the object type to extend.</param>
/// <param name="configure">A delegate to configure the type.</param>
/// <typeparam name="TExtension">The extension type.</typeparam>
/// <returns>The GraphQL configuration builder.</returns>
public static IRequestExecutorBuilder AddObjectTypeExtension<TExtension>(
this IRequestExecutorBuilder builder,
string objectTypeName,
Action<IObjectTypeDescriptor<TExtension>>? configure = null)
{
return builder.ConfigureSchema(
b => b.AddType(new ObjectTypeExtension<TExtension>(d =>
{
d.Name(objectTypeName);
configure?.Invoke(d);
})));
}

public static IRequestExecutorBuilder BindRuntimeType<TRuntimeType, TSchemaType>(
this IRequestExecutorBuilder builder)
where TSchemaType : INamedType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,49 @@ public async Task ExtendObjectTypeAttribute_Extends_SchemaType()
SnapshotExtensions.MatchSnapshot(schema);
}

[Fact]
public async Task AddObjectTypeExtension1_Extends_SchemaType()
{
var schema =
await new ServiceCollection()
.AddGraphQL()
.AddQueryType<QueryType>()
.AddObjectTypeExtension<QueryExtensions2>(
d => d.ExtendsType<QueryType>().Field("foo").Type<IntType>())
.BuildSchemaAsync();

SnapshotExtensions.MatchSnapshot(schema);
}

[Fact]
public async Task AddObjectTypeExtension2_Extends_SchemaType()
{
var schema =
await new ServiceCollection()
.AddGraphQL()
.AddQueryType<QueryType>()
.AddObjectTypeExtension<QueryExtensions2, QueryType>(
d => d.Field("foo").Type<IntType>())
.BuildSchemaAsync();

SnapshotExtensions.MatchSnapshot(schema);
}

[Fact]
public async Task AddObjectTypeExtension3_Extends_SchemaType()
{
var schema =
await new ServiceCollection()
.AddGraphQL()
.AddQueryType<QueryType>()
.AddObjectTypeExtension<QueryExtensions2>(
"Query",
d => d.Field("foo").Type<IntType>())
.BuildSchemaAsync();

SnapshotExtensions.MatchSnapshot(schema);
}

public class FooType : ObjectType<Foo>
{
protected override void Configure(IObjectTypeDescriptor<Foo> descriptor)
Expand Down Expand Up @@ -1147,6 +1190,11 @@ public class QueryExtensions
{
public string Bar() => "baz";
}

public class QueryExtensions2
{
public int AddedField { get; set; }
}
}

#pragma warning restore RCS1102 // Make class static
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
schema {
query: Query
}

type Query {
foo: Int
addedField: Int!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
schema {
query: Query
}

type Query {
foo: Int
addedField: Int!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
schema {
query: Query
}

type Query {
foo: Int
addedField: Int!
}

0 comments on commit 337f1e2

Please sign in to comment.