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

Add default arguments to IEndpointRouteBuilder Map methods #4792

Merged
merged 5 commits into from
Mar 10, 2022
Merged
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 @@ -13,6 +13,12 @@ namespace Microsoft.AspNetCore.Builder;
/// </summary>
public static class EndpointRouteBuilderExtensions
{
private const string GraphQLHttpPath = "/graphql";
private const string GraphQLWebSocketPath = "/graphql/ws";
private const string GraphQLSchemaPath = "/graphql/sdl";
private const string GraphQLToolPath = "/graphql/ui";
private const string GraphQLToolRelativeRequestPath = "..";
michaelstaib marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Adds a GraphQL endpoint to the endpoint configurations.
/// </summary>
Expand All @@ -31,7 +37,7 @@ public static class EndpointRouteBuilderExtensions
/// </returns>
public static GraphQLEndpointConventionBuilder MapGraphQL(
this IEndpointRouteBuilder endpointRouteBuilder,
string path = "/graphql",
string path = GraphQLHttpPath,
NameString schemaName = default)
=> MapGraphQL(endpointRouteBuilder, new PathString(path), schemaName);

Expand Down Expand Up @@ -116,7 +122,7 @@ public static GraphQLEndpointConventionBuilder MapGraphQL(
/// </exception>
public static GraphQLHttpEndpointConventionBuilder MapGraphQLHttp(
this IEndpointRouteBuilder endpointRouteBuilder,
string pattern,
string pattern = GraphQLHttpPath,
NameString schemaName = default)
=> MapGraphQLHttp(endpointRouteBuilder, Parse(pattern), schemaName);

Expand All @@ -141,15 +147,18 @@ public static GraphQLHttpEndpointConventionBuilder MapGraphQLHttp(
/// </exception>
public static GraphQLHttpEndpointConventionBuilder MapGraphQLHttp(
this IEndpointRouteBuilder endpointRouteBuilder,
RoutePattern? pattern = default,
RoutePattern pattern,
NameString schemaName = default)
{
if (endpointRouteBuilder is null)
{
throw new ArgumentNullException(nameof(endpointRouteBuilder));
}

pattern ??= Parse("/graphql");
if (pattern is null)
{
throw new ArgumentNullException(nameof(pattern));
}

IApplicationBuilder requestPipeline = endpointRouteBuilder.CreateApplicationBuilder();
NameString schemaNameOrDefault = schemaName.HasValue ? schemaName : Schema.DefaultName;
Expand Down Expand Up @@ -192,7 +201,7 @@ public static GraphQLHttpEndpointConventionBuilder MapGraphQLHttp(
/// </exception>
public static IEndpointConventionBuilder MapGraphQLWebSocket(
this IEndpointRouteBuilder endpointRouteBuilder,
string pattern,
string pattern = GraphQLWebSocketPath,
NameString schemaName = default)
=> MapGraphQLWebSocket(endpointRouteBuilder, Parse(pattern), schemaName);

Expand All @@ -217,15 +226,18 @@ public static IEndpointConventionBuilder MapGraphQLWebSocket(
/// </exception>
public static IEndpointConventionBuilder MapGraphQLWebSocket(
this IEndpointRouteBuilder endpointRouteBuilder,
RoutePattern? pattern = default,
RoutePattern pattern,
NameString schemaName = default)
{
if (endpointRouteBuilder is null)
{
throw new ArgumentNullException(nameof(endpointRouteBuilder));
}

pattern ??= Parse("/graphql/ws");
if (pattern is null)
{
throw new ArgumentNullException(nameof(pattern));
}

IApplicationBuilder requestPipeline = endpointRouteBuilder.CreateApplicationBuilder();
NameString schemaNameOrDefault = schemaName.HasValue ? schemaName : Schema.DefaultName;
Expand Down Expand Up @@ -266,7 +278,7 @@ public static IEndpointConventionBuilder MapGraphQLWebSocket(
/// </exception>
public static IEndpointConventionBuilder MapGraphQLSchema(
this IEndpointRouteBuilder endpointRouteBuilder,
string pattern,
string pattern = GraphQLSchemaPath,
NameString schemaName = default)
=> MapGraphQLSchema(endpointRouteBuilder, Parse(pattern), schemaName);

Expand All @@ -291,15 +303,18 @@ public static IEndpointConventionBuilder MapGraphQLSchema(
/// </exception>
public static IEndpointConventionBuilder MapGraphQLSchema(
this IEndpointRouteBuilder endpointRouteBuilder,
RoutePattern? pattern = default,
RoutePattern pattern,
NameString schemaName = default)
{
if (endpointRouteBuilder is null)
{
throw new ArgumentNullException(nameof(endpointRouteBuilder));
}

pattern ??= Parse("/graphql/sdl");
if (pattern is null)
{
throw new ArgumentNullException(nameof(pattern));
}

IApplicationBuilder requestPipeline = endpointRouteBuilder.CreateApplicationBuilder();
NameString schemaNameOrDefault = schemaName.HasValue ? schemaName : Schema.DefaultName;
Expand Down Expand Up @@ -328,7 +343,29 @@ public static IEndpointConventionBuilder MapGraphQLSchema(
/// The <see cref="IEndpointConventionBuilder"/>.
/// </param>
/// <param name="toolPath">
/// The path to which banana cake pop is mapped.
/// The path to which Banana Cake Pop is mapped.
/// </param>
/// <param name="relativeRequestPath">
/// The relative path on which the server is listening for GraphQL requests.
/// </param>
/// <returns>
/// Returns the <see cref="IEndpointConventionBuilder"/> so that
/// configuration can be chained.
/// </returns>
public static BananaCakePopEndpointConventionBuilder MapBananaCakePop(
this IEndpointRouteBuilder endpointRouteBuilder,
string toolPath = GraphQLToolPath,
string? relativeRequestPath = GraphQLToolRelativeRequestPath)
=> MapBananaCakePop(endpointRouteBuilder, new PathString(toolPath), relativeRequestPath);

/// <summary>
/// Adds a Banana Cake Pop endpoint to the endpoint configurations.
/// </summary>
/// <param name="endpointRouteBuilder">
/// The <see cref="IEndpointConventionBuilder"/>.
/// </param>
/// <param name="toolPath">
/// The path to which Banana Cake Pop is mapped.
/// </param>
/// <param name="relativeRequestPath">
/// The relative path on which the server is listening for GraphQL requests.
Expand All @@ -339,18 +376,18 @@ public static IEndpointConventionBuilder MapGraphQLSchema(
/// </returns>
public static BananaCakePopEndpointConventionBuilder MapBananaCakePop(
this IEndpointRouteBuilder endpointRouteBuilder,
PathString? toolPath = default,
string? relativeRequestPath = "..")
PathString toolPath,
string? relativeRequestPath = GraphQLToolRelativeRequestPath)
{
if (endpointRouteBuilder is null)
{
throw new ArgumentNullException(nameof(endpointRouteBuilder));
}

toolPath ??= "/graphql/ui";
relativeRequestPath ??= "..";
toolPath = toolPath.ToString().TrimEnd('/');
relativeRequestPath ??= GraphQLToolRelativeRequestPath;

RoutePattern pattern = Parse(toolPath.ToString() + "/{**slug}");
RoutePattern pattern = Parse(toolPath + "/{**slug}");
IApplicationBuilder requestPipeline = endpointRouteBuilder.CreateApplicationBuilder();
IFileProvider fileProvider = CreateFileProvider();

Expand Down