rust-best-practices

โดย apollographql

แนวทางการเขียนโค้ด Rust ตามหลัก Idiomatic โดยอ้างอิงจากคู่มือแนวปฏิบัติที่ดีที่สุดของ Apollo GraphQL ครอบคลุมเก้าหลักสำคัญ: รูปแบบและสำนวนการเขียนโค้ด, การใช้ Clippy ตรวจสอบโค้ด, การปรับปรุงประสิทธิภาพ, การจัดการข้อผิดพลาด, รูปแบบการทดสอบ, Generics และ Dispatch, รูปแบบ Type State, การจัดทำเอกสาร, และความปลอดภัยของพอยน์เตอร์ เน้นการใช้ Borrowing แทน Cloning, การจัดการข้อผิดพลาดแบบ Result ร่วมกับ thiserror/anyhow, และการวัดประสิทธิภาพด้วย Release Builds รวมถึงคำแนะนำด่วนเกี่ยวกับรูปแบบ Ownership, การหลีกเลี่ยง Panic,...

npx skills add https://github.com/apollographql/skills --skill rust-best-practices

Rust Best Practices

Apply these guidelines when writing or reviewing Rust code. Based on Apollo GraphQL's Rust Best Practices Handbook.

Best Practices Reference

Before reviewing, familiarize yourself with Apollo's Rust best practices. Read ALL relevant chapters in the same turn in parallel. Reference these files when providing feedback:

Quick Reference

Borrowing & Ownership

  • Prefer &T over .clone() unless ownership transfer is required
  • Use &str over String, &[T] over Vec<T> in function parameters
  • Small Copy types (≤24 bytes) can be passed by value
  • Use Cow<'_, T> when ownership is ambiguous

Error Handling

  • Return Result<T, E> for fallible operations; avoid panic! in production
  • Never use unwrap()/expect() outside tests
  • Use thiserror for library errors, anyhow for binaries only
  • Prefer ? operator over match chains for error propagation

Performance

  • Always benchmark with --release flag
  • Run cargo clippy -- -D clippy::perf for performance hints
  • Avoid cloning in loops; use .iter() instead of .into_iter() for Copy types
  • Prefer iterators over manual loops; avoid intermediate .collect() calls

Linting

Run regularly: cargo clippy --all-targets --all-features --locked -- -D warnings

Key lints to watch:

  • redundant_clone - unnecessary cloning
  • large_enum_variant - oversized variants (consider boxing)
  • needless_collect - premature collection

Use #[expect(clippy::lint)] over #[allow(...)] with justification comment.

