在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):JuergenGutsch/graphql-aspnetcore开源软件地址(OpenSource Url):https://github.com/JuergenGutsch/graphql-aspnetcore开源编程语言(OpenSource Language):C# 86.0%开源软件介绍(OpenSource Introduction):GraphQl.AspNetCoreThe feedback about my last blog post about the GraphQL end-point in ASP.NET Core was amazing. That post was mentioned on reddit, many times shared on twitter, lInked on http://asp.net and - I'm pretty glad about that - it was mentioned in the ASP.NET Community Standup. Because of that and because GraphQL is really awesome, I decided to make the GraphQL MiddleWare available as a NuGet package. I did some small improvements to make this MiddleWare more configurable and more easy to use in the Branches & contributing & testingThe master branch is the stable branch and I don't axcept PRs to that branch. To contribute, please create PRs based on the develop branch. To play around with the latest changes, please also use the develop branch. Changes on the develop branch ("next version" branch) will be pushed as preview releases to MyGet. To see whether this branch is stable, follow the builds on AppVeyor: Changes on the master branch ("current version" branch) will be pushed as releases to NuGet. To see whether this branch is stable, follow the builds on AppVeyor: Usage and short documentationNuGetPreview builds on MyGet and release builds on NuGet. Install that package via Package Manager Console: PM> Install-Package GraphQl.AspNetCore Install via dotnet CLI: dotnet add package GraphQl.AspNetCore Using the libraryYou still need to configure your GraphQL schema using the graphql-dotnet library, as described in my last post. First configure your schema(s) in the // Configure the default schema
services.AddGraphQl(schema =>
{
schema.SetQueryType<BooksQuery>();
schema.SetMutationType<BooksMutation>();
});
// Also register all graph types
services.AddSingleton<BooksQuery>();
services.AddSingleton<BooksMutation>();
services.AddSingleton<BookType>();
services.AddSingleton<AuthorType>();
services.AddSingleton<PublisherType>();
// ... more types if needed In the You can use different ways to register the GraphQlMiddleware: // the simplest form to use GraphQl. defaults to '/graphql' with default options
app.UseGraphQl();
// or specify options only (default path)
app.UseGraphQl(new GraphQlMiddlewareOptions
{
FormatOutput = true, // default
ComplexityConfiguration = new ComplexityConfiguration()); //default
});
app.UseGraphQl(options =>
{
options.EnableMetrics = true;
});
// or specify path and options
app.UseGraphQl("/graphql", new GraphQlMiddlewareOptions
{
FormatOutput = true, // default
ComplexityConfiguration = new ComplexityConfiguration()); //default
});
// or like this:
app.UseGraphQl("/graph-api", options =>
{
options.SchemaName = "OtherSchema"; // only if additional schemas were registered in ConfigureServices
//options.AuthorizationPolicy = "Authenticated"; // optional
}); Personally I prefer the second way, which is more readable in my opinion. OptionsThe
This should be enough for the first time. If needed it is possible to expose the Newtonsoft.JSON settings, which are used in GraphQL library later on. GraphQL.AspNetCore.GraphiqlThis library provides a middleware to add a GraphiQL UI to your GraphQL endpoint. To learn more about it and the way I created it, read the blog post about it: GraphiQL for ASP.NET Core NuGetPreview builds on MyGet and release builds on NuGet. Install that package via Package Manager Console: PM> Install-Package GraphQl.AspNetCore.Graphiql Install via dotnet CLI: dotnet add package GraphQl.AspNetCore.Graphiql Using the libraryOpen your You can use two different ways to register the GraphiqlMiddleware: app.UseGraphiql("/graphiql", new GraphQlMiddlewareOptions
{
GraphQlEndpoint = "/graphql"
});
app.UseGraphiql("/graphiql", options =>
{
options.GraphQlEndpoint = "/graphql";
}); Personally I prefer the second way, which is more readable in my opinion. The GraphQlEndpoint needs to match the path a GraphQL endpoint. OptionsCurrently the options just have two properties:
One more thingI would be happy, if you try this library and get me some feedback about it. A demo application to quickly start playing around with it, is available here on GitHub. Feel free to raise some issues and to create some PRs to improve this MiddleWare. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论