Back to Skills

use-graphql-api

pjt222
Updated 6 days ago
19 views
17
2
17
View on GitHub
Metaaiapiautomationdesigndata

About

This skill enables command-line interaction with GraphQL APIs, allowing developers to introspect schemas, construct and execute queries/mutations using tools like `gh api graphql` or `curl`, and parse responses with `jq`. It's particularly useful for automating GitHub operations (Discussions, Issues, Projects) and building CLI workflows that require structured API data. The skill supports chaining operations by piping IDs between calls for scripting and integration tasks.

Quick Install

Claude Code

Recommended
Primary
npx skills add pjt222/agent-almanac -a claude-code
Plugin CommandAlternative
/plugin add https://github.com/pjt222/agent-almanac
Git CloneAlternative
git clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/use-graphql-api

Copy and paste this command in Claude Code to install this skill

Documentation

用 GraphQL API

於命列察、構、執、鏈 GraphQL 行。

  • 經 GraphQL 端詢或變數→用
  • 自動 GitHub 須 GraphQL 之行(Discussions、Projects v2)→用
  • 殼本取 GraphQL 結構數→用
  • 鏈多 GraphQL 召(前出為後入)→用

  • :GraphQL 端 URL 或服名(如 github
  • :行意(讀或寫何數)
  • :認令或法(默:GitHub 用 gh CLI 認)
  • :出式(純 JSON、jq 濾、變派)

一:察綱

定可用型、欄、詢、變。

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 status
  • Cannot query field — 端或禁察;參其文
  • 接拒 — 驗 URL 與網

二:辨行型

定須詢(讀)、變(寫)、訂(流)。

取數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"

得:詢或變辨明、附確行名(如 createDiscussionrepository)。

敗:

  • 行未尋 — 以廣詞搜或察 API 版
  • 詢變難辨 — 變態為變

三:構行

以欄、參、變建詢或變。

詢例 — 取庫議類

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"

構則

  1. 必用變($var: Type!)代內值以複用
  2. 僅取所需欄以縮應
  3. 分頁連用 first: Nnodes
  4. 各物選加 id——後鏈須用

得:法正 GraphQL 行附宜變、欄選、分頁參。

敗:

  • 法誤 — 察括配與末逗(GraphQL 無末逗)
  • 型不合 — 對綱驗變型(如 ID! vs String!
  • 缺必欄 — 按綱加必入欄

四:以 CLI 執

行行、捕應。

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 — 詢無配記;驗入值
  • HTTP 403 — 令缺範;GitHub 察 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
  • Unicode 疾 — jq 加 -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 察各中值
  • ID 式誤 — GitHub 節 ID 為不透串(如 R_kgDO...);勿手構
  • 率限 — 召間加 sleep 1 或以別名批詢

  1. 察詢返綱數(步一成)
  2. 構詢法正(無 GraphQL 析誤)
  3. 應含 data 鍵而無 errors
  4. 取值合期型(ID 為非空串、計為數)
  5. 鏈行端到端成(變用前詢之 ID)

忘必變型之 !察綱可空否;多入欄為非空(!
GraphQL 用 REST IDGraphQL 用不透節 ID;經 GraphQL 取,非 REST
大果不分頁first/afterpageInfo { hasNextPage endCursor }
硬編 ID 不詢ID 隨境異;必動詢
errorsdata 在亦察誤——部分誤可能
嵌 JSON 殼引疾gh--jq 旗或經 jq 別管

GitHub Repository

pjt222/agent-almanac
Path: i18n/wenyan-ultra/skills/use-graphql-api
0
agentsagentskillsai-assisted-developmentclaude-codeskillsteams

Related Skills

content-collections

Meta

This skill provides a production-tested setup for Content Collections, a TypeScript-first tool that transforms Markdown/MDX files into type-safe data collections with Zod validation. Use it when building blogs, documentation sites, or content-heavy Vite + React applications to ensure type safety and automatic content validation. It covers everything from Vite plugin configuration and MDX compilation to deployment optimization and schema validation.

View skill

polymarket

Meta

This skill enables developers to build applications with the Polymarket prediction markets platform, including API integration for trading and market data. It also provides real-time data streaming via WebSocket to monitor live trades and market activity. Use it for implementing trading strategies or creating tools that process live market updates.

View skill

creating-opencode-plugins

Meta

This skill helps developers create OpenCode plugins that hook into 25+ event types like commands, files, and LSP operations. It provides the plugin structure, event API specifications, and implementation patterns for JavaScript/TypeScript modules. Use it when you need to intercept, monitor, or extend the OpenCode AI assistant's lifecycle with custom event-driven logic.

View skill

sglang

Meta

SGLang is a high-performance LLM serving framework that specializes in fast, structured generation for JSON, regex, and agentic workflows using its RadixAttention prefix caching. It delivers significantly faster inference, especially for tasks with repeated prefixes, making it ideal for complex, structured outputs and multi-turn conversations. Choose SGLang over alternatives like vLLM when you need constrained decoding or are building applications with extensive prefix sharing.

View skill