Skip to content

Commit

Permalink
Replace graphql-request with fetch (#15)
Browse files Browse the repository at this point in the history
graphql-request is big package and depends on graphql. Practically it
does not do anything here. Only send request and handle errors.

https://packagephobia.com/result?p=graphql-request (756kB)
https://packagephobia.com/result?p=graphql (1.28MB)

Here I replaced it with fetch from undici. Happily codegen have an
option to generate generic sdk with own requester function.

In the future undici may even be replaced with builtin into node
version.
  • Loading branch information
TrySound committed Jun 6, 2023
1 parent 9ccfdfe commit 0f4cad5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 38 deletions.
2 changes: 1 addition & 1 deletion codegen.yml
Expand Up @@ -15,4 +15,4 @@ generates:
plugins:
- typescript
- typescript-operations
- typescript-graphql-request
- typescript-generic-sdk
8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -33,18 +33,18 @@
"dependencies": {
"classnames": "^2.3.1",
"code-tag": "^1.1.0",
"graphql": "^15.5.0",
"graphql-request": "^6.1.0",
"gray-matter": "^4.0.2"
"gray-matter": "^4.0.2",
"undici": "^5.22.1"
},
"devDependencies": {
"@graphql-codegen/cli": "^4.0.1",
"@graphql-codegen/typescript": "^4.0.0",
"@graphql-codegen/typescript-graphql-request": "^5.0.0",
"@graphql-codegen/typescript-generic-sdk": "^3.1.0",
"@graphql-codegen/typescript-operations": "^4.0.0",
"@types/jest": "^29.5.2",
"dotenv-flow": "^3.2.0",
"eslint": "^7.23.0",
"graphql": "^15.5.0",
"jest": "^29.5.0",
"nano-staged": "^0.8.0",
"prettier": "^2.2.1",
Expand Down
31 changes: 12 additions & 19 deletions src/__generated__/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/core/sdk.ts
@@ -1 +1,2 @@
export { getSdk } from "../__generated__";
export type { Requester } from "../__generated__";
32 changes: 23 additions & 9 deletions src/github-blog.ts
@@ -1,8 +1,8 @@
import { GraphQLClient } from "graphql-request";
import { fetch } from "undici";
import { GithubQueryParams, githubQueryBuilder } from "./utils/github-query";
import { PagerParams, buildPager } from "./utils/pager";

import { getSdk } from "./core/sdk";
import { getSdk, Requester } from "./core/sdk";

import { getPosts } from "./methods/getPosts";
import { getPinnedPosts } from "./methods/getPinnedPosts";
Expand All @@ -13,24 +13,38 @@ import { getLabels } from "./methods/getLabels";
export type GithubBlogParams = {
token: string;
repo: string;
fetch?: typeof fetch;
queryDefaults?: Partial<GithubQueryParams>;
paginationDefaults?: Partial<PagerParams>;
};
export class GithubBlog {
client: GraphQLClient;
sdk: ReturnType<typeof getSdk>;
repo: string;
buildQuery: (args?: GithubQueryParams) => ReturnType<ReturnType<typeof githubQueryBuilder>>;
buildPager: (args?: PagerParams) => ReturnType<typeof buildPager>;

constructor(params: GithubBlogParams) {
this.repo = params.repo;
this.client = new GraphQLClient("https://api.github.com/graphql", {
headers: { Authorization: `Bearer ${params.token}` },
fetch: params.fetch ?? undefined,
});
this.sdk = getSdk(this.client);
const request: Requester<void, void> = async (query: string, variables: unknown) => {
const body = JSON.stringify({
query,
variables,
});
const response = await fetch("https://api.github.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${params.token}`,
},
body,
});
const result = (await response.json()) as { data: any; errors: any };
if (result.data) {
return result.data;
}
const status = `${response.status} ${response.statusText}`;
throw Error(`${status}\n${body}\n${JSON.stringify(result)}`);
};
this.sdk = getSdk(request);
const buildQuery = githubQueryBuilder(this.repo);
this.buildQuery = (args) => buildQuery(args, params.queryDefaults);
this.buildPager = (args) => buildPager(args, params.paginationDefaults);
Expand Down
17 changes: 12 additions & 5 deletions yarn.lock
Expand Up @@ -945,10 +945,10 @@
"@graphql-tools/utils" "^10.0.0"
tslib "~2.5.0"

"@graphql-codegen/typescript-graphql-request@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@graphql-codegen/typescript-graphql-request/-/typescript-graphql-request-5.0.0.tgz#1b118753c8cc795d0a43baae74c6eb4a5f5e6844"
integrity sha512-BM9UzZD/5q3b3Q4c+VOy/QoPWgsnfs1GAnGJjh9xtuty6YsCprwhh12BaV16F+xndNUd6qkghigGyjR8m8RdkA==
"@graphql-codegen/typescript-generic-sdk@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@graphql-codegen/typescript-generic-sdk/-/typescript-generic-sdk-3.1.0.tgz#6380c19dd0fafc160cf80b859b50013217973499"
integrity sha512-nQZi/YGRI1+qCZZsh0V5nz6+hCHSN4OU9tKyOTDsEPyDFnGEukDuRdCH2IZasGn22a3Iu5TUDkgp5w9wEQwGmg==
dependencies:
"@graphql-codegen/plugin-helpers" "^3.0.0"
"@graphql-codegen/visitor-plugin-common" "2.13.1"
Expand Down Expand Up @@ -3050,7 +3050,7 @@ graphql-config@^5.0.2:
string-env-interpolation "^1.0.1"
tslib "^2.4.0"

graphql-request@^6.0.0, graphql-request@^6.1.0:
graphql-request@^6.0.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/graphql-request/-/graphql-request-6.1.0.tgz#f4eb2107967af3c7a5907eb3131c671eac89be4f"
integrity sha512-p+XPfS4q7aIpKVcgmnZKhMNqhltk20hfXtkaIkTfjjmiKMJ5xrt5c743cL03y/K7y1rg3WrIC49xGiEQ4mxdNw==
Expand Down Expand Up @@ -5088,6 +5088,13 @@ unc-path-regex@^0.1.2:
resolved "https://registry.yarnpkg.com/unc-path-regex/-/unc-path-regex-0.1.2.tgz#e73dd3d7b0d7c5ed86fbac6b0ae7d8c6a69d50fa"
integrity sha1-5z3T17DXxe2G+6xrCufYxqadUPo=

undici@^5.22.1:
version "5.22.1"
resolved "https://registry.yarnpkg.com/undici/-/undici-5.22.1.tgz#877d512effef2ac8be65e695f3586922e1a57d7b"
integrity sha512-Ji2IJhFXZY0x/0tVBXeQwgPlLWw13GVzpsWPQ3rV50IFMMof2I55PZZxtm4P6iNq+L5znYN9nSTAq0ZyE6lSJw==
dependencies:
busboy "^1.6.0"

unixify@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unixify/-/unixify-1.0.0.tgz#3a641c8c2ffbce4da683a5c70f03a462940c2090"
Expand Down

0 comments on commit 0f4cad5

Please sign in to comment.