When building my GraphQL schema with HotChocolate, I typically use the code first or annotation first approach.
Annotation-based example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Query | |
{ | |
public Book GetBook(string isbn, [Service] IBookRepository bookRepository) | |
{ | |
return bookRepository.GetByISBN(isbn); | |
} | |
} |
Code-first example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Query | |
{ | |
public Book GetBook(string isbn, [Service] IBookRepository bookRepository) | |
{ | |
return bookRepository.GetByISBN(isbn); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class QueryType : ObjectType<Query> | |
{ | |
protected override void Configure(IObjectTypeDescriptor<Query> descriptor) | |
{ | |
descriptor | |
.Field(f => f.GetBook(default!, default!)) | |
.Type<BookType>(); | |
} | |
} |
In both cases I start from a class where I add multiple methods where each method maps to a different query. But what if I use method overloading and introduce a second implementation of the method using different arguments?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Query | |
{ | |
public Book GetBook(string isbn, [Service] IBookRepository bookRepository) | |
{ | |
return bookRepository.GetByISBN(isbn); | |
} | |
public Book GetBook(Author author, [Service] IBookRepository bookRepository) | |
{ | |
return bookRepository.GetByAuthor(author); | |
} | |
} |
HotChocolate will not complain and you will be able to succesfully run your GraphQL endpoint. However when you look at the generated GraphQL schema only one of the methods is available:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Query { | |
book(isbn: String): Book | |
} |
To make this work we need to tell HotChocolate to use a different name for our overloaded method:
Annotation-based example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Query | |
{ | |
public Book GetBook(string isbn, [Service] IBookRepository bookRepository) | |
{ | |
return bookRepository.GetByISBN(isbn); | |
} | |
[GraphQLName("getbookbyauthor")] | |
public Book GetBook(Author author, [Service] IBookRepository bookRepository) | |
{ | |
return bookRepository.GetByAuthor(author); | |
} | |
} |
Code-first example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Query | |
{ | |
public Book GetBook(string isbn, [Service] IBookRepository bookRepository) | |
{ | |
return bookRepository.GetByISBN(isbn); | |
} | |
public Book GetBook(Author author, [Service] IBookRepository bookRepository) | |
{ | |
return bookRepository.GetByAuthor(author); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class QueryType : ObjectType<Query> | |
{ | |
protected override void Configure(IObjectTypeDescriptor<Query> descriptor) | |
{ | |
descriptor | |
.Field(f => f.GetBook(default!, default!)) | |
.Name("getbookbyauthor") | |
.Type<BookType>(); | |
} | |
} |
That should do the trick.