Skip to content

Commit d29971e

Browse files
committed
Add test_graphql_comments management command for easy setup verification
This commit adds a Django management command that simplifies testing and verification of GraphQL SQLCommenter setup, as requested for the quickstart guide. New Features: - Management command: python manage.py test_graphql_comments - Checks configuration (middleware, settings, paths) - Sets test GraphQL metadata - Executes test SQL query - Analyzes queries for GraphQL comments - Reports success/failure with troubleshooting tips - Command options: - --operation-name: Test with custom operation name - --query-type: Test different types (query/mutation/subscription) - --verbose: Show full SQL queries in output Documentation Updates: - Simplified quickstart guide to feature the management command - Moved advanced testing methods to API Reference - Added "Quick Test" section to Troubleshooting guide - Updated Example README to include management command Testing: - 15 comprehensive tests with 100% coverage of management command - All 96 tests pass across Python 3.9, 3.11, and 3.13 - Overall project coverage increased from 91% to 95% The management command provides a simple, single-command way to verify GraphQL SQL commenting is working correctly, improving the developer experience during initial setup.
1 parent cd2a517 commit d29971e

9 files changed

Lines changed: 584 additions & 55 deletions

File tree

docs/getting-started/quickstart.md

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -101,64 +101,42 @@ You'll need at least Django 3.2+ and Python 3.8+. If you want subscription suppo
101101
SQLCOMMENTER_WITH_SUBSCRIPTIONS = True
102102
```
103103

104-
## 3. Test Locally
104+
## 3. Test It Works
105105

106-
### Start Django
106+
Verify your setup with the built-in test command:
107107

108108
```bash
109-
python manage.py runserver
109+
python manage.py test_graphql_comments
110110
```
111111

112-
### Send GraphQL Request
113-
114-
```bash
115-
curl -X POST http://localhost:8000/graphql \
116-
-H "Content-Type: application/json" \
117-
-d '{
118-
"query": "query getUserProfile { user(id: 1) { username } }",
119-
"operationName": "getUserProfile"
120-
}'
121-
```
122-
123-
### Check SQL Comments
124-
125-
```python title="Django shell or view"
126-
from django.db import connection
127-
from django.test.utils import override_settings
128-
129-
with override_settings(DEBUG=True):
130-
# Execute your GraphQL query
131-
# Then check:
132-
for query in connection.queries:
133-
print(query['sql'])
134-
135-
# Should see:
136-
# SELECT * FROM auth_user WHERE id = 1
137-
# /*framework=django:4.2,graphql_op=getUserProfile,graphql_type=query,graphql_sha=abc123*/
112+
**Example output:**
138113
```
114+
=== GraphQL SQLCommenter Test ===
139115
140-
## 4. Verify It Works
116+
1. Checking configuration...
117+
✓ GraphQL commenting enabled
118+
✓ GraphQL paths: /graphql
119+
✓ SqlCommenterMiddleware found
141120
142-
Create a test view:
121+
2. Setting test GraphQL metadata...
122+
✓ Metadata set: op=testOperation, type=query
143123
144-
```python title="your_app/views.py"
145-
from django.http import JsonResponse
146-
from django.db import connection
147-
from graphql_sqlcommenter.context import get_graphql_meta
124+
3. Executing test database query...
125+
✓ Executed 1 SQL query(ies)
148126
127+
4. Analyzing SQL comments...
128+
✓ Query 1: Found all GraphQL metadata
149129
150-
def test_graphql_meta(request):
151-
"""Debug view to check GraphQL metadata."""
152-
meta = get_graphql_meta()
153-
return JsonResponse({
154-
'metadata': meta,
155-
'recent_queries': [q['sql'] for q in connection.queries[-5:]]
156-
})
130+
✓ SUCCESS: GraphQL comments are working!
157131
```
158132

159-
Execute a GraphQL query, then hit this endpoint to verify metadata was captured.
133+
!!! note "More Testing Options"
134+
For advanced testing methods (Django shell, unit tests, debug views, etc.), see the [API Reference - Testing section](../reference/api.md#testing-with-management-command).
135+
136+
!!! warning "Test Failed?"
137+
If the test fails, see the [Troubleshooting Guide](../reference/troubleshooting.md) for detailed debugging steps.
160138

161-
## 5. Deploy to Staging
139+
## 4. Deploy to Staging
162140

163141
```bash
164142
# Commit changes
@@ -176,7 +154,7 @@ git push staging main
176154
# - Filter: graphql_op='getUserProfile'
177155
```
178156

179-
## 6. Production Rollout
157+
## 5. Production Rollout
180158

181159
Add feature flag for gradual rollout:
182160

docs/reference/api.md

Lines changed: 142 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,64 @@ curl -X POST http://localhost:8000/graphql \
128128

129129
**Location:** `graphql_sqlcommenter.middleware.SqlCommenterMiddleware`
130130

131+
## Management Commands
132+
133+
### `test_graphql_comments`
134+
135+
Django management command to test GraphQL SQL comment functionality.
136+
137+
```bash
138+
python manage.py test_graphql_comments
139+
```
140+
141+
**What it does:**
142+
143+
1. ✓ Checks configuration (middleware, settings, paths)
144+
2. ✓ Sets test GraphQL metadata
145+
3. ✓ Executes a test SQL query
146+
4. ✓ Analyzes queries for GraphQL comments
147+
5. ✓ Reports success/failure with troubleshooting tips
148+
149+
**Options:**
150+
151+
```bash
152+
# Test with custom operation name
153+
python manage.py test_graphql_comments --operation-name myCustomOp
154+
155+
# Test different operation types
156+
python manage.py test_graphql_comments --query-type mutation
157+
python manage.py test_graphql_comments --query-type subscription
158+
159+
# Show verbose output with actual SQL queries
160+
python manage.py test_graphql_comments --verbose
161+
```
162+
163+
**Example output:**
164+
165+
```
166+
=== GraphQL SQLCommenter Test ===
167+
168+
1. Checking configuration...
169+
✓ GraphQL commenting enabled
170+
✓ GraphQL paths: /graphql
171+
✓ SqlCommenterMiddleware found
172+
173+
2. Setting test GraphQL metadata...
174+
✓ Metadata set: op=testOperation, type=query
175+
176+
3. Executing test database query...
177+
✓ Executed 1 SQL query(ies)
178+
179+
4. Analyzing SQL comments...
180+
✓ Query 1: Found all GraphQL metadata
181+
182+
✓ SUCCESS: GraphQL comments are working!
183+
```
184+
185+
**Location:** `graphql_sqlcommenter.management.commands.test_graphql_comments`
186+
187+
---
188+
131189
## Django App Configuration
132190

133191
### `GraphQLSQLCommenterConfig`
@@ -345,20 +403,96 @@ users = User.objects.all()
345403
# SQL: SELECT * FROM auth_user /*graphql_op=customQuery,...*/
346404
```
347405

