In the previous articles of this series, we have configured GraphQL with an Employee table. Now, consider a scenario where you want to maintain multiple certifications for each employee. In this situation, we need to create one more table which is a kind of child table for Employees.
You can read the previous parts here.
- GraphQL In .NET Core Web API With Entity Framework Core - Part One
- GraphQL In .NET Core Web API With Entity Framework Core - Part Two
Execute the below script to create the Certification table with some dummy data.
CREATE TABLE [dbo].[Certification](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[EmployeeId] [bigint] NULL,
[Title] [varchar](100) NULL,
[Month] [int] NULL,
[Year] [int] NULL,
[Provider] [varchar](100) NULL,
CONSTRAINT [PK_Certification] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Certification] ON
GO
INSERT [dbo].[Certification] ([Id], [EmployeeId], [Title], [Month], [Year], [Provider]) VALUES (1, 1, N'MCSD', 1, 2019, N'Microsoft')
GO
INSERT [dbo].[Certification] ([Id], [EmployeeId], [Title], [Month], [Year], [Provider]) VALUES (2, 1, N'Scrum Master', 2, 2019, N'Agile')
GO
INSERT [dbo].[Certification] ([Id], [EmployeeId], [Title], [Month], [Year], [Provider]) VALUES (3, 2, N'MCT', 12, 2018, N'Microsoft')
GO
INSERT [dbo].[Certification] ([Id], [EmployeeId], [Title], [Month], [Year], [Provider]) VALUES (4, 2, N'PMP', 1, 2019, N'PMP')
GO
SET IDENTITY_INSERT [dbo].[Certification] OFF
GO
Execute the Scaffold command in the Package Manager Console to update the model and dbContext with the Certification table.
Scaffold-DbContext "Server=AKSHAY-PC\DEVSQL2016;Database=GraphQLDemo;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force
Post execution, you will be able to see that the Certification model is created and DbSet is added for the same in dbContext.
namespace GraphQLInWebApiCore
{
public partial class Certification
{
public long Id { get; set; }
public long EmployeeId { get; set; }
public string Title { get; set; }
public int? Month { get; set; }
public int? Year { get; set; }
public string Provider { get; set; }
}
}
public virtual DbSet<Certification> Certification { get; set; }
In the previous example, we created an EmployeeType. Similarly, we need to create an EmployeeCertificationType, which inherits ObjectGraphType.
EmployeeCertificationType.cs
using GraphQL.Types;
namespace GraphQLInWebApiCore
{
public class EmployeeCertificationType : ObjectGraphType<Certification>
{
public EmployeeCertificationType()
{
Field(t => t.Id);
Field(t => t.Title);
Field(t => t.Month, nullable: true);
Field(t => t.Year, nullable: true);
Field(t => t.Provider);
}
}
}
Create a repository to fetch certificates by the employee. For that, we need to add ICertificationRepository and CertificationRepository.
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GraphQLInWebApiCore
{
public interface ICertificaationRepository
{
Task<List<Certification>> GetCertificationByEmployee(long EmployeeId);
}
}



Visar ElmaziPosted Mar 3, 2019, 9:03 AM
Getting those Nulls at the response is like a big no no for GraphQL.