3

我有一个带有“统计”字段的查询,这是一个具有三个不同累积值的子查询。我希望能够为统计数据提供一个国家/地区参数,然后在解析器中访问子查询中不同字段的该参数。

我正在使用 GraphQL.Server.Transports.AspNetCore nuget 包。子查询使用 IDependencyResolver 解析,因为它对在解析器中用于不同字段的服务有一些依赖关系。

我试图通过 ResolveFieldContext 访问父级,但它似乎不可用。上下文中有一个名为“Source”的属性,但它指的是子查询对象。

如果我们查看 GraphQL 的其他实现似乎应该是可能的,但我不知道如何从 ASP.NET Core 获得结果

下面的代码显示了名为“CompanyGroupQuery”的主查询的一部分

Field<CompanyStatisticsQuery>(
                "statistics",
                arguments: new QueryArguments(new QueryArgument<StringGraphType> { Name = "country" }),
                resolve: context => resolver.Resolve<CompanyStatisticsQuery>()                
            );

子查询看起来像这样

Field<IntGraphType>(
                "completeInvoicesCount",
                resolve: context => {
                    // This is to show what I'm trying to achieve
                    var parent = context.Parent;
                    var companyId = context.GetArgument<string>("companyId");
                    var country = context.GetArgument<string>("country");

                    return null;

                }
            );
4

2 回答 2

5

以下是 SUNMO 的回答示例,并附有一些描述性评论:

// This is an internal object whose properties don't necessarily need to be
// exposed as GraphQL fields. It's purpose is to propagate arguments or some
// partially resolved data which can be further resolved in a child GraphType.
public class Statistics
{
    public string CompanyId { get; set; }
    public string Country { get; set; }
}

// This is the root GraphQL Query class.
public class MyQuery : ObjectGraphType
{
    private const string CompanyIdArgumentName = "companyId";
    private const string CountryArgumentName = "country";

    public MyQuery()
    {
        Field<CompanyStatisticsType>(
            name: "statistics",
            arguments: new QueryArguments(
                new QueryArgument<StringGraphType> { Name = CompanyIdArgumentName },
                new QueryArgument<StringGraphType> { Name = CountryArgumentName }
            ),
            resolve: context => {
                return new Statistics
                {
                    CompanyId = context.GetArgument<string>(CompanyIdArgumentName),
                    Country = context.GetArgument<string>(CountryArgumentName)
                };
            }
            // In the above `resolve` implementation, we're passing the value
            // of the arguments to the a new Statistics object which will be
            // CompanyStatisticsType's context.Source
        );
    }
}

public class CompanyStatisticsType : ObjectGraphType<Statistics>
{
    public CompanyStatisticsType()
    {
        Field<IntGraphType>(
            name: "completeInvoicesCount",
            resolve: context => {
                return GetCompletedInvoicesCount(
                    companyId: context.Source.CompanyId,
                    country: context.Source.Country
                    // Above we are using context.Source, which is an
                    // instance of the `Statistics` class.
                );
            }
        );

        Field(statistics => statistics.Country);
        // Notice how we expose the above `country` field but not `companyId`,
        // further demonstrating that not every property on the `Statistics`
        // class needs to be exposed as a GraphQL field.
    }
}
于 2020-08-26T19:12:56.333 回答
2

如果有人感兴趣,我找到了解决此问题的方法。与其尝试直接访问父级,不如创建一个查询可以继承的类。在这种情况下,我创建了一个包含国家代码属性的统计类。然后将该属性填充到统计字段解析器中,并使用该值返回一个统计类型的新对象。然后,我可以通过查看 context.Source.Country 从子查询访问国家代码,然后我只是不将 country 属性映射到子查询中的字段,因为我对公开不感兴趣它。

于 2019-09-11T10:57:35.780 回答