|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Commands |
| 6 | + |
| 7 | +### Testing |
| 8 | +- `rake test` - Run all tests |
| 9 | +- `bundle exec rake test` - Run all tests with bundler |
| 10 | +- `ruby -Itest test/specific_test.rb` - Run a single test file |
| 11 | + |
| 12 | +### Development |
| 13 | +- `bundle install` - Install gem dependencies |
| 14 | +- `rake` - Default task (runs tests) |
| 15 | +- `bundle exec rake` - Run with bundler |
| 16 | + |
| 17 | +### Building |
| 18 | +- `gem build shopify_graphql.gemspec` - Build the gem |
| 19 | +- `gem install shopify_graphql-*.gem` - Install built gem locally |
| 20 | + |
| 21 | +## Architecture |
| 22 | + |
| 23 | +This is a Ruby gem that provides a wrapper around the Shopify GraphQL Admin API. The gem follows these architectural patterns: |
| 24 | + |
| 25 | +### Core Components |
| 26 | + |
| 27 | +1. **Client Layer** (`lib/shopify_graphql/client.rb`): |
| 28 | + - Wraps `ShopifyAPI::Clients::Graphql::Admin` |
| 29 | + - Handles HTTP errors and GraphQL errors |
| 30 | + - Provides unified error handling and response parsing |
| 31 | + - Converts responses to OpenStruct objects for easy access |
| 32 | + |
| 33 | +2. **Query/Mutation Mixins** (`lib/shopify_graphql/query.rb`, `lib/shopify_graphql/mutation.rb`): |
| 34 | + - Provide `include ShopifyGraphql::Query` or `include ShopifyGraphql::Mutation` |
| 35 | + - Add class methods for easy instantiation: `ClassName.call(...)` |
| 36 | + - Delegate `execute` and `handle_user_errors` methods to client |
| 37 | + |
| 38 | +3. **Response Wrapper** (`lib/shopify_graphql/response.rb`): |
| 39 | + - Wraps GraphQL responses with rate limit information |
| 40 | + - Provides methods: `points_left`, `points_limit`, `points_restore_rate`, `query_cost`, `points_maxed?` |
| 41 | + |
| 42 | +4. **Exception Hierarchy** (`lib/shopify_graphql/exceptions.rb`): |
| 43 | + - Custom exceptions for different HTTP error codes |
| 44 | + - Special `UserError` exception for GraphQL userErrors |
| 45 | + - All inherit from `ShopifyAPI::Errors::HttpResponseError` |
| 46 | + |
| 47 | +### Built-in GraphQL Wrappers (`app/graphql/shopify_graphql/`) |
| 48 | + |
| 49 | +Pre-built GraphQL operations following the gem's conventions: |
| 50 | +- `CurrentShop` - Get shop information (equivalent to `ShopifyAPI::Shop.current`) |
| 51 | +- Subscription management: `CreateRecurringSubscription`, `CreateUsageSubscription`, `CancelSubscription`, `GetAppSubscription` |
| 52 | +- Metafield operations: `UpsertPrivateMetafield`, `DeletePrivateMetafield` |
| 53 | +- Bulk operations: `CreateBulkQuery`, `CreateBulkMutation`, `GetBulkOperation` |
| 54 | +- File uploads: `CreateStagedUploads` |
| 55 | + |
| 56 | +### Naming Conventions |
| 57 | + |
| 58 | +The gem enforces these conventions for organizing GraphQL code: |
| 59 | +- Use `app/graphql/` folder for GraphQL-related code |
| 60 | +- Queries: Use `Get` prefix (e.g., `GetProduct`, `GetAppSubscription`) |
| 61 | +- Mutations: Use imperative names (e.g., `CreateUsageSubscription`, `UpdateProduct`) |
| 62 | +- Fields: Use `Fields` suffix (e.g., `ProductFields`, `AppSubscriptionFields`) |
| 63 | +- Organize related operations in the same namespace |
| 64 | + |
| 65 | +### Error Handling |
| 66 | + |
| 67 | +The gem provides comprehensive error handling: |
| 68 | +- HTTP errors are mapped to specific exception classes |
| 69 | +- GraphQL errors are handled by `Client#handle_graphql_errors` |
| 70 | +- User errors from mutations should be handled with `handle_user_errors(response.data)` |
| 71 | +- Network errors (timeouts, connection issues) are caught and re-raised as `ServerError` |
| 72 | + |
| 73 | +### Session Management |
| 74 | + |
| 75 | +Depends on `shopify_app` gem for authentication. All GraphQL operations must be wrapped in: |
| 76 | +```ruby |
| 77 | +shop.with_shopify_session do |
| 78 | + # GraphQL operations here |
| 79 | +end |
| 80 | +``` |
| 81 | + |
| 82 | +### Testing |
| 83 | + |
| 84 | +- Uses Minitest framework |
| 85 | +- Test helper sets up fake Shopify session and WebMock for API stubbing |
| 86 | +- Fixtures stored in `test/fixtures/` directory |
| 87 | +- Helper method `fake(fixture_path, query, **variables)` for stubbing GraphQL requests |
| 88 | + |
| 89 | +## Development Notes |
| 90 | + |
| 91 | +- Ruby 3.0+ required |
| 92 | +- Depends on `shopify_api` >= 13.4 and `shopify_app` >= 19.0 |
| 93 | +- Engine integration available for Rails applications |
| 94 | +- Webhook functionality is deprecated and will be removed in v3.0 |
0 commit comments