Installations
npm install @octokit-next/graphql
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
20.11.1
NPM Version
10.5.1
Score
73.5
Supply Chain
91.5
Quality
78.8
Maintenance
100
Vulnerability
99.6
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (79.48%)
TypeScript (20.52%)
Developer
octokit
Download Statistics
Total Downloads
4,076
Last Day
3
Last Week
16
Last Month
85
Last Year
1,675
GitHub Statistics
22 Stars
284 Commits
3 Forks
8 Watching
3 Branches
14 Contributors
Bundle Size
9.03 kB
Minified
3.63 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.8.0
Package Id
@octokit-next/graphql@2.8.0
Unpacked Size
33.49 kB
Size
7.08 kB
File Count
13
NPM Version
10.5.1
Node Version
20.11.1
Publised On
03 Apr 2024
Total Downloads
Cumulative downloads
Total Downloads
4,076
Last day
0%
3
Compared to previous day
Last week
-44.8%
16
Compared to previous week
Last month
49.1%
85
Compared to previous month
Last year
7.2%
1,675
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
graphql.js
GitHub GraphQL API client for browsers and Node
Usage
Browsers |
Load
|
---|---|
Node |
Install with
|
Deno |
Load
|
Send a simple query
1const { repository } = await graphql( 2 ` 3 { 4 repository(owner: "octokit", name: "graphql.js") { 5 issues(last: 3) { 6 nodes { 7 title 8 } 9 } 10 } 11 } 12 `, 13 { 14 headers: { 15 authorization: `token secret123`, 16 }, 17 } 18);
Authentication
The simplest way to authenticate a request is to set the Authorization
header, e.g. to a personal access token.
1const graphqlWithAuth = graphql.defaults({ 2 headers: { 3 authorization: `token secret123`, 4 }, 5}); 6const { repository } = await graphqlWithAuth(` 7 { 8 repository(owner: "octokit", name: "graphql.js") { 9 issues(last: 3) { 10 nodes { 11 title 12 } 13 } 14 } 15 } 16`);
For more complex authentication strategies such as GitHub Apps, we recommend to use an authentication strategy plugin.
1const { createAppAuth } = require("@octokit-next/auth-app"); 2const auth = createAppAuth({ 3 appId: process.env.APP_ID, 4 privateKey: process.env.PRIVATE_KEY, 5 installationId: 123, 6}); 7const graphqlWithAuth = graphql.defaults({ 8 request: { 9 hook: auth.hook, 10 }, 11}); 12 13const { repository } = await graphqlWithAuth( 14 `{ 15 repository(owner: "octokit", name: "graphql.js") { 16 issues(last: 3) { 17 nodes { 18 title 19 } 20 } 21 } 22 }` 23);
Variables
Warning Do not use template literals in the query strings as they make your code vulnerable to query injection attacks (see #2). Use variables instead:
1const { repository } = await graphql( 2 ` 3 query lastIssues($owner: String!, $repo: String!, $num: Int = 3) { 4 repository(owner: $owner, name: $repo) { 5 issues(last: $num) { 6 nodes { 7 title 8 } 9 } 10 } 11 } 12 `, 13 { 14 owner: "octokit", 15 repo: "graphql.js", 16 headers: { 17 authorization: `token secret123`, 18 }, 19 } 20);
Pass query together with headers and variables
1const { graphql } = require("@octokit-next/graphql"); 2const { repository } = await graphql({ 3 query: `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) { 4 repository(owner:$owner, name:$repo) { 5 issues(last:$num) { 6 nodes { 7 title 8 } 9 } 10 } 11 }`, 12 owner: "octokit", 13 repo: "graphql.js", 14 headers: { 15 authorization: `token secret123`, 16 }, 17});
Use with GitHub Enterprise
1let { graphql } = require("@octokit-next/graphql"); 2graphql = graphql.defaults({ 3 baseUrl: "https://github-enterprise.acme-inc.com/api", 4 headers: { 5 authorization: `token secret123`, 6 }, 7}); 8const { repository } = await graphql(` 9 { 10 repository(owner: "acme-project", name: "acme-repo") { 11 issues(last: 3) { 12 nodes { 13 title 14 } 15 } 16 } 17 } 18`);
Use custom @octokit-next/request
instance
1const { request } = require("@octokit-next/request"); 2const { withCustomRequest } = require("@octokit-next/graphql"); 3 4let requestCounter = 0; 5const myRequest = request.defaults({ 6 headers: { 7 authentication: "token secret123", 8 }, 9 request: { 10 hook(request, options) { 11 requestCounter++; 12 return request(options); 13 }, 14 }, 15}); 16const myGraphql = withCustomRequest(myRequest); 17await request("/"); 18await myGraphql(` 19 { 20 repository(owner: "acme-project", name: "acme-repo") { 21 issues(last: 3) { 22 nodes { 23 title 24 } 25 } 26 } 27 } 28`); 29// requestCounter is now 2
TypeScript
@octokit-next/graphql
is exposing proper types for its usage with TypeScript projects.
Additional Types
Additionally, GraphQlQueryResponseData
has been exposed to users:
1import type { GraphQlQueryResponseData } from "@octokit-next/graphql";
Errors
In case of a GraphQL error, error.message
is set to a combined message describing all errors returned by the endpoint.
All errors can be accessed at error.errors
. error.request
has the request options such as query, variables and headers set for easier debugging.
1let { graphql, GraphqlResponseError } = require("@octokit-next/graphql"); 2graphql = graphql.defaults({ 3 headers: { 4 authorization: `token secret123`, 5 }, 6}); 7const query = `{ 8 viewer { 9 bioHtml 10 } 11}`; 12 13try { 14 const result = await graphql(query); 15} catch (error) { 16 if (error instanceof GraphqlResponseError) { 17 // do something with the error, allowing you to detect a graphql response error, 18 // compared to accidentally catching unrelated errors. 19 20 // server responds with an object like the following (as an example) 21 // class GraphqlResponseError { 22 // "headers": { 23 // "status": "403", 24 // }, 25 // "data": null, 26 // "errors": [{ 27 // "message": "Field 'bioHtml' doesn't exist on type 'User'", 28 // "locations": [{ 29 // "line": 3, 30 // "column": 5 31 // }] 32 // }] 33 // } 34 35 console.log("Request failed:", error.request); // { query, variables: {}, headers: { authorization: 'token secret123' } } 36 console.log(error.message); // Field 'bioHtml' doesn't exist on type 'User' 37 } else { 38 // handle non-GraphQL error 39 } 40}
Partial responses
A GraphQL query may respond with partial data accompanied by errors. In this case we will throw an error but the partial data will still be accessible through error.data
1let { graphql } = require("@octokit-next/graphql"); 2graphql = graphql.defaults({ 3 headers: { 4 authorization: `token secret123`, 5 }, 6}); 7const query = `{ 8 repository(name: "probot", owner: "probot") { 9 name 10 ref(qualifiedName: "master") { 11 target { 12 ... on Commit { 13 history(first: 25, after: "invalid cursor") { 14 nodes { 15 message 16 } 17 } 18 } 19 } 20 } 21 } 22}`; 23 24try { 25 const result = await graphql(query); 26} catch (error) { 27 // server responds with 28 // { 29 // "data": { 30 // "repository": { 31 // "name": "probot", 32 // "ref": null 33 // } 34 // }, 35 // "errors": [ 36 // { 37 // "type": "INVALID_CURSOR_ARGUMENTS", 38 // "path": [ 39 // "repository", 40 // "ref", 41 // "target", 42 // "history" 43 // ], 44 // "locations": [ 45 // { 46 // "line": 7, 47 // "column": 11 48 // } 49 // ], 50 // "message": "`invalid cursor` does not appear to be a valid cursor." 51 // } 52 // ] 53 // } 54 55 console.log("Request failed:", error.request); // { query, variables: {}, headers: { authorization: 'token secret123' } } 56 console.log(error.message); // `invalid cursor` does not appear to be a valid cursor. 57 console.log(error.data); // { repository: { name: 'probot', ref: null } } 58}
Writing tests
You can pass a replacement for the built-in fetch implementation as request.fetch
option. For example, using fetch-mock works great to write tests
1const assert = require("assert"); 2const fetchMock = require("fetch-mock/es5/server"); 3 4const { graphql } = require("@octokit-next/graphql"); 5 6graphql("{ viewer { login } }", { 7 headers: { 8 authorization: "token secret123", 9 }, 10 request: { 11 fetch: fetchMock 12 .sandbox() 13 .post("https://api.github.com/graphql", (url, options) => { 14 assert.strictEqual(options.headers.authorization, "token secret123"); 15 assert.strictEqual( 16 options.body, 17 '{"query":"{ viewer { login } }"}', 18 "Sends correct query" 19 ); 20 return { data: {} }; 21 }), 22 }, 23});
License
No vulnerabilities found.
Reason
13 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
packaging workflow detected
Details
- Info: Project packages its releases by way of GitHub Actions.: .github/workflows/release.yml:14
Reason
SAST tool is run on all commits
Details
- Info: SAST configuration detected: CodeQL
- Info: all commits (30) are checked with a SAST tool
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Warn: One or no descriptive hints of disclosure, vulnerability, and/or timelines in security policy
- Info: Found text in security policy: SECURITY.md:1
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/add_to_octokit_project.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/add_to_octokit_project.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:41: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:45: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:59: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:72: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/codeql-analysis.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/immediate-response.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/immediate-response.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/openapi-update.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/openapi-update.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/openapi-update.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/openapi-update.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/openapi-update.yml:36: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/openapi-update.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/openapi-update.yml:66: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/openapi-update.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/openapi-update.yml:89: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/openapi-update.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/release.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/release.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/test.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/test.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/test.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/octokit/octokit-next.js/test.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/release.yml:24
- Info: 0 out of 12 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 5 third-party GitHubAction dependencies pinned
- Info: 2 out of 3 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/codeql-analysis.yml:28
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/codeql-analysis.yml:29
- Warn: no topLevel permission defined: .github/workflows/add_to_octokit_project.yml:1
- Warn: no topLevel permission defined: .github/workflows/codeql-analysis.yml:1
- Warn: no topLevel permission defined: .github/workflows/openapi-update.yml:1
- Warn: topLevel 'contents' permission set to 'write': .github/workflows/release.yml:8
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Score
6.5
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More