{"name":"@octokit/graphql","version":"7.1.1","keywords":["octokit","github","api","graphql"],"author":{"url":"https://github.com/gr2m","name":"Gregor Martynus"},"license":"MIT","_id":"@octokit/graphql@7.1.1","maintainers":[{"name":"gr2m","email":"gregor@martynus.net"},{"name":"octokitbot","email":"security+octokitbot@github.com"},{"name":"kfcampbell","email":"kfcampbell@github.com"},{"name":"nickfloyd","email":"nicholas.floyd.info@gmail.com"}],"homepage":"https://github.com/octokit/graphql.js#readme","bugs":{"url":"https://github.com/octokit/graphql.js/issues"},"dist":{"shasum":"79d9f3d0c96a8fd13d64186fe5c33606d48b79cc","tarball":"http://registry-npm.ywwl.com/@octokit/graphql/download/@octokit/graphql-7.1.1.tgz","fileCount":18,"integrity":"sha512-3mkDltSfcDUoa176nlGoA32RGjeWjl3K7F/BwHwRMJUW/IteSa4bnSV8p2ThNkcIcZU2umkZWxwETSSCJf2Q7g==","signatures":[{"sig":"MEUCIAo7Bz2E2r+Kv8ZKw5iTSN56GxrJHS9+DG5atgGali+SAiEAxnAvWUOjej8Kh6kyqh8dM75q1BajSw2UDnoEDoedq5M=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@octokit%2fgraphql@7.1.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":39556,"size":8778,"noattachment":false,"key":"/@octokit/graphql/-/@octokit/graphql-7.1.1.tgz"},"main":"dist-node/index.js","types":"dist-types/index.d.ts","module":"dist-web/index.js","source":"dist-src/index.js","engines":{"node":">= 18"},"gitHead":"267a17a3edc0a76612a66fc14fbdb92beeb02182","_npmUser":{"name":"octokitbot","email":"security+octokitbot@github.com"},"repository":{"url":"git+https://github.com/octokit/graphql.js.git","type":"git"},"_npmVersion":"10.9.2","description":"GitHub GraphQL API client for browsers and Node","directories":{},"sideEffects":false,"_nodeVersion":"22.13.1","dependencies":{"@octokit/types":"^13.0.0","@octokit/request":"^8.4.1","universal-user-agent":"^6.0.0"},"publishConfig":{"access":"public","provenance":true},"_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"glob":"^10.2.6","jest":"^29.0.0","esbuild":"~0.25.0","ts-jest":"^29.0.0","prettier":"3.0.3","fetch-mock":"^11.1.5","typescript":"^5.0.0","@types/jest":"^29.0.0","@types/node":"^18.0.0","@octokit/tsconfig":"^2.0.0","semantic-release-plugin-update-version-in-files":"^1.0.0"},"_npmOperationalInternal":{"tmp":"tmp/graphql_7.1.1_1740083797279_0.6823458516701719","host":"s3://npm-registry-packages-npm-production"},"readme":"# graphql.js\n\n> GitHub GraphQL API client for browsers and Node\n\n[![@latest](https://img.shields.io/npm/v/@octokit/graphql.svg)](https://www.npmjs.com/package/@octokit/graphql)\n[![Build Status](https://github.com/octokit/graphql.js/workflows/Test/badge.svg)](https://github.com/octokit/graphql.js/actions?query=workflow%3ATest+branch%3Amain)\n\n<!-- toc -->\n\n- [Usage](#usage)\n  - [Send a simple query](#send-a-simple-query)\n  - [Authentication](#authentication)\n  - [Variables](#variables)\n  - [Pass query together with headers and variables](#pass-query-together-with-headers-and-variables)\n  - [Use with GitHub Enterprise](#use-with-github-enterprise)\n  - [Use custom `@octokit/request` instance](#use-custom-octokitrequest-instance)\n- [TypeScript](#typescript)\n  - [Additional Types](#additional-types)\n- [Errors](#errors)\n- [Partial responses](#partial-responses)\n- [Writing tests](#writing-tests)\n- [License](#license)\n\n<!-- tocstop -->\n\n## Usage\n\n<table>\n<tbody valign=top align=left>\n<tr><th>\nBrowsers\n</th><td width=100%>\n\nLoad `@octokit/graphql` directly from [esm.sh](https://esm.sh)\n\n```html\n<script type=\"module\">\n  import { graphql } from \"https://esm.sh/@octokit/graphql\";\n</script>\n```\n\n</td></tr>\n<tr><th>\nNode\n</th><td>\n\nInstall with <code>npm install @octokit/graphql</code>\n\n```js\nimport { graphql } from \"@octokit/graphql\";\n```\n\n</td></tr>\n</tbody>\n</table>\n\n### Send a simple query\n\n```js\nconst { repository } = await graphql(\n  `\n    {\n      repository(owner: \"octokit\", name: \"graphql.js\") {\n        issues(last: 3) {\n          edges {\n            node {\n              title\n            }\n          }\n        }\n      }\n    }\n  `,\n  {\n    headers: {\n      authorization: `token secret123`,\n    },\n  },\n);\n```\n\n### Authentication\n\nThe simplest way to authenticate a request is to set the `Authorization` header, e.g. to a [personal access token](https://github.com/settings/tokens/).\n\n```js\nconst graphqlWithAuth = graphql.defaults({\n  headers: {\n    authorization: `token secret123`,\n  },\n});\nconst { repository } = await graphqlWithAuth(`\n  {\n    repository(owner: \"octokit\", name: \"graphql.js\") {\n      issues(last: 3) {\n        edges {\n          node {\n            title\n          }\n        }\n      }\n    }\n  }\n`);\n```\n\nFor more complex authentication strategies such as GitHub Apps or Basic, we recommend the according authentication library exported by [`@octokit/auth`](https://github.com/octokit/auth.js).\n\n```js\nconst { createAppAuth } = await import(\"@octokit/auth-app\");\nconst auth = createAppAuth({\n  appId: process.env.APP_ID,\n  privateKey: process.env.PRIVATE_KEY,\n  installationId: 123,\n});\nconst graphqlWithAuth = graphql.defaults({\n  request: {\n    hook: auth.hook,\n  },\n});\n\nconst { repository } = await graphqlWithAuth(\n  `{\n    repository(owner: \"octokit\", name: \"graphql.js\") {\n      issues(last: 3) {\n        edges {\n          node {\n            title\n          }\n        }\n      }\n    }\n  }`,\n);\n```\n\n### Variables\n\n⚠️ Do not use [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) in the query strings as they make your code vulnerable to query injection attacks (see [#2](https://github.com/octokit/graphql.js/issues/2)). Use variables instead:\n\n```js\nconst { repository } = await graphql(\n  `\n    query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {\n      repository(owner: $owner, name: $repo) {\n        issues(last: $num) {\n          edges {\n            node {\n              title\n            }\n          }\n        }\n      }\n    }\n  `,\n  {\n    owner: \"octokit\",\n    repo: \"graphql.js\",\n    headers: {\n      authorization: `token secret123`,\n    },\n  },\n);\n```\n\n### Pass query together with headers and variables\n\n```js\nimport { graphql } from(\"@octokit/graphql\");\nconst { repository } = await graphql({\n  query: `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {\n    repository(owner: $owner, name: $repo) {\n      issues(last: $num) {\n        edges {\n          node {\n            title\n          }\n        }\n      }\n    }\n  }`,\n  owner: \"octokit\",\n  repo: \"graphql.js\",\n  headers: {\n    authorization: `token secret123`,\n  },\n});\n```\n\n### Use with GitHub Enterprise\n\n```js\nimport { graphql } from \"@octokit/graphql\";\ngraphql = graphql.defaults({\n  baseUrl: \"https://github-enterprise.acme-inc.com/api\",\n  headers: {\n    authorization: `token secret123`,\n  },\n});\nconst { repository } = await graphql(`\n  {\n    repository(owner: \"acme-project\", name: \"acme-repo\") {\n      issues(last: 3) {\n        edges {\n          node {\n            title\n          }\n        }\n      }\n    }\n  }\n`);\n```\n\n### Use custom `@octokit/request` instance\n\n```js\nimport { request } from \"@octokit/request\";\nimport { withCustomRequest } from \"@octokit/graphql\";\n\nlet requestCounter = 0;\nconst myRequest = request.defaults({\n  headers: {\n    authorization: \"bearer secret123\",\n  },\n  request: {\n    hook(request, options) {\n      requestCounter++;\n      return request(options);\n    },\n  },\n});\nconst myGraphql = withCustomRequest(myRequest);\nawait request(\"/\");\nawait myGraphql(`\n  {\n    repository(owner: \"acme-project\", name: \"acme-repo\") {\n      issues(last: 3) {\n        edges {\n          node {\n            title\n          }\n        }\n      }\n    }\n  }\n`);\n// requestCounter is now 2\n```\n\n## TypeScript\n\n`@octokit/graphql` is exposing proper types for its usage with TypeScript projects.\n\n### Additional Types\n\nAdditionally, `GraphQlQueryResponseData` has been exposed to users:\n\n```ts\nimport type { GraphQlQueryResponseData } from \"@octokit/graphql\";\n```\n\n## Errors\n\nIn case of a GraphQL error, `error.message` is set to a combined message describing all errors returned by the endpoint.\nAll errors can be accessed at `error.errors`. `error.request` has the request options such as query, variables and headers set for easier debugging.\n\n```js\nimport { graphql, GraphqlResponseError } from \"@octokit/graphql\";\ngraphql = graphql.defaults({\n  headers: {\n    authorization: `token secret123`,\n  },\n});\nconst query = `{\n  viewer {\n    bioHtml\n  }\n}`;\n\ntry {\n  const result = await graphql(query);\n} catch (error) {\n  if (error instanceof GraphqlResponseError) {\n    // do something with the error, allowing you to detect a graphql response error,\n    // compared to accidentally catching unrelated errors.\n\n    // server responds with an object like the following (as an example)\n    // class GraphqlResponseError {\n    //  \"headers\": {\n    //    \"status\": \"403\",\n    //  },\n    //  \"data\": null,\n    //  \"errors\": [{\n    //   \"message\": \"Field 'bioHtml' doesn't exist on type 'User'\",\n    //   \"locations\": [{\n    //    \"line\": 3,\n    //    \"column\": 5\n    //   }]\n    //  }]\n    // }\n\n    console.log(\"Request failed:\", error.request); // { query, variables: {}, headers: { authorization: 'token secret123' } }\n    console.log(error.message); // Field 'bioHtml' doesn't exist on type 'User'\n  } else {\n    // handle non-GraphQL error\n  }\n}\n```\n\n## Partial responses\n\nA 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`\n\n```js\nimport { graphql } from \"@octokit/graphql\";\ngraphql = graphql.defaults({\n  headers: {\n    authorization: `token secret123`,\n  },\n});\nconst query = `{\n  repository(name: \"probot\", owner: \"probot\") {\n    name\n    ref(qualifiedName: \"master\") {\n      target {\n        ... on Commit {\n          history(first: 25, after: \"invalid cursor\") {\n            nodes {\n              message\n            }\n          }\n        }\n      }\n    }\n  }\n}`;\n\ntry {\n  const result = await graphql(query);\n} catch (error) {\n  // server responds with\n  // {\n  //   \"data\": {\n  //     \"repository\": {\n  //       \"name\": \"probot\",\n  //       \"ref\": null\n  //     }\n  //   },\n  //   \"errors\": [\n  //     {\n  //       \"type\": \"INVALID_CURSOR_ARGUMENTS\",\n  //       \"path\": [\n  //         \"repository\",\n  //         \"ref\",\n  //         \"target\",\n  //         \"history\"\n  //       ],\n  //       \"locations\": [\n  //         {\n  //           \"line\": 7,\n  //           \"column\": 11\n  //         }\n  //       ],\n  //       \"message\": \"`invalid cursor` does not appear to be a valid cursor.\"\n  //     }\n  //   ]\n  // }\n\n  console.log(\"Request failed:\", error.request); // { query, variables: {}, headers: { authorization: 'token secret123' } }\n  console.log(error.message); // `invalid cursor` does not appear to be a valid cursor.\n  console.log(error.data); // { repository: { name: 'probot', ref: null } }\n}\n```\n\n## Writing tests\n\nYou can pass a replacement for [the built-in fetch implementation](https://github.com/bitinn/node-fetch) as `request.fetch` option. For example, using [fetch-mock](http://www.wheresrhys.co.uk/fetch-mock/) works great to write tests\n\n```js\nimport assert from \"assert\";\nimport fetchMock from \"fetch-mock\";\n\nimport { graphql } from \"@octokit/graphql\";\n\ngraphql(\"{ viewer { login } }\", {\n  headers: {\n    authorization: \"token secret123\",\n  },\n  request: {\n    fetch: fetchMock\n      .sandbox()\n      .post(\"https://api.github.com/graphql\", (url, options) => {\n        assert.strictEqual(options.headers.authorization, \"token secret123\");\n        assert.strictEqual(\n          options.body,\n          '{\"query\":\"{ viewer { login } }\"}',\n          \"Sends correct query\",\n        );\n        return { data: {} };\n      }),\n  },\n});\n```\n\n## License\n\n[MIT](LICENSE)\n","publish_time":1740083797468,"_cnpm_publish_time":1740083797468,"dist-tags":{"beta":"8.0.0-beta.1","latest":"9.0.5","release-7.x":"7.1.1"}}