Asp.Net Core Api İle Graphql Projesi Oluşturmak
Bu yazı dizimiz de Asp.Net Core Api teması kullanarak graphql ile sql server veritabanina nasıl bağlantı kurabileceğimizi göreceğiz.
Kaynak Kodlara aşağıdaki repodan ulaşabilirsiniz.

.Net Core Web Api ve GraphQL kullanarak SQL Server veritabanında select işlemlerinin nasıl yapılacağını tecrübe edineceğiz.
Aşağıdaki terimler hakkında bilgi sahibi olmalısınız.
- 1..Net Core Web Api Nedir?
- 2..Net Core Web Api Projesi Nasıl Oluşturulur?
- 3.GraphQL Nedir?
- 4.ObjectGraphType Nedir?
- 5.Dapper Nedir?
- 6.SqlKata Nedir?
Orm tool olarak Dapper altyapısı kullanılarak hızlı bir şekilde sql kodu yazabileceğimiz SqlKata Query Generater kütüphanesini kullanacağız.

Web Projemize aşağıdaki kütüphaneleri ekliyoruz.
<PackageReference Include="GraphQL" Version="2.4.0" />
Tablo setlerimizi yöneteceğimiz Entity katmanını oluşturuyoruz.

Referans Projesi olarak aşağıdaki projeyi ekliyoruz.
<ProjectReference Include="..\CK.Tutorial.GraphQlApi.Common\CK.Tutorial.GraphQlApi.Common.csproj" />
Tüm katmanlarda kullanacağımız enum, const ve helper sınıfları için Common katmanını oluşturuyoruz.

Projemize Newtonsoft.Json kütüphanesini ekliyoruz.
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
İş süreçlerini kontrol edebileceğimiz Business katmanını oluşturuyoruz.

Verilerimizi transfer edeceğimiz Model katmanını oluşturuyoruz.

Referans Projesi olarak aşağıdaki projeyi ekliyoruz.
<ProjectReference Include="..\CK.Tutorial.GraphQlApi.Common\CK.Tutorial.GraphQlApi.Common.csproj" />
Veritabanı işlemlerini yönetebildiğimiz data katmanımız olan Repository katmanını oluşturuyoruz.

