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 MapGraphiQL extension method for endpoints #1695

Merged
merged 1 commit into from
Dec 7, 2023
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
1 change: 0 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<Project>
<PropertyGroup>
<LangVersion>11</LangVersion>
<Copyright>Pekka Heikura</Copyright>
<PackageProjectUrl>https://github.com/pekkah/tanka-graphql</PackageProjectUrl>
<RepositoryUrl>https://github.com/pekkah/tanka-graphql</RepositoryUrl>
Expand Down
3 changes: 2 additions & 1 deletion samples/GraphQL.Samples.Http/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@
app.UseWebSockets();

// this uses the default pipeline
// you can access GraphiQL at "/graphql/ui"
app.MapTankaGraphQL("/graphql", "System");
// you can access GraphiQL at "/graphql/ui"
app.MapGraphiQL("/graphql/ui");

app.Run();

Expand Down
41 changes: 21 additions & 20 deletions src/GraphQL.Language/GraphQL.Language.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<Compile Remove="Shims/**" />
<Compile Remove="System.Diagnostics.CodeAnalysis/**" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<Compile Remove="Shims/**" />
<Compile Remove="System.Diagnostics.CodeAnalysis/**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
</Project>
18 changes: 10 additions & 8 deletions src/GraphQL.Server/GraphQLApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ public GraphQLApplication(
_transports = transports;
}


public IEndpointConventionBuilder MapDefault(string pattern, string schemaName, IEndpointRouteBuilder routes)
{
var builders = new List<IEndpointConventionBuilder>();
foreach (IGraphQLTransport transport in _transports)
builders.Add(MapTransport(pattern, routes, transport, b => ConfigureDefaultPipeline(b, schemaName)));

if (_optionsMonitor.CurrentValue.EnableUi)
builders.Add(routes.MapGet($"{pattern}/ui", CreateUiDelegate(pattern)));

return new RouteHandlerBuilder(builders);
}

Expand All @@ -38,12 +34,18 @@ public IEndpointConventionBuilder Map(string pattern, IEndpointRouteBuilder rout
foreach (IGraphQLTransport transport in _transports)
builders.Add(MapTransport(pattern, routes, transport, configureRequest));

if (_optionsMonitor.CurrentValue.EnableUi)
builders.Add(routes.MapGet($"{pattern}/ui", CreateUiDelegate(pattern)));

return new RouteHandlerBuilder(builders);
}

public IEndpointConventionBuilder MapUi(string pattern, IEndpointRouteBuilder routes)
{
var builders = new List<IEndpointConventionBuilder>
{
routes.MapGet($"{pattern}", CreateUiDelegate(pattern))
};

return new RouteHandlerBuilder(builders);
}

