Skip to content

Commit 5b0bd7c

Browse files
Copilotpelikhan
andcommitted
Add GraphQL query execution support to GitHub client
- Add graphql() method to GitHub interface in prompt_template.d.ts - Implement graphql() method in GitHubClient class with proper error handling - Add test case for GraphQL functionality in githubclient.test.ts - Update GitHub documentation with GraphQL usage examples - Support TypeScript generics for type-safe results Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent d8407bc commit 5b0bd7c

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

docs/src/content/docs/reference/scripts/github.mdx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,83 @@ const releases = await github.listReleases()
216216
console.log(releases)
217217
```
218218

219+
## GraphQL Queries
220+
221+
Execute GraphQL queries against the GitHub API using the `graphql` method. This provides access to GitHub's full GraphQL API v4, which often allows more efficient data retrieval than REST APIs.
222+
223+
```js
224+
// Basic repository information
225+
const query = `
226+
query($owner: String!, $name: String!) {
227+
repository(owner: $owner, name: $name) {
228+
name
229+
description
230+
stargazerCount
231+
forkCount
232+
primaryLanguage {
233+
name
234+
color
235+
}
236+
}
237+
}
238+
`
239+
240+
const result = await github.graphql(query, {
241+
owner: "microsoft",
242+
name: "genaiscript"
243+
})
244+
245+
console.log(result.repository)
246+
```
247+
248+
You can also query multiple data points in a single request:
249+
250+
```js
251+
// Get issues and pull requests together
252+
const complexQuery = `
253+
query($owner: String!, $name: String!) {
254+
repository(owner: $owner, name: $name) {
255+
issues(first: 5, states: OPEN) {
256+
nodes {
257+
number
258+
title
259+
author { login }
260+
}
261+
}
262+
pullRequests(first: 5, states: OPEN) {
263+
nodes {
264+
number
265+
title
266+
author { login }
267+
}
268+
}
269+
}
270+
}
271+
`
272+
273+
const data = await github.graphql(complexQuery, {
274+
owner: "microsoft",
275+
name: "genaiscript"
276+
})
277+
278+
console.log("Open Issues:", data.repository.issues.nodes)
279+
console.log("Open PRs:", data.repository.pullRequests.nodes)
280+
```
281+
282+
The GraphQL method supports TypeScript generics for type safety:
283+
284+
```js
285+
interface RepositoryData {
286+
repository: {
287+
name: string
288+
stargazerCount: number
289+
}
290+
}
291+
292+
const result = await github.graphql<RepositoryData>(query, variables)
293+
// result is now properly typed
294+
```
295+
219296
## Octokit access
220297

221298
Utilize [octokit](https://www.npmjs.com/package/octokit) to access the full GitHub APIs.

packages/core/src/githubclient.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,34 @@ describe("GitHubClient", async () => {
134134
assert(resolved.files[0].content)
135135
assert.strictEqual(resolved.files[0].type, "image/jpeg")
136136
})
137+
138+
await test("graphql() executes GraphQL query", async () => {
139+
// Test with a simple GraphQL query to get repository information
140+
const query = `
141+
query($owner: String!, $name: String!) {
142+
repository(owner: $owner, name: $name) {
143+
name
144+
description
145+
stargazerCount
146+
}
147+
}
148+
`
149+
const info = await client.info()
150+
const variables = {
151+
owner: info.owner,
152+
name: info.repo
153+
}
154+
155+
const result = await client.graphql<{
156+
repository: {
157+
name: string
158+
description: string
159+
stargazerCount: number
160+
}
161+
}>(query, variables)
162+
163+
assert(result.repository)
164+
assert(result.repository.name === info.repo)
165+
assert(typeof result.repository.stargazerCount === "number")
166+
})
137167
})

packages/core/src/githubclient.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,21 @@ export class GitHubClient implements GitHub {
16611661
}
16621662
return res
16631663
}
1664+
1665+
async graphql<T = any>(query: string, variables?: Record<string, any>): Promise<T> {
1666+
const { client } = await this.api()
1667+
dbg(`executing GraphQL query: ${query.slice(0, 100)}...`)
1668+
dbg(`GraphQL variables: %O`, variables)
1669+
1670+
try {
1671+
const result = await client.graphql<T>(query, variables)
1672+
dbg(`GraphQL query executed successfully`)
1673+
return result
1674+
} catch (error) {
1675+
dbg(`GraphQL query failed: %O`, error)
1676+
throw error
1677+
}
1678+
}
16641679
}
16651680

16661681
function parseJobLog(text: string) {

packages/core/src/types/prompt_template.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,6 +3875,13 @@ interface GitHub {
38753875
*/
38763876
resolveAssetUrl(url: string): Promise<string | undefined>
38773877

3878+
/**
3879+
* Executes a GraphQL query against the GitHub API
3880+
* @param query The GraphQL query string
3881+
* @param variables Optional variables for the query
3882+
*/
3883+
graphql<T = any>(query: string, variables?: Record<string, any>): Promise<T>
3884+
38783885
/**
38793886
* Gets the underlying Octokit client
38803887
*/

0 commit comments

Comments
 (0)