名称“Foo”已被另一种类型 GraphQL 注册

Oce*_*lot 9 c# mediatr graphql

我使用 GraphQL 作为 CQRS 应用程序的一部分。我已将命令和查询域模型分成两个不同的项目(查询和命令)。每个项目都有一些模型,例如学生模型。Domain.Query.Students.Student我在和中使用两个不同的模型Domain.Command.Students.Student,但两者具有相同的实体名称(学生和地址)我简化了我的实体,如下所示:

public class Student:Entity
{
    public  string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
    public DateTimeOffset CreatedOn { get; set; }
    public DateTimeOffset ModifiedOn { get; set; }
 }
Run Code Online (Sandbox Code Playgroud)

Student 用于查询和命令项目。我已经Startup.cs像这样配置了 GraphQL:

 services
 .AddGraphQLServer()
 .AddQueryType<StudentQuery>() 
 .AddMutationType<AddStudentMutation>();
Run Code Online (Sandbox Code Playgroud)

StudentQuery.cs

namespace GraphQL.Query.Students
{
    public class StudentQuery
    {
        
         
         public List<Student> AllStudents([ScopedService] StudentRepository studentRepository)
         {
             var query = studentRepository.GetEmployees();
             return query;
         }
    }
 }
Run Code Online (Sandbox Code Playgroud)

AddStudentMutation.cs

namespace GraphQL.Command.Students
{
    public class AddStudentMutation
    {
        public async Task<Student> AddStudentAsync(AddStudentInputType student,[Service] IMediator mediator)
        {
            var result =await mediator.Send(new AddStudentCommand(student.FirstName,student.LastName,student.StreetAddress,
                student.City,student.State,student.ZipCode));
            return result;
        }
    }
    
    [GraphQLName("AddStudentInput")]
    public class AddStudentInputType
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string StreetAddress { get;  set; }
        public string City { get;  set; }
        public string State { get;  set; }
        public string ZipCode { get;  set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行该应用程序时,出现以下错误:

The name `Student` was already registered by another type. (HotChocolate.Types.ObjectType<Domain.Query.Students.Student>)
Run Code Online (Sandbox Code Playgroud)

如果我将 Student 实体更改Domain.Query.Students.Student为 ReadStudent 或其他实体,它将运行没有任何问题,但我不想更改它。

小智 1

您可以将 GraphQL 注释添加到Student类中,如下所示 [GraphQLName("StudentQuery")]