Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nicksrandall/graphql-query-batcher
A light weight query batcher for graphql
https://github.com/nicksrandall/graphql-query-batcher
graphql graphql-query query-batcher
Last synced: about 1 month ago
JSON representation
A light weight query batcher for graphql
- Host: GitHub
- URL: https://github.com/nicksrandall/graphql-query-batcher
- Owner: nicksrandall
- License: mit
- Created: 2017-01-17T22:22:00.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-02T01:30:23.000Z (about 7 years ago)
- Last Synced: 2024-09-28T21:41:17.373Z (about 2 months ago)
- Topics: graphql, graphql-query, query-batcher
- Language: JavaScript
- Size: 182 KB
- Stars: 9
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Graphql Query Batcher
A light weight (<700 bytes) [graphql](http://graphql.org/) query batcher for javascript.
## Docs
### QueryBatcher
Create a batcher client.
**Parameters**
- `fetcher` **Fetcher** A function that can handle the network requests to graphql endpoint
- `options` **Options** the options to be used by client (optional, default `{}`)
- `options.batchInterval` **integer** duration (in MS) of each batch window. (default 6) (optional, default `6`)
- `options.shouldBatch` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** should the client batch requests. (default true) (optional, default `true`)
- `options.maxBatchSize` **integer** max number of requests in a batch. (default 0) (optional, default `0`)
- `options.defaultHeaders` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** default headers to include with every request**Examples**
```javascript
const fetcher = batchedQuery => fetch('path/to/graphql', {
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(batchedQuery),
credentials: 'include',
})
.then(response => response.json())const client = new QueryBatcher(fetcher, { maxBatchSize: 10 })
```#### fetch
Fetch will send a graphql request and return the parsed json.
**Parameters**
- `query` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the graphql query.
- `variables` **Variables** any variables you wish to inject as key/value pairs.
- `operationName` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** the graphql operationName.
- `overrides` **Options** the client options overrides. (optional, default `{}`)**Examples**
```javascript
client.fetch(`
query getHuman($id: ID!) {
human(id: $id) {
name
height
}
}
`, { id: "1001" }, 'getHuman')
.then(human => {
// do something with human
console.log(human);
});
```Returns **[promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** resolves to parsed json of server response
#### forceFetch
Fetch will send a graphql request and return the parsed json.
**Parameters**
- `query` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the graphql query.
- `variables` **Variables** any variables you wish to inject as key/value pairs.
- `operationName` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** the graphql operationName.
- `overrides` **Options** the client options overrides. (optional, default `{}`)**Examples**
```javascript
client.forceFetch(`
query getHuman($id: ID!) {
human(id: $id) {
name
height
}
}
`, { id: "1001" }, 'getHuman')
.then(human => {
// do something with human
console.log(human);
});
```Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<Result>>** resolves to parsed json of server response
## Default Options
```js
const options = {
shouldBatch: true, // should we batch queries?
batchInterval: 6, // duration of each batch window (in MS)
maxBatchSize: 0, // max number of requests in a batch (0 = no max)
};
```## Requirements
The graphql implementation you are using must suppoprt batching! This means that your graphql endpoint should be able to take an array of requests and return an array of results.
To learn more read this:
> To see an expmaple implementation in Golang, see
## Alternatives
- [Apollo](https://github.com/apollostack/apollo-client)