-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-local.sh
More file actions
executable file
·254 lines (223 loc) · 7.34 KB
/
run-local.sh
File metadata and controls
executable file
·254 lines (223 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
# Source shared utilities
if [ -z "$SCRIPT_DIR" ]; then
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
fi
# If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
source "$SCRIPT_DIR/utils.sh"
source "$SCRIPT_DIR/config.sh"
else
source "$SCRIPT_DIR/scripts/utils.sh"
source "$SCRIPT_DIR/scripts/config.sh"
fi
# =============================================================================
# Apollo Supergraph - Local Development Script (No Kubernetes)
# =============================================================================
#
# This script runs the Apollo Supergraph locally WITHOUT Kubernetes:
# - Subgraphs: Runs directly with npm start (Node.js)
# - Router: Runs directly with rover dev (Apollo Router)
#
# This is different from the Kubernetes deployment which runs everything
# in containers within minikube. This approach is faster and simpler
# for development.
#
# Usage:
# ./run-local.sh # Run both subgraphs and router
# ./run-local.sh --subgraphs-only # Run only subgraphs
# ./run-local.sh --router-only # Run only router
#
# =============================================================================
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo " -s, --subgraphs-only Run only the subgraphs (for development)"
echo " -r, --router-only Run only the router (requires subgraphs running)"
echo ""
echo "Examples:"
echo " $0 # Run both subgraphs and router"
echo " $0 --subgraphs-only # Run only subgraphs"
echo " $0 --router-only # Run only router"
echo " $0 --help # Show this help message"
}
# Parse command line arguments
RUN_SUBGRAPHS=true
RUN_ROUTER=true
while [[ $# -gt 0 ]]; do
case $1 in
-s|--subgraphs-only)
RUN_SUBGRAPHS=true
RUN_ROUTER=false
shift
;;
-r|--router-only)
RUN_SUBGRAPHS=false
RUN_ROUTER=true
shift
;;
-h|--help)
show_usage
exit 0
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
echo "🚀 Starting Apollo Supergraph locally..."
# Check if .env file exists
if [ ! -f "router/.env" ]; then
print_error "router/.env file not found. Please create it with APOLLO_KEY and APOLLO_GRAPH_REF"
print_error "Run the following command to create it safely:"
print_error " if [ ! -f \"router/.env\" ]; then cp router/env.example router/.env; fi"
print_error "Then edit router/.env with your actual Apollo Studio credentials"
exit 1
fi
# Install/update Rover if needed
print_status "Checking Rover installation..."
if ! command -v rover &> /dev/null; then
print_status "Rover not found. Installing Rover..."
cd router
./download-rover.sh
cd ..
else
print_success "Rover is already installed"
fi
# Generate supergraph schema
print_status "Generating supergraph schema..."
cd router
./compose.sh
cd ..
print_success "Supergraph schema generated"
# Function to cleanup background processes
cleanup() {
print_status "Cleaning up background processes..."
pkill -f "npm start" || true
pkill -f "rover dev" || true
print_success "Cleanup completed"
}
# Set up trap to cleanup on script exit
trap cleanup EXIT
# Run subgraphs if requested
if [ "$RUN_SUBGRAPHS" = true ]; then
print_status "Starting subgraphs..."
cd subgraphs
npm start &
SUBGRAPHS_PID=$!
cd ..
# Wait for subgraphs to be ready
print_status "Waiting for subgraphs to be ready..."
sleep 5
# Test if subgraphs are responding
for i in {1..15}; do
if curl -s "$(get_subgraphs_products_url)" > /dev/null 2>&1 && \
curl -s "$(get_subgraphs_reviews_url)" > /dev/null 2>&1 && \
curl -s "$(get_subgraphs_users_url)" > /dev/null 2>&1; then
print_success "Subgraphs are ready!"
break
fi
if [ $i -eq 15 ]; then
print_error "Subgraphs failed to start properly"
exit 1
fi
print_status "Waiting for subgraphs... (attempt $i/15)"
sleep 3
done
# Regenerate supergraph schema after subgraphs are ready
print_status "Regenerating supergraph schema..."
cd router
./compose.sh
cd ..
print_success "Supergraph schema regenerated"
fi
# Run router if requested
if [ "$RUN_ROUTER" = true ]; then
# Check if subgraphs are running when using router-only mode
if [ "$RUN_SUBGRAPHS" = false ]; then
print_status "Checking if subgraphs are running..."
if ! curl -s "$(get_subgraphs_products_url)" > /dev/null 2>&1; then
print_error "Subgraphs are not running on $(get_subgraphs_url)"
print_error "Please start the subgraphs first:"
print_error " ./run-local.sh --subgraphs-only"
print_error " or"
print_error " ./run-local.sh"
exit 1
fi
print_success "Subgraphs are running"
fi
print_status "Starting Apollo Router..."
cd router
./rover-dev.sh &
ROUTER_PID=$!
cd ..
# Wait for router to be ready
print_status "Waiting for Apollo Router to be ready..."
sleep 5
# Test if router is responding
for i in {1..10}; do
if curl -s "$(get_router_health_url)" > /dev/null 2>&1; then
print_success "Apollo Router is ready!"
break
fi
if [ $i -eq 10 ]; then
print_error "Apollo Router failed to start properly"
print_error "Check router logs for more details"
exit 1
fi
print_status "Waiting for router... (attempt $i/10)"
sleep 2
done
fi
print_success "Apollo Supergraph is running locally!"
echo ""
echo "📋 Service Status:"
if [ "$RUN_SUBGRAPHS" = true ]; then
echo " ✅ Subgraphs: $(get_subgraphs_url)"
echo " - Products: $(get_subgraphs_products_url)"
echo " - Reviews: $(get_subgraphs_reviews_url)"
echo " - Users: $(get_subgraphs_users_url)"
fi
if [ "$RUN_ROUTER" = true ]; then
echo " ✅ Apollo Router: $(get_router_graphql_url)"
echo " ✅ Router Health: $(get_router_health_url)"
fi
echo ""
echo "🧪 Test the GraphQL API:"
echo " curl -X POST $(get_router_graphql_url) \\"
echo " -H \"Content-Type: application/json\" \\"
echo " -d '{\"query\":\"{ searchProducts { id title price } }\"}'"
echo ""
echo "🔍 Useful commands:"
echo " - View subgraphs logs: tail -f subgraphs/logs/*"
echo " - View router logs: Check the terminal where rover-dev.sh is running"
echo " - Stop all services: Ctrl+C (this script will cleanup automatically)"
echo ""
echo "⚠️ Press Ctrl+C to stop all services"
# Keep the script running and wait for background processes
wait