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-federation
apollographql
Apollo Federation ช่วยให้สามารถรวม GraphQL API หลายตัว (ซับกราฟ) เข้าด้วยกันเป็นซูเปอร์กราฟแบบรวมศูนย์
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) ที่สามารถรองรับการดำเนินการทุกประเภท
apollo-router
apollographql
Apollo Router เป็นกราฟเราเตอร์ประสิทธิภาพสูงที่เขียนด้วยภาษา Rust สำหรับรันซูเปอร์กราฟของ Apollo Federation 2 โดยจะอยู่ด้านหน้าซับกราฟของคุณและจัดการการวางแผนคิวรี การดำเนินการ และการประกอบคำตอบ
apollo-router-plugin-creator
apollographql
สร้างปลั๊กอิน Rust ดั้งเดิมสำหรับ Apollo Router
apollo-server
apollographql
คู่มือฉบับสมบูรณ์สำหรับการสร้างเซิร์ฟเวอร์ GraphQL ด้วย Apollo Server 5.x ในทุกเฟรมเวิร์ก ครอบคลุมการกำหนดสคีมา ตัวแก้ไข การตั้งค่าคอนเทกซ์ และการจัดการข้อผิดพลาดพร้อมรองรับ TypeScript รองรับโหมดสแตนด์อโลนสำหรับการสร้างต้นแบบ และการผสานรวมกับ Express, Fastify, Koa และสภาพแวดล้อมแบบไร้เซิร์ฟเวอร์ รวมถึงรูปแบบตัวแก้ไข การรับรองความถูกต้อง/การอนุญาต ปลั๊กอิน DataLoader สำหรับป้องกันปัญหา N+1 และเทคนิคการปรับปรุงประสิทธิภาพ ให้เอกสารอ้างอิงสำหรับแหล่งข้อมูล ข้อผิดพลาด...
graphql-operations
apollographql
คู่มือแนวทางปฏิบัติที่ดีที่สุดสำหรับการเขียน GraphQL operations ที่มีประสิทธิภาพและปลอดภัยต่อชนิดข้อมูล พร้อมการจัดระเบียบด้วย fragments ครอบคลุม queries, mutations, subscriptions และ fragments พร้อมหลักการตั้งชื่อ ไวยากรณ์ตัวแปร และการใช้ directives เน้นหลักการสำคัญ: ขอเฉพาะฟิลด์ที่จำเป็น ตั้งชื่อ operations ทั้งหมด ใช้ตัวแปรแทนค่าคงที่ และรวมฟิลด์ id เพื่อให้แคชได้ แนะนำให้วาง fragments ไว้ร่วมกับ components และใช้ directives @include / @skip สำหรับฟิลด์แบบมีเงื่อนไข...
graphql-schema
apollographql
คู่มือแนวปฏิบัติที่ดีที่สุดในอุตสาหกรรมสำหรับการออกแบบ GraphQL schemas ที่ใช้งานง่าย มีประสิทธิภาพสูง และบำรุงรักษาได้ ครอบคลุมหลักการออกแบบหลัก เช่น การจัดระเบียบประเภทที่เน้นผู้ใช้เป็นศูนย์กลาง รูปแบบการกำหนดค่า nullability ที่ชัดเจน และกลยุทธ์การพัฒนาที่เข้ากันได้ย้อนหลัง มีเอกสารอ้างอิงเกี่ยวกับประเภท หลักการตั้งชื่อ การแบ่งหน้าแบบ cursor-based การสร้างแบบจำลองข้อผิดพลาด และข้อควรพิจารณาด้านความปลอดภัย รวมถึงรูปแบบที่ใช้งานได้จริงสำหรับ interfaces, unions, input types, mutations และกลยุทธ์ ID พร้อมตัวอย่างโค้ด...
rover
apollographql
Apollo Rover CLI สำหรับจัดการ GraphQL schemas, federation และการพัฒนา supergraph ในเครื่อง เผยแพร่ ดึงข้อมูล และตรวจสอบความถูกต้องของ subgraph schemas; ประกอบ supergraph แบบ federated ในเครื่องหรือผ่าน GraphOS รวมถึงการตรวจสอบ schema (การตรวจสอบก่อน deploy), การ linting และการ introspection จากเซิร์ฟเวอร์ที่กำลังทำงาน คำสั่ง rover dev เริ่ม Router ในเครื่องพร้อมการประกอบ schema อัตโนมัติสำหรับขั้นตอนการพัฒนา รองรับรูปแบบ CI/CD ด้วยการตรวจสอบก่อนเผยแพร่และเอาต์พุต JSON สำหรับการเขียนสคริปต์ ต้องมี...