【问题标题】:How to access arguments in nested fields in ASP.NET Core GraphQL如何在 ASP.NET Core GraphQL 中访问嵌套字段中的参数
【发布时间】:2020-08-27 04:54:52
【问题描述】:

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

我正在使用 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;

                }
            );

【问题讨论】:

    标签: asp.net-core graphql graphql-dotnet


    【解决方案1】:

    以下是 SUNMO 的回答示例,其中包含一些描述性的 cmets:

    // 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.
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这很有帮助,但一些演示代码会更清晰。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 2018-03-13
      • 1970-01-01
      相关资源
      最近更新 更多