Prerequisites
- Cosmo CLI installed and configured
Overview
This tutorial shows you how to create a gRPC subgraph from scratch using the Cosmo CLI templates. You’ll start with a project template that includes all the necessary files and code generation to get up and running quickly. The tutorial supports both TypeScript and Go implementations using Connect RPC, which is a modern gRPC framework with a great developer experience and performance.Step 1: Initialize a gRPC Service Template
First, list the available gRPC templates:golang-connect-rpc- Go implementation using Connect RPCtypescript-connect-rpc-fastify- TypeScript implementation using Connect RPC and Fastify
- TypeScript
- Go
Step 2: Define Your GraphQL Schema
The template includes a simple “hello” schema. Let’s replace it with a more comprehensive project management schema. Edit the schema file:- TypeScript
- Go
Edit
src/graph/schema.graphql:Step 3: Generate Code and Configuration
The template includes build scripts that handle all code generation:- TypeScript
- Go
npm run generate:proto- Generates protobuf files from GraphQL schemanpm run generate:buf- Generates Connect RPC TypeScript codenpm run generate:router- Generates router execution confignpm run router:download- Downloads the Cosmo Router binary
Step 4: Implement Your Service Logic
Now implement the business logic for your gRPC service:- TypeScript
- Go
Edit
src/routes.ts:Step 5: Run Your gRPC Service and Router
Start both the gRPC service and the Cosmo Router:- TypeScript
- Go
- gRPC service on port 50051
- Cosmo Router on port 3002
Step 6: Test Your Integration
Open your browser to http://localhost:3002 to access the GraphQL Playground. Try these queries:Understanding the Generated Files
The template creates several important files:- TypeScript
- Go
src/graph/schema.graphql- Your GraphQL schema (source of truth)src/proto/service/v1/- Generated protobuf filesrouter.compose.yaml- Router composition configurationrouter.config.yaml- Router runtime configurationrouter.execution.config.json- Generated router execution configpackage.json- Build scripts and dependencies