GraphQL API مع Rust: دليل خطوة بخطوة لإنشاء خدمة ويب

مدرب بايثون أونلاين للمبتدئين

تعلم Python بسهولة دون إرهاق نظري. حل مهام عملية مع التحقق التلقائي واكتب الكود مباشرة في المتصفح.

ابدأ الدورة

GraphQL API مع Rust: دليل خطوة بخطوة



GraphQL هي لغة استعلام لواجهات API تتيح للعملاء الحصول على البيانات المطلوبة فقط. بينما تشتهر Rust بالأداء العالي وأمان الذاكرة. في هذا الدليل، سنقوم بإنشاء واجهة GraphQL API كاملة باستخدام Rust مع مكتبات Juniper و Actix-web. ستتعلم كيفية إعداد الخادم، تعريف المخطط، تنفيذ الاستعلامات والتحويرات، واختبار API.



هذه المادة ستكون مفيدة للمطورين الملمين بأساسيات Rust وتطوير الويب. سنركز على التنفيذ العملي، متجنبين النظريات الزائدة.



إعداد المشروع وتثبيت التبعيات



أولاً، سننشئ مشروع Rust جديد:



cargo new rust-graphql-apicd rust-graphql-api


سنضيف التبعيات اللازمة في ملف Cargo.toml:



[dependencies]actix-web = "4"actix-rt = "2"serde = { version = "1", features = ["derive"] }serde_json = "1"juniper = "0.16"juniper_actix = "2"


هنا actix-web هو إطار عمل ويب، و juniper هي مكتبة للعمل مع GraphQL. juniper_actix توفر التكامل بينهما.



إنشاء مخطط GraphQL أساسي



الآن سنقوم بتعريف نموذج بيانات بسيط ومخطط. على سبيل المثال، سننشئ نظاماً لإدارة المستخدمين والمنشورات.



سننشئ ملف src/schema.rs:



use juniper::{GraphQLObject, GraphQLInputObject, RootNode};

#[derive(GraphQLObject)]#[graphql(description = "مستخدم النظام")]pub struct User { pub id: i32, pub name: String, pub email: String,}

#[derive(GraphQLInputObject)]#[graphql(description = "بيانات الإدخال لإنشاء مستخدم")]pub struct NewUser { pub name: String, pub email: String,}

#[derive(GraphQLObject)]#[graphql(description = "منشور المستخدم")]pub struct Post { pub id: i32, pub title: String, pub body: String, pub author_id: i32,}

pub struct QueryRoot;

#[juniper::object]impl QueryRoot { fn users() -> Vec<User> { vec![ User { id: 1, name: "أليكسي".into(), email: "alex@example.com".into() }, User { id: 2, name: "ماريا".into(), email: "maria@example.com".into() }, ] }

fn posts() -> Vec<Post> { vec![ Post { id: 1, title: "المدخل الأول".into(), body: "نص المنشور".into(), author_id: 1 }, ] }}

pub struct MutationRoot;

#[juniper::object]impl MutationRoot { fn create_user(new_user: NewUser) -> User { User { id: 3, name: new_user.name, email: new_user.email, } }}

pub type Schema = RootNode<'static, QueryRoot, MutationRoot>;


لقد عرّفنا كائنين جذريين: QueryRoot للاستعلامات (جلب البيانات) و MutationRoot للتحويرات (تعديل البيانات). #[derive(GraphQLObject)] يقوم تلقائياً بتوليد أنواع GraphQL من هياكل Rust.



إعداد خادم الويب والتكامل مع Actix-web



الآن سننشئ الملف الرئيسي src/main.rs لتشغيل الخادم:



mod schema;

use actix_web::{web, App, HttpServer, HttpResponse, middleware};use juniper_actix::{graphql_handler, playground_handler};use std::sync::Arc;

use crate::schema::Schema;

async fn graphql_route( req: actix_web::HttpRequest, payload: actix_web::web::Payload, schema: web::Data<Arc<Schema>>,) -> actix_web::Result<HttpResponse> { let context = (); graphql_handler(&schema, &context, req, payload).await}

async fn playground_route() -> actix_web::Result<HttpResponse> { playground_handler("/graphql", None).await}

#[actix_web::main]async fn main() -> std::io::Result<()> { let

المدونات

توصيات الكتب