InputObjectGraphType的字段解析器

时间:2018-12-11 02:56:43

标签: asp.net graphql graphql-dotnet

我正在使用ASP.NET Web API,并使用GraphQL for .NET 添加了对GraphQL请求的支持。 我的查询按预期工作,但是我现在正努力为突变使用相同的逻辑。

我的查询逻辑如下:

Field<ContactType>("contact", "This field returns the contact by Id",
            arguments: new QueryArguments(QA_ContactId),
            resolve: ctx => ContactResolvers.ContactDetails(ctx));

我的解析器返回一个 ContactDomainEntity ,然后将其解析为 ContactType

public class ContactType : ObjectGraphType<ContactDomainEntity>
{
    public ContactType()
    {
        Name = "Contact";
        Description = "Contact Type";

        Field(c => c.Id);
        Field(c => c.FirstName, nullable: true);

        Field<ListGraphType<AddressType>, IEnumerable<AddressDTO>>(Field_Addresses)
            .Description("Contact's addresses")
            .Resolve(ctx => LocationResolvers.ResolveAddresses(ctx));
    }
}

这一切都非常好,并且地址列表由其自己的reslver(LocationResolvers.ResolveAddresses)解析,这使其可重用并有助于分离问题。

现在,我希望能够编辑联系人,并希望使用相同的逻辑,其中子对象(如地址列表)将由其自己的解析程序处理。因此,我创建了以下突变:

Field<ContactType>("UpdateContact", "This field updates the Contact's details",
                arguments: new QueryArguments(QA_Input<Types.Input.ContactInputType>()),
                resolve: ctx => ContactResolvers.UpdateContact(ctx));

具有 ContactInputType

public class ContactInputType : InputObjectGraphType<ContactInputDTO>
{
    public ContactInputType()
    {
        Name = "UpdateContactInput";
        Description = "Update an existing contact";

        Field(c => c.Id);
        Field(c => c.FirstName, nullable: true);

        Field<ListGraphType<AddressInputType>, IEnumerable<AddressDTO>>("Addresses")
             .Description("Manage contact's addresses")
             .Resolve(ctx => LocationResolvers.ManageAddresses(ctx));
    }
}

(请注意,我使用DTO将字段映射到对我而言有意义的对象,但与我的问题无关

我的问题是只有解析器“ ContactResolvers.UpdateContact”被调用。字段解析器“ LocationResolvers.ManageAddresses”从未命中。如果我将地址字段替换为以下内容:

Field(c => c.Addresses, nullable: true, type: typeof(ListGraphType<AddressInputType>));

我的ContactInputDTO已正确填充(即其属性“地址”包含正确的数据),但是这意味着我无法控制对象属性的映射方式,因此必须依赖具有相同名称的对象,并且无法添加我的解析器可能具有的其他逻辑。


tl; dr 如何在InputObjectGraphType中使用字段解析器?返回ObjectGraphType时它可以正常工作,但是我无法在接收端使用它。

0 个答案:

没有答案
相关问题