348-
### Testing
406+
### Testing with Management Command
407+
408+
```bash
409+
# Quick test
410+
python manage.py test_graphql_comments
411+
412+
# Test specific operation type
413+
python manage.py test_graphql_comments --query-type mutation --verbose
414+
```
415+
416+
### Testing with Real GraphQL Queries
417+
418+
```bash
419+
# Start Django
420+
python manage.py runserver
421+
422+
# Send GraphQL request
423+
curl -X POST http://localhost:8000/graphql \
424+
-H "Content-Type: application/json" \
425+
-d '{
426+
"query": "query getUserProfile { user(id: 1) { username } }",
427+
"operationName": "getUserProfile"
428+
}'
429+
430+
# Check logs for SQL comments
431+
```
432+
433+
### Testing in Django Shell
434+
435+
```python
436+
from django.db import connection
437+
from django.test.utils import override_settings
438+
from graphql_sqlcommenter import set_graphql_meta
439+
440+
# Enable query logging
441+
with override_settings(DEBUG=True):
442+
# Set metadata
443+
set_graphql_meta("testOp", "query", "test123")
444+
445+
# Execute query
446+
from django.contrib.auth.models import User
447+
User.objects.first()
448+
449+
# Check queries
450+
for query in connection.queries:
451+
print(query['sql'])
452+
# Should see: /*graphql_op=testOp,graphql_type=query,graphql_sha=test123*/
453+
```
454+
455+
### Testing in Unit Tests
349456

350457
```python
351458
from graphql_sqlcommenter import set_graphql_meta, get_graphql_meta, clear_graphql_meta
459+
from django.test.utils import CaptureQueriesContext
460+
from django.db import connection
352461

353-
# Set test metadata
354-
set_graphql_meta("testOp", "query", "test123")
462+
def test_graphql_comments():
463+
# Set test metadata
464+
set_graphql_meta("testOp", "query", "test123")
355465

356-
# Verify
357-
meta = get_graphql_meta()
358-
assert meta['op'] == "testOp"
466+
# Verify metadata is set
467+
meta = get_graphql_meta()
468+
assert meta['op'] == "testOp"
359469

360-
# Clean up
361-
clear_graphql_meta()
470+
# Capture queries
471+
with CaptureQueriesContext(connection) as queries:
472+
User.objects.first()
473+
474+
# Verify comment
475+
assert 'graphql_op=testOp' in queries[0]['sql']
476+
477+
# Clean up
478+
clear_graphql_meta()
479+
```
480+
481+
### Testing Debug View
482+
483+
```python
484+
# Create a debug view to inspect metadata
485+
from django.http import JsonResponse
486+
from django.db import connection
487+
from graphql_sqlcommenter.context import get_graphql_meta
488+
489+
def debug_graphql_metadata(request):
490+
"""Debug view to check GraphQL metadata."""
491+
meta = get_graphql_meta()
492+
return JsonResponse({
493+
'metadata': meta,
494+
'recent_queries': [q['sql'] for q in connection.queries[-5:]]
495+
})
362496
```
363497

364498
## Version

docs/reference/troubleshooting.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
Common issues and solutions for GraphQL SQLCommenter.
44

5+
## Quick Test
6+
7+
Before diving into troubleshooting, use the built-in test command:
8+
9+
```bash
10+
python manage.py test_graphql_comments
11+
```
12+
13+
This will check your configuration and verify that SQL comments are working. Options:
14+
15+
```bash
16+
# Test with custom operation name
17+
python manage.py test_graphql_comments --operation-name myCustomOp
18+
19+
# Test different operation types
20+
python manage.py test_graphql_comments --query-type mutation
21+
22+
# See verbose output with actual SQL queries
23+
python manage.py test_graphql_comments --verbose
24+
```
25+
26+
If the test succeeds but you still don't see comments in your application, continue with the troubleshooting steps below.
27+
528
## SQL Comments Not Appearing
629

730
### Issue: No comments in SQL queries

example/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ MIDDLEWARE = [
3333

3434
### 3. Test it
3535

36+
**Quick test with management command:**
37+
38+
```bash
39+
python manage.py test_graphql_comments
40+
```
41+
42+
This command will verify your configuration and test that SQL comments are working correctly.
43+
44+
**Or test with a real GraphQL request:**
45+
3646
```bash
3747
# Run your GraphQL query
3848
curl -X POST http://localhost:8000/graphql \

graphql_sqlcommenter/management/__init__.py

Whitespace-only changes.

graphql_sqlcommenter/management/commands/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)