rust-best-practices

作成者: apollographql

Apollo GraphQLのベストプラクティスハンドブックに基づく慣用的なRustコーディング標準。9つの主要領域をカバー:コーディングスタイルとイディオム、clippyリンティング、パフォーマンス最適化、エラーハンドリング、テストパターン、ジェネリクスとディスパッチ、タイプステートパターン、ドキュメンテーション、ポインタ安全性。クローンよりも借用を重視し、thiserror/anyhowを用いたResultベースのエラーハンドリング、リリースビルドでのパフォーマンスプロファイリングを強調。所有権パターン、パニック回避に関するクイックリファレンスガイダンスを含む...

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

apollographqlのその他のスキル

apollo-federation
apollographql
Apollo Federationは、複数のGraphQL API(サブグラフ)を統合されたスーパーグラフに構成することを可能にします。
apollo-ios
apollographql
Apollo iOSは、Appleプラットフォーム向けの強く型付けされたGraphQLクライアントです。GraphQLの操作とスキーマからSwiftの型を生成し、async/awaitクライアント、正規化キャッシュ(インメモリまたはSQLite対応)、クエリ、ミューテーション、マルチパートサブスクリプションを処理するプラグ可能なインターセプターベースのHTTPトランスポート、および任意の操作タイプを伝送可能なオプションのWebSocketトランスポート(graphql-transport-ws)を搭載しています。
apollo-router
apollographql
Apollo Routerは、Apollo Federation 2のスーパーグラフを実行するためにRustで書かれた高性能なグラフルーターです。サブグラフの前に配置され、クエリの計画、実行、レスポンスの合成を処理します。
apollo-router-plugin-creator
apollographql
Apollo Router用のネイティブRustプラグインを作成します。
apollo-server
apollographql
Apollo Server 5.xを使用してフレームワーク間でGraphQLサーバーを構築するための完全ガイド。スキーマ定義、リゾルバ、コンテキスト設定、TypeScriptサポートによるエラーハンドリングを網羅。プロトタイピング用のスタンドアロンモードと、Express、Fastify、Koa、サーバーレス環境との統合をサポート。リゾルバパターン、認証/認可、プラグイン、N+1問題防止のためのDataLoader、パフォーマンス最適化手法を含む。データソース、エラー...のリファレンスドキュメントを提供。
graphql-operations
apollographql
効率的で型安全なGraphQLオペレーションを作成し、フラグメントで整理するためのベストプラクティスガイド。クエリ、ミューテーション、サブスクリプション、フラグメントについて、命名規則、変数構文、ディレクティブの使用法を網羅。必要なフィールドのみをリクエストする、すべてのオペレーションに名前を付ける、ハードコードされた値の代わりに変数を使用する、キャッシュ可能性のためにIDフィールドを含めるといった基本原則を強調。フラグメントをコンポーネントと併置し、条件付きフィールドには@include/@skipディレクティブを使用することを推奨...
graphql-schema
apollographql
直感的で高性能かつ保守可能なGraphQLスキーマを設計するための業界ベストプラクティスガイド。クライアント中心の型構成、明示的なnull許容パターン、後方互換性のある進化戦略を含む中核設計原則を網羅。型、命名規則、カーソルベースのページネーション、エラーモデリング、セキュリティ考慮事項に関するリファレンスドキュメントを提供。インターフェース、ユニオン、入力型、ミューテーション、ID戦略に関する実践的なパターンをコード例とともに収録...
rover
apollographql
Apollo Rover CLIは、GraphQLスキーマ、フェデレーション、ローカルスーパーグラフ開発を管理するためのツールです。サブグラフスキーマの公開、取得、検証、フェデレーテッドスーパーグラフのローカルまたはGraphOS経由での構成を行います。スキーマチェック(デプロイ前検証)、リンティング、実行中のサーバーからのイントロスペクションを含みます。rover devコマンドは、開発ワークフロー向けに自動スキーマ構成を備えたローカルRouterを起動します。公開前チェック検証とスクリプト用のJSON出力を備えたCI/CDパターンをサポートします。必須条件...