Connect Client
Connect Client enables you to generate type-safe gRPC/Connect clients and OpenAPI specifications directly from your GraphQL operations. This allows you to consume your Federated (or monolithic) Graph using standard gRPC tooling in any language, or expose REST APIs via OpenAPI without writing manual adapters.Overview
While Connect gRPC Services (gRPC Subgraphs) focuses on implementing subgraphs using gRPC, Connect Client focuses on the consumer side. It allows you to define GraphQL operations (Queries, Mutations and soon Subscriptions) and compile them into a Protobuf service definition. The Cosmo Router acts as a bridge. It serves your generated operations via the Connect protocol, executes them against your Federated Graph, and maps the GraphQL response back to typed Protobuf messages.Workflow
The typical workflow involves defining your operations, configuring the Router to serve them, and then distributing the Protobuf or OpenAPI definition to consumers who generate their client SDKs.Usage Example
1. Define GraphQL Operations
Create a directory for your operations, e.g.,services/:
services/GetEmployee.graphql
2. Generate Protobuf
Run thewgc grpc-service generate command with the --with-operations flag. You must also provide the schema SDL to validate the operations.
Each collection of operations represents a distinct Protobuf service. You can organize your operations into different directories (packages) to create multiple services, giving you the flexibility to expose specific subsets of your graph to different consumers or applications.
service.proto file and a service.proto.lock.json file in the ./services directory.
3. Configure and Start Router
Enable the ConnectRPC server in yourconfig.yaml and point the services provider to the directory containing your generated service.proto.
config.yaml
service.proto.
4. Generate Client SDK
Use buf orprotoc to generate the client code for your application.
Example buf.gen.yaml for Go:
buf.gen.yaml
5. Use the Client
You can now use the generated client to call your GraphQL API via the Router. The Router acts as the server implementation for your generated service.Directory Structure & Organization
The Cosmo Router uses a convention-based directory structure to automatically discover and load Connect RPC services. This approach co-locates proto files with their GraphQL operations for easy management.Configuration
Configure the router to point to your root services directory using a storage provider:config.yaml
services directory and automatically discover all proto files and their associated GraphQL operations.
Recommended Structure
For organization purposes, we recommend keeping all services in a rootservices directory, with subdirectories for packages and individual services.
Single Service per Package
When you have one service per package, you can organize files directly in the package directory:Multiple Services per Package
When multiple services share the same proto package, organize them in service subdirectories:Flexible Organization
The router determines service identity by proto package declarations, not directory names. This gives you flexibility in organizing your files:Service Uniqueness: The combination of proto package name and service name must be unique. For example, you can have multiple services with the same package name (e.g.,
company.v1) as long as they have different service names (EmployeeService, DepartmentService). The router uses the package + service combination to identify and route requests.Discovery Rules
The router follows these rules when discovering services:- Recursive Discovery: The router recursively walks the services directory to find all
.protofiles - Proto Association: Each
.protofile discovered becomes a service endpoint - Operation Association: All
.graphqlfiles in the same directory (or subdirectories) are associated with the nearest parent.protofile - Nested Proto Limitation: If a
.protofile is found in a directory, any.protofiles in subdirectories are not discovered (the parent proto takes precedence)
Example: Nested Proto Files
Best Practices
- Use Semantic Versioning: Include version numbers in package names (e.g.,
employee.v1,employee.v2) to support API evolution - Co-locate Operations: Keep GraphQL operations close to their proto definitions for easier maintenance
- Consistent Naming: Use clear, descriptive names for packages and services that reflect their purpose
- Lock File Management: Always commit
.proto.lock.jsonfiles to version control to maintain field number stability
Observability
The Cosmo Router provides built-in observability features that work seamlessly with Connect Client. Because the Router translates RPC calls into GraphQL operations, you get detailed metrics and tracing for both layers.- GraphQL Metrics: Track the performance, error rates, and usage of your underlying GraphQL operations (
GetEmployee, etc.). - Request Tracing: Trace the entire flow from the incoming RPC request, through the GraphQL engine, to your subgraphs and back.
- Standard Protocols: Export data using OpenTelemetry (OTLP) or Prometheus to your existing monitoring stack (Grafana, Datadog, Cosmo Cloud, etc.).
Forward Compatibility & Lock Files
When you generate your Protobuf definition, the CLI creates aservice.proto.lock.json file. You should commit this file to your version control system.
This lock file maintains a history of your operations and their field assignments. When you modify your operations (e.g., add new fields, reorder fields, or add new operations), the CLI uses the lock file to ensure:
- Stable Field Numbers: Existing fields retain their unique Protobuf field numbers, even if you reorder them in the GraphQL query.
- Safe Evolution: You can safely evolve your client requirements without breaking existing clients.
Roadmap
The following features are planned for future releases of Connect Client:- OpenAPI Generation: Enhanced support for generating OpenAPI specifications including descriptions, summaries, deprecated fields, and tags.
- Subscription Support: Ability to consume GraphQL subscriptions as gRPC streams, enabling real-time data updates over the Connect protocol.
- Multiple Root Fields: Support for executable operations containing multiple root selection set fields, allowing more complex queries in a single operation.
- Field Aliases: Support for GraphQL aliases to control the shape of the API surface, enabling customized field names in the generated Protobuf definitions.