Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@octokit/graphql
Advanced tools
The @octokit/graphql npm package is designed to simplify making GraphQL queries to the GitHub API. It provides a straightforward way to execute queries and mutations, handle authentication, and manage GraphQL variables. This package is part of the Octokit suite, which is officially maintained by GitHub, ensuring high compatibility and reliability for developers interacting with GitHub's data.
Executing a GraphQL query
This feature allows you to execute a basic GraphQL query to fetch the login name of the authenticated GitHub user. You need to replace 'YOUR_GITHUB_TOKEN' with your actual GitHub token.
{
"query": "query { viewer { login }}",
"headers": {
"authorization": "token YOUR_GITHUB_TOKEN"
}
}
Executing a GraphQL mutation
This feature demonstrates how to execute a mutation to create a new issue in a repository. You need to replace 'REPOSITORY_ID' and 'YOUR_GITHUB_TOKEN' with your repository's ID and your GitHub token, respectively.
{
"mutation": "mutation ($repositoryId: ID!, $issueTitle: String!) { createIssue(input: {repositoryId: $repositoryId, title: $issueTitle}) { issue { id } } }",
"variables": {
"repositoryId": "REPOSITORY_ID",
"issueTitle": "New Issue Title"
},
"headers": {
"authorization": "token YOUR_GITHUB_TOKEN"
}
}
graphql-request is a minimal GraphQL client for making queries and mutations. It's simpler and has fewer features compared to @octokit/graphql, lacking built-in GitHub authentication mechanisms, but it's versatile for any GraphQL API, not just GitHub's.
Apollo Client is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. It's more feature-rich than @octokit/graphql, offering advanced features like caching and optimistic UI updates, making it suitable for complex applications.
GitHub GraphQL API client for browsers and Node
Send a simple query
const graphql = require('@octokit/graphql')
const { repository } = await graphql(`{
repository(owner:"octokit", name:"graphql.js") {
issues(last:3) {
edges {
node {
title
}
}
}
}
}`, {
headers: {
authorization: `token secret123`
}
})
⚠️ Do not use template literals in the query strings as they make your code vulnerable to query injection attacks (see #2). Use variables instead:
const graphql = require('@octokit/graphql')
const { lastIssues } = await graphql(`query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
repository(owner:$owner, name:$repo) {
issues(last:$num) {
edges {
node {
title
}
}
}
}
}`, {
owner: 'octokit',
repo: 'graphql.js'
headers: {
authorization: `token secret123`
}
}
})
Set default options
const graphql = require('@octokit/graphql').defaults({
headers: {
authorization: `token secret123`
}
})
const { repository } = await graphql(`{
repository(owner:"octokit", name:"graphql.js") {
issues(last:3) {
edges {
node {
title
}
}
}
}
}`)
Pass query together with headers and variables
const graphql = require('@octokit/graphql')
const { lastIssues } = await graphql({
query: `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
repository(owner:$owner, name:$repo) {
issues(last:$num) {
edges {
node {
title
}
}
}
}
}`,
owner: 'octokit',
repo: 'graphql.js'
headers: {
authorization: `token secret123`
}
})
Use with GitHub Enterprise
const graphql = require('@octokit/graphql').defaults({
baseUrl: 'https://github-enterprise.acme-inc.com/api',
headers: {
authorization: `token secret123`
}
})
const { repository } = await graphql(`{
repository(owner:"acme-project", name:"acme-repo") {
issues(last:3) {
edges {
node {
title
}
}
}
}
}`)
In case of a GraphQL error, error.message
is set to the first error from the response’s errors
array. All errors can be accessed at error.errors
. error.request
has the request options such as query, variables and headers set for easier debugging.
const graphql = require('@octokit/graphql').defaults({
headers: {
authorization: `token secret123`
}
})
const query = `{
viewer {
bioHtml
}
}`
try {
const result = await graphql(query)
} catch (error) {
// server responds with
// {
// "data": null,
// "errors": [{
// "message": "Field 'bioHtml' doesn't exist on type 'User'",
// "locations": [{
// "line": 3,
// "column": 5
// }]
// }]
// }
console.log('Request failed:', error.request) // { query, variables: {}, headers: { authorization: 'token secret123' } }
console.log(error.message) // Field 'bioHtml' doesn't exist on type 'User'
}
FAQs
GitHub GraphQL API client for browsers and Node
The npm package @octokit/graphql receives a total of 0 weekly downloads. As such, @octokit/graphql popularity was classified as not popular.
We found that @octokit/graphql demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.