https://github.com/labd/node-graphql-fetcher
node-graphql-fetcher
https://github.com/labd/node-graphql-fetcher
Last synced: 3 months ago
JSON representation
node-graphql-fetcher
- Host: GitHub
- URL: https://github.com/labd/node-graphql-fetcher
- Owner: labd
- Created: 2023-11-09T16:33:11.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-22T13:53:32.000Z (about 2 years ago)
- Last Synced: 2024-05-01T09:49:12.369Z (about 2 years ago)
- Language: TypeScript
- Homepage:
- Size: 158 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
Opinionated `fetch` wrappers for our client and server side queries in our Next.js frontends.
Only used for fetching from GraphQL endpoints.
## Features
- GraphQL support using `TypedDocumentString` as the query
- Persisted queries support using either pregenerated hashes or on the fly hashing
- Fallback when persisted query fails
- Client fetcher with React context support when the endpoint is only known at runtime
- Next data cache support
## Usage
```ts
import { initClientFetcher } from "@labdigital/graphql-fetcher";
const fetcher = initClientFetcher("https://localhost/graphql");
const gqlResponse = await fetcher(query, {
myVar: "baz",
}, {
signal: AbortSignal.timeout(10),
headers: {
"X-extra-header": "foo",
}
});
```
## Notes
### Node 18.x requires webcrypto on globalThis
From node 20.x onwards the WebCrypto API is available on globalThis, versions before 20.x will need a small polyfill:
```
if (typeof window === "undefined" && !globalThis.crypto) {
globalThis.crypto = require("node:crypto").webcrypto;
}
```
### Old browsers might need a AbortSignal.timeout() polyfill
Old browsers might not have AbortSignal.timeout() available. We do not support these versions but you can add a polyfill using the following code:
```typescript
// Polyfill for AbortSignal.timeout() for older browsers
if (typeof AbortSignal !== "undefined" && !AbortSignal.timeout) {
AbortSignal.timeout = function timeout(ms: number) {
const controller = new AbortController();
setTimeout(() => controller.abort(), ms);
return controller.signal;
};
}
export {};
```