The name `Foo` was already registered by another type, GraphQL

Viewed 1447

I am using GraphQL as a part of my CQRS application. I have separated Command and Query domain model into two different projects (Query and Command). each of there projects has some models for instance Student model. I use two different model in Domain.Query.Students.Student and Domain.Command.Students.Student, but both have the same entity name (Student and Address) I simplified my entity as below:

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; }
 }

Student is being used in Query and Command projects. in Startup.cs I have configured GraphQL like this:

 services
 .AddGraphQLServer()
 .AddQueryType<StudentQuery>() 
 .AddMutationType<AddStudentMutation>();

In StudentQuery.cs

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

and 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; }
    }
}

When I run the application I get the following error:

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

If I change the Student entity in Domain.Query.Students.Student to ReadStudent or something else, it will run without any issue, but I don't want to change it.

1 Answers

You can add the GraphQL annotation onto the Student class as [GraphQLName("StudentQuery")]

Related