Use GraphQL API
Discover, construct, execute, and chain GraphQL operations from the command line.
When to Use
- Querying or mutating data via a GraphQL endpoint (GitHub, Hasura, Apollo)
- Automating GitHub ops that need GraphQL (Discussions, Projects v2)
- Shell scripts fetching structured data from GraphQL
- Chaining calls where one operation's output feeds the next
Procedure
Step 1 — Discover the schema
Introspect available types/queries/mutations. GitHub: gh api graphql -f query='{ __schema { queryType { fields { name description } } } }'. Generic: POST a __schema introspection query via curl with a Bearer token. 401 → check token (gh auth status); "Cannot query field" → introspection may be disabled, use the docs.
Step 2 — Identify the operation type
query (read), mutation (write/create/update/delete), subscription (stream, rare in CLI). If the action changes state, it is a mutation. Confirm the exact operation name exists (grep the mutationType fields).
Step 3 — Construct the operation
Rules: always use variables ($var: Type!) not inline values; request only needed fields; use first: N with nodes for connections; add id to every object selection (you'll need it for chaining). Watch nullability: most input fields are non-null (!). GraphQL has no trailing commas.
Step 4 — Execute via CLI
GitHub: gh api graphql -f query='...' -f owner="..." -f repo="...". Generic: curl -s -X POST "$ENDPOINT" -H "Authorization: Bearer $TOKEN" -d "$(jq -n --arg query '...' '{query:$query}')". Errors array → read the message (permissions, invalid IDs, rate limits); 403 → token missing scope (gh auth refresh -s <scope>).
Step 5 — Parse the response
Use --jq (gh) or pipe to jq. Extract single values (.data.viewer.login), iterate lists (.data...nodes[] | ...), or assign IDs to shell variables for later steps.
Step 6 — Chain operations
Extract id fields in earlier queries and pass them as ID! variables to subsequent mutations (e.g., get repo ID → get category ID → create discussion). Node IDs are opaque strings (e.g., R_kgDO...) — never construct them manually. Use set -e so a failed step doesn't silently empty a variable; add sleep 1 if rate-limited.
Validation
- Introspection returns schema data
- Queries syntactically valid (no parser errors)
- Responses have
datawithouterrors - Extracted values match types (IDs non-empty strings)
- Chained operations complete end-to-end
Common Pitfalls
- Forgetting
!on required variable types — check schema nullability. - Using REST IDs in GraphQL — fetch opaque node IDs via GraphQL.
- Not paginating — use
first/afterwithpageInfo { hasNextPage endCursor }. - Hardcoding IDs — they differ between environments; query dynamically.
- Ignoring the
errorsarray even whendatais present (partial errors). - Shell quoting issues with nested JSON — use
--jqor pipe to jq.
Related
- headless-web-scraping — fallback when no API exists
- serialize-data-formats — handling the JSON responses