您好,登錄后才能下訂單哦!
要在C#中使用GraphQL Playground,您需要首先設(shè)置一個(gè)ASP.NET Core項(xiàng)目,并安裝必要的NuGet包
創(chuàng)建一個(gè)新的ASP.NET Core Web應(yīng)用程序項(xiàng)目。
通過(guò)NuGet添加以下包:
在項(xiàng)目的Startup.cs
文件中,配置GraphQL和GraphQL Playground。這是一個(gè)示例配置:
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using GraphQL;
using GraphQL.Types;
namespace GraphQLDemo
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
services.AddSingleton<ISchema, MySchema>(); // 創(chuàng)建并注冊(cè)您自己的GraphQL Schema
services.AddGraphQL()
.AddSystemTextJson();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGraphQL();
});
app.UseGraphQLPlayground(new GraphQL.Server.Ui.Playground.GraphQLPlaygroundOptions
{
Path = "/ui/playground"
});
}
}
}
MySchema.cs
:using GraphQL.Types;
namespace GraphQLDemo
{
public class MySchema : Schema
{
public MySchema()
{
Query = new MyQuery();
}
}
public class MyQuery : ObjectGraphType
{
public MyQuery()
{
Field<StringGraphType>("hello", resolve: context => "Hello, world!");
}
}
}
http://localhost:<port>/ui/playground
,其中<port>
是您的應(yīng)用程序正在運(yùn)行的端口號(hào)。現(xiàn)在,您已經(jīng)將GraphQL Playground集成到了C# ASP.NET Core項(xiàng)目中。您可以使用Playground查詢(xún)您的GraphQL API。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。