GraphQL .NET - 架构加载时出现 AutoRegisteringObjectGraphType 错误:无法注册 GraphType

Har*_*ryM 4 .net c# graphql

我有一个 CustomerDetails 类:

public record CustomerDetails{
      public string? Code { get; init; }
      public string? Name { get; init; }
      public string? Notes { get; init; }
}
Run Code Online (Sandbox Code Playgroud)

在 GraphQL 方面,我有以下内容:

在查询方面:

public class CustomerDetailsGraphType : AutoRegisteringObjectGraphType<CustomerDetails> { }
Run Code Online (Sandbox Code Playgroud)

在突变方面:

public class CustomerDetailsInputGraphType : AutoRegisteringInputObjectGraphType<CustomerDetails> {}
Run Code Online (Sandbox Code Playgroud)

当我运行此命令并尝试在 Altair 中查看架构时,它给出了以下类型的错误:

 "message": "GraphQL.Execution.UnhandledError: Error executing document.\r\n ---> System.InvalidOperationException: Unable to register GraphType 'MyProject.Customers.Mutations.ContactDetailsInputGraphType' with the name 'ContactDetails';\nthe name 'ContactDetails' is already registered to 'MyProject.Customers.Query.ContactDetailsGraphType'.\r\n..."
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会这样?

Har*_*ryM 5

我找到了解决方案。在这两种情况下,图形类型对象的名称将自动为 CustomerDetails。这不适合 GraphQL 架构,无法生成。因此,无论是在查询中还是在突变中,您都必须在构造函数中显式指定不同的名称,如下所示:

public class CustomerDetailsGraphType : AutoRegisteringObjectGraphType<CustomerDetails> 
{
       public CustomerDetailsGraphType()
       {
             Name = "CustomerDetailsGraphType"; // or any other name you consider
       }

}
Run Code Online (Sandbox Code Playgroud)