如何在ASP.NET Core GraphQL中访问嵌套字段中的参数 [英] How to access arguments in nested fields in ASP.NET Core GraphQL

查看:65
本文介绍了如何在ASP.NET Core GraphQL中访问嵌套字段中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,该查询的字段为"statistics",这是一个具有三个不同累积值的子查询.我希望能够提供一个国家(地区)参数的统计信息,然后在解析器中为子查询中的不同字段访问该参数.

I have a query with the field "statistics" which is a sub-query with three different accumulated values. I would like to be able to give statistics a country argument and then access that argument in the resolvers for the different fields in the sub-query.

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

I'm using the GraphQL.Server.Transports.AspNetCore nuget package. The sub query is resolved using the IDependencyResolver since it has some dependencies to services that are used in the resolver for the different field.

我尝试通过ResolveFieldContext访问父级,但是它似乎不可用.上下文中有一个名为源"的属性,但它引用的是子查询对象.

I've tried to access the parent through the ResolveFieldContext but it doesn't seem to be available. There is a property on the context called "Source" but that is referring to the sub query object.

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

It seems that it should be possible if we look at other implementations of GraphQL but I have no clue how the get the result from ASP.NET Core

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

The code below shows a part of the main Query called "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;

                }
            );

推荐答案

我发现了针对此问题的解决方案,以防万一有人感兴趣.无需尝试直接访问父级,而是创建一个可以从中继承查询的类.在这种情况下,我创建了一个统计类,其中包含国家/地区代码的属性.然后,将属性填充到统计信息字段解析器中,并使用该值返回统计信息"类型的新对象.然后,我可以通过查看context.Source.Country来从子查询中访问国家/地区代码,然后我就不会将country属性映射到子查询中的字段,因为我不希望公开它.

I found a solution for this problem in case anyone is interested. Instead of trying to access the parent directly create a class which the query can inherit from. In this case I created a statistics class which contained a property for the country code. The property is then populated in the statistics field resolver and a new object of type Statistics is returned with that value. I'm then able to access the country code from the sub-query by looking at context.Source.Country and then I simply don't map the country property to a field in the sub-query since I'm not interested in exposing it.

这篇关于如何在ASP.NET Core GraphQL中访问嵌套字段中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