Gathering detailed insights and metrics for @octokit-next/graphql
Gathering detailed insights and metrics for @octokit-next/graphql
Gathering detailed insights and metrics for @octokit-next/graphql
Gathering detailed insights and metrics for @octokit-next/graphql
npm install @octokit-next/graphql
Typescript
Module System
Node Version
NPM Version
73
Supply Chain
98.3
Quality
85.5
Maintenance
100
Vulnerability
99.6
License
JavaScript (79.48%)
TypeScript (20.52%)
Total Downloads
4,814
Last Day
5
Last Week
148
Last Month
339
Last Year
1,488
MIT License
24 Stars
302 Commits
3 Forks
8 Watchers
5 Branches
17 Contributors
Updated on May 20, 2025
Minified
Minified + Gzipped
Latest Version
3.0.0
Package Id
@octokit-next/graphql@3.0.0
Unpacked Size
33.49 kB
Size
7.07 kB
File Count
13
NPM Version
10.9.2
Node Version
22.15.0
Published on
May 20, 2025
Cumulative downloads
Total Downloads
Last Day
25%
5
Compared to previous day
Last Week
155.2%
148
Compared to previous week
Last Month
74.7%
339
Compared to previous month
Last Year
-30.1%
1,488
Compared to previous year
GitHub GraphQL API client for browsers and Node
Browsers |
Load
|
---|---|
Node |
Install with
|
Deno |
Load
|
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);
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);
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);
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});
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`);
@octokit-next/request
instance1const { 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
@octokit-next/graphql
is exposing proper types for its usage with TypeScript projects.
Additionally, GraphQlQueryResponseData
has been exposed to users:
1import type { GraphQlQueryResponseData } from "@octokit-next/graphql";
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}
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}
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});
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
15 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
SAST tool is run on all commits
Details
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
Found 1/28 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Score
Last Scanned on 2025-05-12
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