如何首先组织GraphQL.Net GraphType中的多个突变?
在GraphQL.Net中,大多数示例代码都有一个顶级突变图对象,其中定义了许多实际突变。如何首先组织GraphQL.Net GraphType中的多个突变?,graphql,graphql-dotnet,graphql.net,Graphql,Graphql Dotnet,Graphql.net,在GraphQL.Net中,大多数示例代码都有一个顶级突变图对象,其中定义了许多实际突变。 下面是GraphQL.NET中的一个示例: public类StarWarsSchema:Schema { 公共StarWarsSchema(IServiceProvider提供商) :base(提供程序) { Query=provider.Resolve(); 突变=provider.Resolve(); } } 公共类StarWarsMutation:ObjectGraphType { 公共StarWa
下面是GraphQL.NET中的一个示例:
public类StarWarsSchema:Schema
{
公共StarWarsSchema(IServiceProvider提供商)
:base(提供程序)
{
Query=provider.Resolve();
突变=provider.Resolve();
}
}
公共类StarWarsMutation:ObjectGraphType
{
公共StarWarsStation(StarWarsData数据)
{
场(
“创造人类”,
参数:新的QueryArguments(
新QueryArgument{Name=“human”}
),
解析:上下文=>
{
var human=context.GetArgument(“human”);
返回数据。AddHuman(human);
});
}
}
当你有1-5个突变时,这看起来很好,但是在一些更大的项目中,随着时间的推移,你可能会有几十个突变。把他们放在一个大班里似乎就足够了,尽管似乎也缺少一些组织。我尝试将子突变GraphTypeObject放入父突变的字段中,但调用子突变时遇到了一点困难。也许我把它配置错了
这让我想知道,肯定有一个用户拥有十几个突变,他们可能组织了他们的突变,而不仅仅是将所有突变放在一个顶级突变对象中
如何首先组织GraphQL.Net GraphType中的多个突变?
您可以通过添加顶级字段将查询或突变“分组”在一起。“技巧”是在解析器中返回一个空对象
public class StarWarsSchema : Schema
{
public StarWarsSchema(IServiceProvider provider)
: base(provider)
{
Query = provider.Resolve<StarWarsQuery>();
Mutation = provider.Resolve<StarWarsMutation>();
}
}
public class StarWarsMutation : ObjectGraphType
{
public StarWarsMutation(StarWarsData data)
{
Field<CharacterMutation>(
"characters",
resolve: context => new { });
}
}
public class CharacterMutation : ObjectGraphType
{
public CharacterMutation(StarWarsData data)
{
Field<HumanType>(
"createHuman",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<HumanInputType>> {Name = "human"}
),
resolve: context =>
{
var human = context.GetArgument<Human>("human");
return data.AddHuman(human);
});
}
}
public类StarWarsSchema:Schema
{
公共StarWarsSchema(IServiceProvider提供商)
:base(提供程序)
{
Query=provider.Resolve();
突变=provider.Resolve();
}
}
公共类StarWarsMutation:ObjectGraphType
{
公共StarWarsStation(StarWarsData数据)
{
场(
“字符”,
解析:context=>new{});
}
}
公共类字符突变:ObjectGraphType
{
公共字符突变(StarWarsData数据)
{
场(
“创造人类”,
参数:新的QueryArguments(
新QueryArgument{Name=“human”}
),
解析:上下文=>
{
var human=context.GetArgument(“human”);
返回数据。AddHuman(human);
});
}
}
这种组织反映在如何调用突变(或查询)中。如果您只是希望它们在外部显示为一个平面列表(相当于一个巨大的文件),您还可以使用分部类将其分解为任意多个文件。谢谢,这正是我要找的。
public class StarWarsSchema : Schema
{
public StarWarsSchema(IServiceProvider provider)
: base(provider)
{
Query = provider.Resolve<StarWarsQuery>();
Mutation = provider.Resolve<StarWarsMutation>();
}
}
public class StarWarsMutation : ObjectGraphType
{
public StarWarsMutation(StarWarsData data)
{
Field<CharacterMutation>(
"characters",
resolve: context => new { });
}
}
public class CharacterMutation : ObjectGraphType
{
public CharacterMutation(StarWarsData data)
{
Field<HumanType>(
"createHuman",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<HumanInputType>> {Name = "human"}
),
resolve: context =>
{
var human = context.GetArgument<Human>("human");
return data.AddHuman(human);
});
}
}