GraphQL nedir?
GraphQL REST API yazmak yerine kullanılabilecek bir sorgu dilidir. GraphQL hiçbir veritabanı türüne bağlı değildir. Yani POSTGRESQL veya mongoDB ile kullanılabilir. GraphQL beş ana bölümde incelenebilir.
Örnekler express, graphql, sequelize, postgresql teknolojilerinin kombininden oluşturulmuştur.
Types
Veritabanında bulunan tabloların ve alanların tanımlamaları olarak söylenebilir.
type App {
name: String!
}
Gerçek uygulamada kullanımı.
// AppType.jsimport { GraphQLInt, GraphQLObjectType, GraphQLString } from 'graphql';
const AppType = new GraphQLObjectType({
name: 'app',
description: '...',
fields: () => ({
id: { type: GraphQLInt },
name: { type: GraphQLString },
slug: { type: GraphQLString },
template: { type: GraphQLInt },
createdAt: { type: GraphQLString },
updatedAt: { type: GraphQLString },
deletedAt: { type: GraphQLString },
UserId: { type: GraphQLInt },
}),
});
export default AppType;
Queries
Query içerisine yazılabilecek sorgulama işlemlerini tanımlıyoruz. buraya yazdığımız tanımlamalar dışında herhangi birşey çalıştıramayız.
type Query {
getApp(id: ID!): App
}
Gerçek uygulamada kullanımı.
// Query.jsimport { GraphQLInt, GraphQLList } from 'graphql';
import AppType from '../type';
import { getById, getByUserId } from '../resolvers';
export const getAppById = {
type: AppType,
args: {
id: { type: GraphQLInt },
},
resolve: getById,
};
export const getAppsByUserId = {
type: new GraphQLList(AppType),
args: {
userId: { type: GraphQLInt },
},
resolve: getByUserId,
};
Mutations
Veri oluşturma, silme ve güncelleme işlemleri için bu alanı kullanıyoruz.
type Mutation {
deleteApp(id: ID!): App
}
Gerçek uygulamada kullanımı.
// Mutations.jsimport { GraphQLInt } from 'graphql';
import AppType from '../type';
import { create, remove } from '../resolvers';
export const appCreate = {
type: AppType,
args: {
template: {
name: 'template',
type: GraphQLInt,
},
},
resolve: create,
};
export const appRemove = {
type: AppType,
args: {
id: {
name: 'id',
type: GraphQLInt,
},
},
resolve: remove,
};
Subscriptions
Subscriptionlar server tarafında belirli bir event gerçekletiğinde WebSoket kullanarak belirtilen datayı gerçek zamanlı olarak client’a gönderir. Aşağıdaki subscription tanımlaması App oluşturulduğunda, güncellendiğinde ve silindiğinde çalışıyor olacak.
type Subscription {
app(where: AppSubscriptionWhereInput): AppSubscriptionPayload
}type AppSubscriptionPayload {
mutation: MutationType!
node: App
updatedFields: [String!]
previousValues: AppPreviousValues
}type AppPreviousValues {
id: ID!
description: String!
url: String!
}
Resolvers
Üstte tanımladığımız query ve mutation’ların sequelize taranfındaki karşılıklarını resolver kısmına yazıyoruz ve Yazdığımız query ve mutationları resolver fonksiyonlar ile birbirine bağlıyoruz. Yani GraphQL çalıştırıldığında arkada buradaki kodlar çalıştırılmış oluyor.
// Resolvers.jsimport models from '../../models/index';
export async function getByUserId(parentValue, { userId }) {
return models.App.findAll({ where: { UserId: userId } });
}
export async function getById(parentValue, { id }) {
return models.App.findOne({ where: { id } });
}
export async function create(parentValue, { template }, context) {
return models.App.create({ template, UserId: context.userId });
}
export async function remove(parentValue, { id }) {
return models.App.destroy({ where: { id } });
}
GraphQL Kullanımı ilerleyen zamanlarda daha detaylı olarak yazacağım.