Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add Readme section for batching #294

Merged
merged 2 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Minimal GraphQL client supporting Node and browsers for scripts or simple apps
- [File Upload](#file-upload)
- [Browser](#browser)
- [Node](#node)
- [Batching](#batching)
- [FAQ](#faq)
- [Why do I have to install `graphql`?](#why-do-i-have-to-install-graphql)
- [Do I need to wrap my GraphQL documents inside the `gql` template exported by `graphql-request`?](#do-i-need-to-wrap-my-graphql-documents-inside-the-gql-template-exported-by-graphql-request)
Expand Down Expand Up @@ -502,6 +503,43 @@ request('/api/graphql', UploadUserAvatar, {

[TypeScript Source](examples/receiving-a-raw-response.ts)


### Batching

It is possible with `graphql-request` to use [batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md) via the `batchRequests()` function. Example available at [examples/batching-requests.ts](examples/batching-requests.ts)

```ts
import { batchRequests } from 'graphql-request';

(async function () {
const endpoint = 'https://api.spacex.land/graphql/';

const query1 = /* GraphQL */ `
query ($id: ID!) {
capsule(id: $id) {
id
landings
}
}
`;

const query2 = /* GraphQL */ `
{
rockets(limit: 10) {
active
}
}
`;

const data = await batchRequests(endpoint, [
{ document: query1, variables: { id: 'C105' } },
{ document: query2 },
])
console.log(JSON.stringify(data, undefined, 2))
})().catch((error) => console.error(error))
```


## FAQ

#### Why do I have to install `graphql`?
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export async function request<T = any, V = Variables>(
* ```ts
* // You can pass a raw string
*
* await request('https://foo.bar/graphql', [
* await batchRequests('https://foo.bar/graphql', [
* {
* query: `
* {
Expand All @@ -417,7 +417,7 @@ export async function request<T = any, V = Variables>(
*
* import gql from 'graphql-tag'
*
* await request('https://foo.bar/graphql', [{ query: gql`...` }])
* await batchRequests('https://foo.bar/graphql', [{ query: gql`...` }])
* ```
*/
export async function batchRequests<T extends any = any, V = Variables>(
Expand Down