Testing

  • Name tests descriptively: process_should_return_error_when_input_empty()
  • One assertion per test when possible
  • Use doc tests (///) for public API examples
  • Consider cargo insta for snapshot testing generated output

Generics & Dispatch

  • Prefer generics (static dispatch) for performance-critical code
  • Use dyn Trait only when heterogeneous collections are needed
  • Box at API boundaries, not internally

Type State Pattern

Encode valid states in the type system to catch invalid operations at compile time:

struct Connection<State> { /* ... */ _state: PhantomData<State> }
struct Disconnected;
struct Connected;

impl Connection<Connected> {
    fn send(&self, data: &[u8]) { /* only connected can send */ }
}

Documentation

  • // comments explain why (safety, workarounds, design rationale)
  • /// doc comments explain what and how for public APIs
  • Every TODO needs a linked issue: // TODO(#42): ...
  • Enable #![deny(missing_docs)] for libraries

Skills เพิ่มเติมจาก apollographql

apollo-client
apollographql
Apollo Client เป็นไลบรารีจัดการสถานะที่ครอบคลุมสำหรับ JavaScript ที่ช่วยให้คุณจัดการข้อมูลทั้งในเครื่องและระยะไกลด้วย GraphQL เวอร์ชัน 4.x นำเสนอการแคชที่ปรับปรุงดีขึ้น การรองรับ TypeScript ที่ดีขึ้น และความเข้ากันได้กับ React 19
official
apollo-client
apollographql
คู่มือที่ครอบคลุมสำหรับการสร้างแอปพลิเคชัน React ด้วย Apollo Client 4.x ครอบคลุมการสอบถาม การกลายพันธุ์ การแคช และการจัดการสถานะ รองรับเฟรมเวิร์ก React และการตั้งค่าหลายแบบ: แอปฝั่งไคลเอ็นต์ (Vite, CRA), Next.js App Router พร้อม React Server Components, React Router 7 พร้อมสตรีมมิ่ง SSR และ TanStack Start รวมถึง hooks สำหรับการสอบถาม (useQuery, useLazyQuery), การกลายพันธุ์ (useMutation) และรูปแบบที่ใช้ Suspense (useSuspenseQuery, useBackgroundQuery) สำหรับ React 18+ และ 19 สมัยใหม่...
official
apollo-connectors
apollographql
รวม REST API เข้ากับ GraphQL supergraphs โดยใช้ @source และ @connect directives มีกระบวนการ 5 ขั้นตอนที่มีโครงสร้าง: ศึกษาโครงสร้าง API, ใช้ schema กับ directives, ตรวจสอบผ่าน rover supergraph compose, ดำเนินการ connectors, และทดสอบครอบคลุม รองรับการกำหนดค่าคำขอรวมถึง headers, body payloads, การทำ batch สำหรับรูปแบบ N+1, และการแทรกตัวแปรสภาพแวดล้อมผ่าน $env จัดการการแมปการตอบสนองด้วยการเลือกฟิลด์, การตั้งชื่ออื่น, การเลือกย่อยสำหรับข้อมูลที่ซ้อนกัน, และ entity...
official
apollo-federation
apollographql
Apollo Federation ช่วยให้สามารถรวม GraphQL API หลายตัว (ซับกราฟ) เข้าด้วยกันเป็นซูเปอร์กราฟแบบรวมศูนย์
official
apollo-ios
apollographql
Apollo iOS เป็น GraphQL ไคลเอนต์แบบ strongly-typed สำหรับแพลตฟอร์ม Apple โดยสร้างประเภท Swift จากการดำเนินการและสคีมา GraphQL ของคุณ และมาพร้อมกับไคลเอนต์แบบ async/await, แคชแบบ normalized (ในหน่วยความจำหรือ backed โดย SQLite), การขนส่ง HTTP แบบ interceptor-based ที่เสียบได้ซึ่งจัดการ queries, mutations, และ multipart subscriptions, และการขนส่ง WebSocket แบบเลือกได้ (graphql-transport-ws) ที่สามารถรองรับการดำเนินการทุกประเภท
official
apollo-kotlin
apollographql
Apollo Kotlin เป็นไคลเอนต์ GraphQL ที่มีการกำหนดชนิดข้อมูลอย่างเข้มงวด ซึ่งสร้างโมเดล Kotlin จากการดำเนินการและสคีมา GraphQL ของคุณ สามารถใช้ในโปรเจกต์ Android, JVM และ Kotlin Multiplatform ได้
official
apollo-mcp-server
apollographql
เชื่อมต่อ AI agents กับ GraphQL APIs ผ่าน Model Context Protocol พร้อมเครื่องมือ introspection และ operation ในตัว เปิดเผย GraphQL operations เป็น MCP tools; รองรับแหล่ง operation สามประเภท: ไฟล์ในเครื่อง, คอลเลกชัน GraphOS Studio, และ persisted query manifests มีเครื่องมือ introspection สี่อย่าง (introspect, search, validate, execute) สำหรับการสำรวจ schema และทดสอบ query แบบ ad-hoc; โหมด minification ลดการใช้ token ด้วยสัญกรณ์แบบกะทัดรัด กำหนดค่าการรับรองความถูกต้องผ่าน static headers,...
official
apollo-router
apollographql
Apollo Router เป็นกราฟเราเตอร์ประสิทธิภาพสูงที่เขียนด้วยภาษา Rust สำหรับรันซูเปอร์กราฟของ Apollo Federation 2 โดยจะอยู่ด้านหน้าซับกราฟของคุณและจัดการการวางแผนคิวรี การดำเนินการ และการประกอบคำตอบ
official