Overview
Namespaces allow you to run multiple GraphQL schemas in the same Rails application. Each schema can have its own set of queries, mutations, and subscriptions while sharing common types and components.Use Cases
Admin & Client APIs
Separate admin operations from public client queries with different permission models.
API Versioning
Run multiple API versions side-by-side during migrations.
Multi-tenant Apps
Provide tenant-specific schemas with shared base types.
Feature Isolation
Isolate experimental features in separate schemas.
How Namespaces Work
Three key rules to remember:- Schemas refer to a single namespace
- All other components (types, sources, directives) can have multiple namespaces
- The
:basenamespace is the fallback for all schemas
Creating Multiple Schemas
Define Schemas with Namespaces
Configure Routes
Setting Component Namespaces
Namespaces can be set on any GraphQL component:Force a Single Namespace
Useset_namespace to replace inherited namespaces:
Add to Namespaces
Usenamespace to add to the list:
Check Current Namespaces
Module Association
Automate namespace assignment by associating Ruby modules with namespaces:Using Type Map
GraphQL::Admin automatically get the :admin namespace:
Using Schema
Schemas can also associate modules:Real-World Example
Here’s a complete example with admin and client schemas:Shared Base Components
Admin-Only Components
Client-Only Components
Namespace Inheritance
Namespaces are inherited by default:Sources and Namespaces
Sources automatically attach to schemas in their namespaces:Directives and Namespaces
Directives can also use namespaces:The :base Namespace
The:base namespace is special:
- Acts as a fallback for all schemas
- Types without explicit namespaces default to
:base - Shared components should use
:base
Checking Component Availability
Query which schemas have access to a component:Best Practices
Use Module Organization
Use Module Organization
Organize code by namespace using modules:
Share Common Types
Share Common Types
Explicit Over Implicit
Explicit Over Implicit
Be explicit about namespaces for clarity:
Use Module Association
Use Module Association
Leverage module association to reduce boilerplate:
Document Namespace Strategy
Document Namespace Strategy
Document your namespace strategy in a README:
- Which namespaces exist
- What each namespace represents
- How to add components to namespaces
Testing with Namespaces
Troubleshooting
Type Not Found in Schema
Type Not Found in Schema
Problem: Type exists but isn’t available in schema.Solution: Check namespace configuration:Ensure they match or component uses
:base.Namespace Override Issues
Namespace Override Issues
Problem:
set_namespace not working as expected.Solution: set_namespace replaces all namespaces. Use namespace to add:Module Association Not Working
Module Association Not Working
Problem: Module association not applying.Solution: Ensure association happens before type loading:
Migration Strategies
Adding a New Namespace
- Create new schema with namespace
- Associate module
- Move/create types under module
- Update routes
- Test both schemas
Merging Namespaces
- Update components to use both namespaces temporarily
- Deprecate old schema
- Migrate clients
- Remove old namespace
Splitting a Namespace
- Create new namespace
- Copy shared types to
:base - Move specific types to new namespace
- Update module associations
- Test thoroughly
Next Steps
Custom Sources
Build sources that work with namespaces
Schema Configuration
Learn more about schema configuration