Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AddObjectTypeExtension methods to IRequestExecutorBuilder #7235

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!
}
Loading