小编Sun*_*rad的帖子

如何访问 ASP.NET Core GraphQL 中嵌套字段中的参数

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

我正在使用 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>()                
            );
Run Code Online (Sandbox Code Playgroud)

子查询看起来像这样

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;

                }
            );
Run Code Online (Sandbox Code Playgroud)

graphql asp.net-core graphql-dotnet

3
推荐指数
1
解决办法
2131
查看次数

标签 统计

asp.net-core ×1

graphql ×1

graphql-dotnet ×1