Introduction
In the previous article of the GraphQL series, we discussed the introduction of GraphQL and Pros & Cons of using GraphQL. If you haven’t read my previous articles, then I highly encourage you to do so.
I believe you have a prior understanding of EF Core and we'll be concentrating only on GraphQL concepts. If not, please refer to the below article.
Coding
Let's directly jump into coding section by adding NuGet package
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.Tools
- GraphQL
- GraphQL.Server.Transports.AspNetCore
- GraphQL.Server.UI.Playground
In this article, we are illustrating a model that is concerned with books and their authors.
- public class Author
- {
- public int AuthorId { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public ICollection<Book> Books { get; set; }
- }
- public class Book
- {
- public int BookId { get; set; }
- public string Title { get; set; }
- public int PublicationYear { get; set; }
- public int AuthorId { get; set; }
- public Author Author { get; set; }
- }
- public class AuthorContext:DbContext
- {
- public DbSet<Author> Authors { get; set; }
- public DbSet<Book> Books { get; set; }
- public AuthorContext(DbContextOptions dbContextOptions) :base(dbContextOptions)
- {
- }
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- modelBuilder.Entity<Author>().HasData(new Author
- {
- FirstName= "Agatha",
- LastName="Christry",
- AuthorId = 1
- });
- modelBuilder.Entity<Book>().HasData(
- new Book { Title = "The Mysterious Affair at Styles", PublicationYear = 1921,AuthorId=1,BookId=1 },
- new Book { Title = "The Secret Adversary", PublicationYear = 1922,AuthorId = 1,BookId=2 }
- );
- }
- }
I highly encourage you to use IEntityTypeConfiguration for data seeding rather than using OnModelCreating method. You can refer to the article here.
- public interface IAuthorRepository
- {
- Task<IEnumerable<Author>> GetAuthorsAsync();
- Task<Author> GetAuthorAsync(int authorId);
- Task<Author> InsertAuthorAsync(Author author);
- Task<Author> GetAuthorByFirstNameAsync(string firstName);
- }
- public class AuthorRepository: IAuthorRepository
- {
- private readonly AuthorContext authorContext;
- public AuthorRepository(AuthorContext authorContext)
- {
- this.authorContext = authorContext;
- }
- public async Task<Author> GetAuthorAsync(int authorId)
- {
- return await authorContext.Authors.FirstOrDefaultAsync(x => x.AuthorId == authorId);
- }
- public async Task<Author> GetAuthorByFirstNameAsync(string firstName)
- {
- return await authorContext.Authors.FirstOrDefaultAsync(x => x.FirstName == firstName);
- }
- public async Task<IEnumerable<Author>> GetAuthorsAsync()
- {
- return await authorContext.Authors.ToListAsync();
- }
- public async Task<Author> InsertAuthorAsync(Author author)
- {
- var result = (await authorContext.Authors.AddAsync(author)).Entity;
- await authorContext.SaveChangesAsync();
- return result;
- }
- }
- public interface IBookRepository
- {
- Task<IEnumerable<Book>> GetBooksAsync();
- Task<IEnumerable< Book>> GetBooks(int authorId);
- Task<Book> InsertBook(Book book);
- }
- public class BookRepository: IBookRepository
- {
- private readonly AuthorContext authorContext;
- public BookRepository(AuthorContext authorContext)
- {
- this.authorContext = authorContext;
- }
- public async Task<IEnumerable< Book>> GetBooks(int authorId)
- {
- var result = await (from author in authorContext.Set<Author>()
- join book in authorContext.Set<Book>() on author.AuthorId equals book.AuthorId
- where author.AuthorId==authorId
- select book).ToListAsync();
- return result;
- }
- public async Task<IEnumerable<Book>> GetBooksAsync()
- {
- return await authorContext.Books.ToListAsync();
- }
- public async Task<Book> InsertBook(Book book)
- {
- var result = (await authorContext.Books.AddAsync(book)).Entity;
- await authorContext.SaveChangesAsync();
- return result;
- }
- }
- services.AddDbContext<AuthorContext>(x => x.UseSqlServer(Configuration.GetConnectionString("SqlConnectionString")));
- services.AddScoped<IAuthorRepository, AuthorRepository>();
- services.AddScoped<IBookRepository, BookRepository>();
- //Adding migration
- dotnet ef migrations add InitialMigration
- //Update Database
- dotnet ef database update
GraphQL Middleware


Join the conversation! Your thoughts help the community grow.