技能创建器
skill-creator
创建、改进和评估自定义技能,把专业流程转化为可重复使用的 AI 能力。
use-graphql-api【数据与研究】Interact with GraphQL APIs from the command line — discover schemas via introspection, construct queries and mutations, execute them with gh api graphql or curl, parse responses with jq, and chain operations by piping IDs between calls. Use when automating GitHub Discussions, Issues, or Projects via GraphQL, integrating with any GraphQL endpoint from…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
於命列察、構、執、鏈 GraphQL 行。
github)gh CLI 認)定可用型、欄、詢、變。
GitHub:
# List available query fields
gh api graphql -f query='{ __schema { queryType { fields { name description } } } }' \
| jq '.data.__schema.queryType.fields[] | {name, description}'
# List available mutation fields
gh api graphql -f query='{ __schema { mutationType { fields { name description } } } }' \
| jq '.data.__schema.mutationType.fields[] | {name, description}'
# Inspect a specific type
gh api graphql -f query='{
__type(name: "Repository") {
fields { name type { name kind ofType { name } } }
}
}' | jq '.data.__type.fields[] | {name, type: .type.name // .type.ofType.name}'
泛端:
# Full introspection query via curl
curl -s -X POST https://api.example.com/graphql \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"query":"{ __schema { types { name kind fields { name } } } }"}' \
| jq '.data.__schema.types[] | select(.kind == "OBJECT") | {name, fields: [.fields[].name]}'
得:JSON 列可用型、欄、變。綱應確端可達且令有效。
敗:
401 Unauthorized — 驗令;GitHub 行 gh auth statusCannot query field — 端或禁察;參其文定須詢(讀)、變(寫)、訂(流)。
| 意 | 行 | 例 |
|---|---|---|
| 取數 | query | 取庫詳、列議 |
| 建/更/刪 | mutation | 建議、加註 |
| 即時更 | subscription | 觀新議(CLI 罕) |
GitHub 特行→GitHub GraphQL API 文。
# Quick check: does the mutation exist?
gh api graphql -f query='{ __schema { mutationType { fields { name } } } }' \
| jq '.data.__schema.mutationType.fields[].name' | grep -i "discussion"
得:詢或變辨明、附確行名(如 createDiscussion、repository)。
敗:
以欄、參、變建詢或變。
詢例 — 取庫議類:
gh api graphql -f query='
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
discussionCategories(first: 10) {
nodes { id name }
}
}
}
' -f owner="OWNER" -f repo="REPO" | jq '.data.repository.discussionCategories.nodes'
變例 — 建 GitHub Discussion:
gh api graphql -f query='
mutation($repoId: ID!, $categoryId: ID!, $title: String!, $body: String!) {
createDiscussion(input: {
repositoryId: $repoId,
categoryId: $categoryId,
title: $title,
body: $body
}) {
discussion { url number }
}
}
' -f repoId="$REPO_ID" -f categoryId="$CAT_ID" \
-f title="My Discussion" -f body="Discussion body here"
構則:
$var: Type!)代內值以複用first: N 與 nodesid——後鏈須用得:法正 GraphQL 行附宜變、欄選、分頁參。
敗:
ID! vs String!)行行、捕應。
GitHub — 用 gh api graphql:
# Simple query
gh api graphql -f query='{ viewer { login } }'
# With variables
gh api graphql \
-f query='query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) { id name }
}' \
-f owner="octocat" -f repo="Hello-World"
# With jq post-processing
REPO_ID=$(gh api graphql \
-f query='query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) { id }
}' \
-f owner="OWNER" -f repo="REPO" \
--jq '.data.repository.id')
泛端 — 用 curl:
curl -s -X POST "$GRAPHQL_ENDPOINT" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "$(jq -n \
--arg query 'query { users { id name } }' \
'{query: $query}'
)"
得:JSON 應附 data 鍵含請欄,或 errors 陣若敗。
敗:
errors — 讀訊;常因為缺權、ID 誤、率限data — 詢無配記;驗入值gh auth status、加範以 gh auth refresh -s scope由 JSON 應取所需數。
# Extract a single value
gh api graphql -f query='{ viewer { login } }' --jq '.data.viewer.login'
# Extract from a list
gh api graphql -f query='
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
issues(first: 5, states: OPEN) {
nodes { number title }
}
}
}
' -f owner="OWNER" -f repo="REPO" \
--jq '.data.repository.issues.nodes[] | "\(.number): \(.title)"'
# Assign to a variable for later use
CATEGORY_ID=$(gh api graphql -f query='
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
discussionCategories(first: 20) {
nodes { id name }
}
}
}
' -f owner="OWNER" -f repo="REPO" \
--jq '.data.repository.discussionCategories.nodes[] | select(.name == "Show and Tell") | .id')
得:清取值,可示或派殼變。
敗:
jq 返 null — 欄路誤;先管純 JSON 入 jq . 察構select() 濾或 | first-r 取純串出前行之出為後行之入。
# Step A: Get the repository ID
REPO_ID=$(gh api graphql \
-f query='query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) { id }
}' \
-f owner="$OWNER" -f repo="$REPO" \
--jq '.data.repository.id')
# Step B: Get the discussion category ID
CAT_ID=$(gh api graphql \
-f query='query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
discussionCategories(first: 20) {
nodes { id name }
}
}
}' \
-f owner="$OWNER" -f repo="$REPO" \
--jq '.data.repository.discussionCategories.nodes[]
| select(.name == "Show and Tell") | .id')
# Step C: Create the discussion using both IDs
RESULT=$(gh api graphql \
-f query='mutation($repoId: ID!, $catId: ID!, $title: String!, $body: String!) {
createDiscussion(input: {
repositoryId: $repoId,
categoryId: $catId,
title: $title,
body: $body
}) {
discussion { url number }
}
}' \
-f repoId="$REPO_ID" -f catId="$CAT_ID" \
-f title="$TITLE" -f body="$BODY" \
--jq '.data.createDiscussion.discussion')
echo "Created: $(echo "$RESULT" | jq -r '.url')"
式:必於前詢取 id 欄以為後變之 ID! 變傳。
得:多步流,每召成、ID 正流於行間。
敗:
set -e 察各中值R_kgDO...);勿手構sleep 1 或以別名批詢data 鍵而無 errors| 忌 | 防 |
|---|---|
忘必變型之 ! | 察綱可空否;多入欄為非空(!) |
| GraphQL 用 REST ID | GraphQL 用不透節 ID;經 GraphQL 取,非 REST |
| 大果不分頁 | 用 first/after 與 pageInfo { hasNextPage endCursor } |
| 硬編 ID 不詢 | ID 隨境異;必動詢 |
忽 errors 陣 | data 在亦察誤——部分誤可能 |
| 嵌 JSON 殼引疾 | 用 gh 之 --jq 旗或經 jq 別管 |