Repository katmanına aşağıdaki referansları ekleyin.
<PackageReference Include="System.Data.SqlClient" Version="4.7.0" />
<PackageReference Include="SqlKata" Version="1.1.7" />
<PackageReference Include="SqlKata.Execution" Version="1.1.7" />
Referans projesini olarak aşağıdaki kütüphaneleri ekliyoruz.
CK.Tutorial.GraphQlApi.Repository.csproj
<ProjectReference Include="..\CK.Tutorial.GraphQlApi.Common\CK.Tutorial.GraphQlApi.Common.csproj" />
<ProjectReference Include="..\CK.Tutorial.GraphQlApi.Entity\CK.Tutorial.GraphQlApi.Entity.csproj" />
<ProjectReference Include="..\CK.Tutorial.GraphQlApi.Model\CK.Tutorial.GraphQlApi.Model.csproj" />
Array değişkenlerimize yardımcı olması için aşağıdaki extension'ı yazıyoruz.
using System.Linq;
namespace CK.Tutorial.GraphQlApi.Common.Extension
{
public static class ArrayExtension
{
public static bool AnyItem(this string[] value)
{
return value != null && value.Any();
}
}
}
Primitive değişkenlerimize yardımcı olmak için aşağıdaki extension'ı yazıyoruz.
using System;
namespace CK.Tutorial.GraphQlApi.Common.Extension
{
public static class PrimitiveExtension
{
public static bool IsNotNullOrEmpty(this string value)
{
return !string.IsNullOrEmpty(value);
}
public static bool IsNotNull(this bool? value)
{
return value != null;
}
public static bool IsTrue(this bool? value)
{
return value != null && value == true;
}
public static byte ConvertToByte(this bool? value)
{
if (value ==null )
{
throw new ArgumentException("Boolean Item must be convertible");
}
return (byte) (value.Value ? 1 : 0);
}
}
}
Kullanıcılardan parametrik olarak alacağımız veriler için base request sınıfı oluşturuyoruz.
namespace CK.Tutorial.GraphQlApi.Common.Model
{
public class RequestBase
{
public int DefaultPage { get; } = 1;
public int DefaultPageSize { get; } = 10;
public string[] Columns { get; set; }
public int? PageSize { get; set; }
public int? Page { get; set; }
}
}
GraphQL için aşağıdaki model dosyamızı oluşturuyoruz.
using System.Text;
namespace CK.Tutorial.GraphQlApi.Common.Model
{
public class GraphQlQuery
{
public string OperationName { get; set; }
public string NamedQuery { get; set; }
public string Query { get; set; }
public string Variables { get; set; }
public override string ToString()
{
var builder = new StringBuilder();
builder.AppendLine();
if (!string.IsNullOrWhiteSpace(OperationName)) builder.AppendLine($"OperationName = {OperationName}");
if (!string.IsNullOrWhiteSpace(NamedQuery)) builder.AppendLine($"NamedQuery = {NamedQuery}");
if (!string.IsNullOrWhiteSpace(Query)) builder.AppendLine($"Query = {Query}");
if (!string.IsNullOrWhiteSpace(Variables)) builder.AppendLine($"Variables = {Variables}");
return builder.ToString();
}
}
}
namespace CK.Tutorial.GraphQlApi.Entity
{
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
}
using CK.Tutorial.GraphQlApi.Common.Enum;
namespace CK.Tutorial.GraphQlApi.Entity
{
public class Product
{
public int Id { get; set; }
public int CompanyId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int ProductTypeId { get; set; }
public Status Status { get; set; }
}
}
Kullanıcıdan aldığımız verileri alt katmanlara aşağıdaki veri deseniyle göndereceğiz.
using CK.Tutorial.GraphQlApi.Common.Model;
namespace CK.Tutorial.GraphQlApi.Model.Request
{
public class SearchCompany : RequestBase
{
public int? Id { get; set; }
public string Name { get; set; }
public bool? IsActive { get; set; }
}
}
Repository sınıflarımız için generic base bir Interface tanımlıyoruz.
IRepository.cs
using SqlKata.Execution;
namespace CK.Tutorial.GraphQlApi.Repository
{
public interface IRepositoryBase<TEntity>
{
QueryFactory QueryFactory { get; }
}
}
Oluşturacağımız Entity Repository'leri için RepositoryBase sınıfını, IRepositoryBase interface'i üzerinden oluşturuyoruz.
RepositoryBase.cs
using SqlKata.Execution;
namespace CK.Tutorial.GraphQlApi.Repository
{
public class RepositoryBase<TEntity> : IRepositoryBase<TEntity>
{
public RepositoryBase(QueryFactory queryFactory)
{
QueryFactory = queryFactory;
}
public QueryFactory QueryFactory { get; set; }
}
}
ICompanyRepository.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using CK.Tutorial.GraphQlApi.Entity;
using CK.Tutorial.GraphQlApi.Model.Request;
using SqlKata.Execution;
namespace CK.Tutorial.GraphQlApi.Repository
{
public interface ICompanyRepository
{
Task<IEnumerable<Company>> GetCompanies(SearchCompany request);
Task<Company> GetCompany(SearchCompany request);
}
}
CompanyRepository.cs
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CK.Tutorial.GraphQlApi.Common.Extension;
using CK.Tutorial.GraphQlApi.Entity;
using CK.Tutorial.GraphQlApi.Model.Request;
using SqlKata.Execution;
namespace CK.Tutorial.GraphQlApi.Repository
{
public class CompanyRepository : RepositoryBase<Company>, ICompanyRepository
{
private readonly QueryFactory _queryFactory;
private const string TableName = "Company";
public CompanyRepository(QueryFactory queryFactory) : base(queryFactory)
{
_queryFactory = queryFactory;
}
public async Task<IEnumerable<Company>> GetCompanies(SearchCompany request)
{
var query = _queryFactory
.Query(TableName)
.When(request.Columns.AnyItem(),
q => q.Select(request.Columns))
.When(request.IsActive.IsNotNull(), q => q.Where("IsActive", request.IsActive.ConvertToByte()))
.OrderBy("Id")
.ForPage(request.Page ?? request.DefaultPage, request.PageSize ?? request.DefaultPageSize)
.GetAsync<Company>();
return await query.ConfigureAwait(false);
}
public async Task<Company> GetCompany(SearchCompany request)
{
var query = _queryFactory
.Query(TableName)
.When(request.Columns.AnyItem(),
q => q.Select(request.Columns))
.When(request.Id.HasValue, q => q.Where("Id", request.Id))
.When(request.Name.IsNotNullOrEmpty(), q => q.WhereContains("Name", request.Name))
.When(request.IsActive.IsNotNull(), q => q.Where("IsActive", request.IsActive.ConvertToByte()))
.OrderBy("Id")
.FirstOrDefaultAsync<Company>();
return await query.ConfigureAwait(false);
}
}
}
Company Entity'sinin iş süreçlerini yazmak için ICompanyService adında bir interface oluşturuyoruz.
Şimdilik iki tane metod yazacağız.
ICompanyService.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using CK.Tutorial.GraphQlApi.Entity;
using CK.Tutorial.GraphQlApi.Model.Request;
using SqlKata.Execution;
namespace CK.Tutorial.GraphQlApi.Business
{
public interface ICompanyService
{
Task<IEnumerable<Company>> GetCompanies(SearchCompany request);
Task<Company> GetCompany(SearchCompany request);
}
}
ICompanyService üzerinden CompanyService adında yeni bir sınıf oluşturuyoruz.
CompanyService.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using CK.Tutorial.GraphQlApi.Entity;
using CK.Tutorial.GraphQlApi.Model.Request;
using CK.Tutorial.GraphQlApi.Repository;
namespace CK.Tutorial.GraphQlApi.Business
{
public class CompanyService : ICompanyService
{
private readonly ICompanyRepository _companyRepository;
public CompanyService(ICompanyRepository companyRepository)
{
_companyRepository = companyRepository;
}
public Task<IEnumerable<Company>> GetCompanies(SearchCompany request)
{
return _companyRepository.GetCompanies(request);
}
public Task<Company> GetCompany(SearchCompany request)
{
return _companyRepository.GetCompany(request);
}
}
}
GraphQL Schema adını verdiğimiz referans bir root sınıfından oluşur.
Schema sınıfına Resolver bir tane QUERY referans gösterilir..
Query dosyaları ise GraphType'lardan beslenerek dinamik sorgular oluşturacağımız bir yapı kurmamızı sağlar.
Kısacası Schema > Query > GraphTypes olarak tanımlayabiliriz.
Retail adında bir tane Root Schema sınıfımızı aşağıdaki gibi tanımlıyoruz.
RetailSchema sınıfının TutorialQuery adında bir sorgulama sınıfını çözümlediğini bildiriyoruz.
RetailSchema.cs
using CK.Tutorial.GraphQlApi.Web.Query;
using GraphQL;
namespace CK.Tutorial.GraphQlApi.Web.Schema
{
public class RetailSchema : GraphQL.Types.Schema
{
public RetailSchema(IDependencyResolver resolver) : base(resolver)
{
Query = resolver.Resolve<TutorialQuery>();
}
}
}
Entity sınıflarımızın field alanlarını kullanarak graphql için sorgulanabilir tipler oluşturuyoruz.
Aşağıdaki örneğimizde Company Entity dosyasımızı referans göstermiş ve field alanları için Type tanımlamalarını yapmış olduğumuzu göreceksiniz.
Örneğin Name alanı için ikinci parametre olan IsNullable alanını True vererek, bu field için null alabilen bir field düzenlemiş olduk. Diğer override parametrelerini kullanarak TYPE tipinide değiştirmeniz veya belirtmeniz mümkündür.
CompanyGraphType.cs
using System;
using CK.Tutorial.GraphQlApi.Business;
using CK.Tutorial.GraphQlApi.Entity;
using GraphQL.Types;
namespace CK.Tutorial.GraphQlApi.Web.Types
{
public class CompanyGraphType : ObjectGraphType<Company>
{
public CompanyGraphType()
{
Name = "Company";
Field(x => x.Id).Description("Şirket Benzersiz No");
Field(x => x.Name, true).Description("Şirket Adı");
Field(x => x.IsActive, true).Description("Şirket Aktif mi?");
}
}
}
Bu örneğimizde 2 tane sorgulama endpoint'i oluşturacağız.
Birinci endpoint; "company" adını verdiğimiz, CompanyGraphType kullanarak sorgu yapabileceğimiz,
- companyId
- companyName
- isActive
parametrelerinden oluşan ve return type değeri COMPANY entity sınıfımızdır.
İkinci endpoint; "companies" adını verdiğimiz ve return tipi List<Company> olan sınıfımızdır.
TutorialQuery.cs
using System.Collections.Generic;
using CK.Tutorial.GraphQlApi.Business;
using CK.Tutorial.GraphQlApi.Model.Request;
using CK.Tutorial.GraphQlApi.Web.Extensions;
using CK.Tutorial.GraphQlApi.Web.Types;
using GraphQL.Types;
namespace CK.Tutorial.GraphQlApi.Web.Query
{
public class TutorialQuery : ObjectGraphType
{
public TutorialQuery(ICompanyService companyService)
{
#region Company
Field<CompanyGraphType>(
name: "company",
arguments: new QueryArguments(new List<QueryArgument>
{
new QueryArgument<IntGraphType>
{
Name = "companyId"
},
new QueryArgument<StringGraphType>
{
Name = "companyName"
},
new QueryArgument<BooleanGraphType>
{
Name = "isActive"
}
}),
resolve: delegate (ResolveFieldContext<object> context)
{
var columns = context.GetMainSelectedFields();
var companyId = context.GetArgument<int?>("companyId");
var companyName = context.GetArgument<string>("companyName");
var isActive = context.GetArgument<bool?>("isActive");
var request = new SearchCompany
{
Id = companyId,
Name = companyName,
IsActive = isActive,
Columns = columns
};
return companyService.GetCompany(request);
});
Field<ListGraphType<CompanyGraphType>>(
"companies",
arguments: new QueryArguments(new List<QueryArgument>
{
new QueryArgument<ListGraphType<IntGraphType>>
{
Name = "companyIds"
},
new QueryArgument<ListGraphType<StringGraphType>>
{
Name = "companyNames"
},
new QueryArgument<BooleanGraphType>
{
Name = "isActive"
},
new QueryArgument<IntGraphType>
{
Name = "page"
},
new QueryArgument<IntGraphType>
{
Name = "pageSize"
}
}),
resolve: delegate (ResolveFieldContext<object> context)
{
var columns = context.GetMainSelectedFields();
var companyIds = context.GetArgument<int?[]>("companyIds");
var companyNames = context.GetArgument<string[]>("companyNames");
var isActive = context.GetArgument<bool?>("isActive");
var page = context.GetArgument<int?>("page");
var pageSize = context.GetArgument<int?>("pageSize");
var request = new SearchCompanies()
{
Ids = companyIds,
Names = companyNames,
IsActive = isActive,
Page = page,
PageSize = pageSize,
Columns = columns
};
return companyService.GetCompanies(request);
});
#endregion
}
}
}
Öncelikle yeni bir SQL SERVER veritabanı açmalısınız.
Sonrasında database klasörü içerisinde yer alan create-table.sql dosyasını çalıştırmalısınız.
create-table.sql
-- auto-generated definition
create table Company
(
Id int identity
constraint Company_pk
primary key nonclustered,
Name nvarchar(200) not null,
IsActive bit default 1 not null
)
go
create unique index Company_Id_uindex
on Company (Id)
go
Sizlerin projeyi test edebilmesi için; database klasörünün içerisinde yer alan bulk-data.sql dosyasını oluşturdum, bu dosyayı çalıştırabilirsiniz.
Projenin AppSettings dosyasını kendi connectionString değerlerinize göre güncellemesiniz.
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"SqlServerDatabaseConnection": "Server=localhost;Database=GraphQLSample;User Id=sa;Password=Aa123456;"
}
}
Aşağıdaki komutları çalıştırıp projeyi ayağa kaldırabilirsiniz.
dotnet restore
dotnet build
dotnet run
Tarayıcınızdan https://localhost:5001/ui/playground adresine gitmek istediğinizde; sorgularınızı test edebileceğiniz bir arayüze erişebilmeniz gerekmektedir.

Aşağıdaki sorguyu yazdığınızda çıktısını almalısınız.
query {
company(companyId:5) {
id
name
isActive
}
}