private void ConfigureDefaultPipeline(GraphQLRequestPipelineBuilder builder, string schemaName)
{
Expand All @@ -56,7 +58,7 @@ private RequestDelegate CreateUiDelegate(string apiPattern)
Stream? htmlStream = typeof(GraphQLApplication)
.Assembly.GetManifestResourceStream("Tanka.GraphQL.Server.GraphiQL.host.html");

var reader = new StreamReader(htmlStream);
var reader = new StreamReader(htmlStream ?? throw new InvalidOperationException());
string htmlTemplate = reader.ReadToEnd();


Expand Down
2 changes: 0 additions & 2 deletions src/GraphQL.Server/GraphQLApplicationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
public class GraphQLApplicationOptions
{
public List<string> SchemaNames { get; } = new();

public bool EnableUi { get; set; } = true;
}
9 changes: 1 addition & 8 deletions src/GraphQL.Server/GraphQLRequestContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;

namespace Tanka.GraphQL.Server;
Expand All @@ -17,12 +16,6 @@ public GraphQLRequestContext(): this(new FeatureCollection(2))
{
}

public IServiceProvider RequestServices
{
get => ServiceProvidersFeature.RequestServices;
set => ServiceProvidersFeature.RequestServices = value;
}

public HttpContext HttpContext
{
get => HttpContextFeature.HttpContext;
Expand Down
76 changes: 41 additions & 35 deletions src/GraphQL.Server/GraphiQL/host.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!--
* Copyright (c) 2021 GraphQL Contributors
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
* Copyright (c) 2021 GraphQL Contributors
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
-->
<!DOCTYPE html>
<html lang="en">
Expand All @@ -22,43 +22,49 @@
}
</style>

<script
crossorigin
src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script
crossorigin
src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<!--
This GraphiQL example depends on Promise and fetch, which are available in
modern browsers, but can be "polyfilled" for older browsers.
GraphiQL itself depends on React DOM.
If you do not want to rely on a CDN, you can host these files locally or
include them directly in your favored resource bundler.
These two files can be found in the npm module, however you may wish to
copy them directly into your environment, or perhaps include them in your
favored resource bundler.
-->
<script src="https://unpkg.com/react@17/umd/react.development.js"
integrity="sha512-Vf2xGDzpqUOEIKO+X2rgTLWPY+65++WPwCHkX2nFMu9IcstumPsf/uKKRd5prX3wOu8Q0GBylRpsDB26R6ExOg=="
crossorigin="anonymous"></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"
integrity="sha512-Wr9OKCTtq1anK0hq5bY3X/AvDI5EflDSAh0mE9gma+4hl+kXdTJPKZ3TwLMBcrgUeoY0s3dq9JjhCQc7vddtFg=="
crossorigin="anonymous"></script>
<script
src="https://unpkg.com/graphiql/graphiql.min.js"
type="application/javascript"></script>
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css"/>
<!--
These are imports for the GraphIQL Explorer plugin.
-->
<script
src="https://unpkg.com/@graphiql/plugin-explorer/dist/index.umd.js"
crossorigin></script>

<!--
These two files can be found in the npm module, however you may wish to
copy them directly into your environment, or perhaps include them in your
favored resource bundler.
-->
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" />
<link
rel="stylesheet"
href="https://unpkg.com/@graphiql/plugin-explorer/dist/style.css"/>
</head>

<body>
<div id="graphiql">Loading...</div>
<script src="https://unpkg.com/graphiql/graphiql.min.js"
type="application/javascript"></script>
<div id="graphiql">Loading...</div>
<script>
ReactDOM.render(
React.createElement(GraphiQL, {
fetcher: GraphiQL.createFetcher({
url: '{{httpUrl}}',
subscriptionUrl: '{{httpUrl}}/ws'.replace('http', "ws")
}),
defaultEditorToolsVisibility: true,
}),
document.getElementById('graphiql'),
);
const root = ReactDOM.createRoot(document.getElementById('graphiql'));
const fetcher = GraphiQL.createFetcher({
url: '{{httpUrl}}',
subscriptionUrl: '{{httpUrl}}/ws'.replace('http', "ws")
});
const explorerPlugin = GraphiQLPluginExplorer.explorerPlugin();
root.render(
React.createElement(GraphiQL, {
fetcher,
defaultEditorToolsVisibility: true,
plugins: [explorerPlugin],
}),
);
</script>
</body>
</html>
29 changes: 0 additions & 29 deletions src/GraphQL.Server/Utils/CancellationTokenExtensions.cs

This file was deleted.

9 changes: 9 additions & 0 deletions src/GraphQL.Server/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ public static IEndpointConventionBuilder MapTankaGraphQL(
var tankaApp = webApp.ServiceProvider.GetRequiredService<GraphQLApplication>();
return tankaApp.Map(pattern, webApp, configurePipeline);
}

public static IEndpointConventionBuilder MapGraphiQL(
this IEndpointRouteBuilder webApp,
string pattern)
{
// resolve tanka application
var tankaApp = webApp.ServiceProvider.GetRequiredService<GraphQLApplication>();
return tankaApp.MapUi(pattern, webApp);
}
}
3 changes: 2 additions & 1 deletion src/GraphQL.Server/WebSockets/GraphQLWSConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private async Task HandleMessage(MessageBase message, CancellationToken cancella
ConnectionInit => TooManyInitializationRequests(),
Subscribe subscribe => HandleSubscribe(subscribe, cancellationToken),
Ping ping => HandlePing(ping, cancellationToken),
Complete complete => HandleComplete(complete, cancellationToken)
Complete complete => HandleComplete(complete, cancellationToken),
_ => throw new ArgumentOutOfRangeException(nameof(message), message, null)
};

await task;
Expand Down
Loading
Loading