简体   繁体   English

有没有办法通过引用另一种类型来解决 GraphQl 类型?

[英]Is there a way to resolve a GraphQl type by referencing another type?

Im using the Graphql.Net library to build a GraphQl API.我使用 Graphql.Net 库来构建 GraphQl API。 The following is a domain example of what we currently have, where, the area has a list of sampling point identifiers:以下是我们目前拥有的域示例,其中,该区域有一个采样点标识符列表:

public class AreaRoot {
   public String Id { get; set; }
   public List<String > SamplingPointIds { get; set; }
}

public class SamplingPointRoot {
   public String Id { get; set; }
   public String Description { get; set; }
}

And the types are defined as follow:类型定义如下:

public class AreaType : ObjectGraphType<AreaRoot>
{
    public AreaType()
    {
        Name = "Area";
        Field(x => x.Id, type: typeof(IdGraphType));
        Field(x => x.SamplingPointIds, type: typeof(ListGraphType<StringGraphType>));
    }
}

public class SamplingPointType : ObjectGraphType<SamplingPointRoot>
{
    public SamplingPointType()
    {
        Name = "SamplingPoint";
        Field(x => x.Id, type: typeof(IdGraphType));
        Field(x => x.description, type: typeof(StringGraphType));
    }
}

Is there any way to retrieving everything from the sampling point without changing the domain classes?有什么方法可以在不更改域类的情况下从采样点检索所有内容? there is an example in the conference GraphQL vs Traditional Rest API , in the 25:41 min, but this example is in java, and we could not make the same using the graphQl .net. there is an example in the conference GraphQL vs Traditional Rest API , in the 25:41 min, but this example is in java, and we could not make the same using the graphQl .net.

The next example illustrates the type of query we want to make:下一个示例说明了我们想要进行的查询类型:

query GetAreas(){
    areas(){
        Id
        samplingPoints{
           Id
           description
        }
    }
}

So the question is: Is there a way to this as in the video above, as we pass the samplingPoints, and resolve it, retrieving the samplingPoints for that area (in some query)?所以问题是:有没有办法像上面的视频那样,当我们传递采样点并解决它,检索该区域的采样点(在某些查询中)?

Question resolved on github .github上解决了问题。 For those trying to do the same, its really easy actually, we just have to had resolver inside the AreaType like this:对于那些试图做同样的事情的人来说,实际上真的很容易,我们只需要像这样在 AreaType 中有解析器:

public class AreaType : ObjectGraphType<AreaRoot>
{
    public AreaType(IRepository repository)
    {
        Name = "Area";
        Field(x => x.Id, type: typeof(IdGraphType));
        Field<ListGraphType<SamplingPointType>>("areaSamplingPoints",
           resolve: context =>
        {
           return repository.GetAllByAreaId(context.Source?.Id);
        });
    }
}

notice the context.Source?.Id used to access the Area Id...注意context.Source?.Id用于访问区域 ID...

And also, if you are trying to access the arguments of the top level context, well, you can't, as stated here , but instead, you can access the variables passed to the query, not the best, but not the worst, so use: context.Variables.ValueFor("variableName")而且,如果您尝试访问顶级上下文的 arguments,那么您不能,如此处所述,但是您可以访问传递给查询的变量,不是最好,但不是最差的,所以使用: context.Variables.ValueFor("variableName")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法解析 Graphql HotChocolate 中的字符类型 - Could not resolve Char Type in Graphql HotChocolate C#-引用其他引用类型的引用类型 - c# - reference type referencing another reference type 如何在 ef core 6 中配置引用另一个实体的拥有类型 - How to configure Owned type referencing another entity in ef core 6 Unity C#:引用来自另一个脚本的浮点数的类型错误 - Unity C#: type error referencing float from another script 我如何在不引用容器的情况下从System.Type解析 - How can I resolve from System.Type without referencing the container 在C#中引用多个Web服务时解决类型歧义 - Resolve Type Ambiguity When Referencing More Than One Webservice in C# 引用另一个程序集中的类型导致“在未引用的程序集中定义类型”Razor.Compile()中的错误 - Referencing type that is in another assembly causes “the type is defined in an assembly that is not referenced” error in Razor.Compile() 在另一个IHostedService中注入IHostedService时出现“无法解析服务类型”错误 - “Unable to resolve service for type” error when injecting IHostedService in another IHostedService 解决类型是否可以支持接口的最佳方法? (鸭打字) - Best way to resolve if a Type could support an interface? (Duck-Typing) 在不知道类型的情况下引用泛型类型的实例 - Referencing instances of Generic Type without knowing the Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM