# gql-fetch

> The GraphQL client powered by fetch. Supports multiform and upload files

Latest version **0.3.9** (published 2019-05-08) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install gql-fetch
pnpm add gql-fetch
yarn add gql-fetch
bun add gql-fetch
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.3.9 |
| Published | 2019-05-08 |
| First published | 2019-02-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 6.0 |
| Dependencies | 7 |
| Unpacked size | 30.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Panates |
| Maintainers | erayhanoglu |
| Keywords | javascript, graphql, fetch, client |

## Links

- npm: https://www.npmjs.com/package/gql-fetch
- Repository: https://github.com/gqlorg/gql-fetch
- Homepage: https://github.com/gqlorg/gql-fetch#readme
- Issues: https://github.com/gqlorg/gql-fetch/issues
- npm.io page: https://npm.io/package/gql-fetch

## Dependencies (7)

- [path](https://npm.io/package/path.md) ^0.12.7
- [rxjs](https://npm.io/package/rxjs.md) ^6.5.1
- [form-data](https://npm.io/package/form-data.md) ^2.3.3
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.5.0
- [http-errors](https://npm.io/package/http-errors.md) ^1.7.2
- [whatwg-fetch](https://npm.io/package/whatwg-fetch.md) >=0.10.0
- [abortcontroller-polyfill](https://npm.io/package/abortcontroller-polyfill.md) ^1.3.0

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.3.9 (latest) — 2019-05-08
- 0.3.8 — 2019-04-19
- 0.3.7 — 2019-04-19
- 0.3.6 — 2019-04-19
- 0.3.5 — 2019-04-19
- 0.3.4 — 2019-03-19
- 0.3.3 — 2019-03-19
- 0.3.2 — 2019-03-19
- 0.3.1 — 2019-03-19
- 0.2.3 — 2019-03-12
- 0.2.2 — 2019-03-12
- 0.2.1 — 2019-02-22
- 0.2.0 — 2019-02-21
- 0.1.6 — 2019-02-15
- 0.1.5 — 2019-02-15
- … 8 more at https://npm.io/package/gql-fetch/versions

## README

# gql-fetch

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]
[![Dependencies][dependencies-image]][dependencies-url]
[![DevDependencies][devdependencies-image]][devdependencies-url]

[![NPM](https://nodei.co/npm/gql-fetch.png?downloads=true&downloadRank=true)](https://nodei.co/npm/gql-fetch/)

## gql-fetch

The purpose of creating this library is to gather special usage standards in a 
client library and to facilitate development process about client-server communication.

Thanks to its simple and easy use, this library provides fast and clean code development.
In the background, a new generation request method as known as fetch is used.
This library solves this problem by polyfilling for browsers that don't have fetch support.

## Outstanding

* Thanks to it is designed for Graphql server,  it provides fast and clean code development.
* File upload support.
* Requests can be aborted.
* It is light and it has polyfill support.
* Rxjs Observable support.

## Installation

```sh
$ npm install gql-fetch
```

## Usage

gql-fetch has two usage forms:

- **[`GQLClient`](#GQLClient)**: class
- **[`getFetch`](#getFetch)**: function

---
### GQLClient
The GQLClient class takes two arguments when it is being created.
- **url**: RequestInfo (required)
- **options**: [`IClientOptions`](#IClientOptions) (optional)

```ts
const client = new GQLClient('http://localhost:5000/graphql');
```

Class Properties:
- **url**: RequestInfo
- **headers**: object
- **fetch(query: string, variables?: IQueryVariables, options?: [`IFetchOptions`](#IFetchOptions))**: [`GQLRequest`](#GQLRequest)
- **fetchObservable(query: string, variables?: IQueryVariables, options?: [`IFetchOptions`](#IFetchOptions))**: Observable[`<GQLResponse>`](#GQLResponse)

Sample Usage
```ts
const client = new GQLClient('http://localhost:5000/graphql');
const request = client.fetch(`query ($id: Int!) {
                                user(id: $id) {
                                  id
                                  name
                                }
                              }`,{ id: 1 });
request.then(...);                  
```

In order to cancel a request created with GQLClient;
```ts
const client = new GQLClient('http://localhost:5000/graphql');
const request = client.fetch(`query ($id: Int!) {
                                user(id: $id) {
                                  id
                                  name
                                }
                              }`,{ id: 1 });
request.abort();                  
```

fetchObservable Sample 
```ts
const client = new GQLClient('http://localhost:5000/graphql');
const fo = client.fetchObservable(`query ($id: Int!) {
                                user(id: $id) {
                                  id
                                  name
                                }
                              }`,{ id: 1 });                              
fo.subscribe(...);    
```
To upload file via GQLClient;
```ts
const client = new GQLClient('http://localhost:5000/graphql');
const request = client.fetch(`mutation ($file: File!) {
                                uploadFile(file: $file)           
                             }`, { file: fs.createReadStream(__dirname + '/file.txt')});
request.then(...);
```

---
### getFetch

The getFetch function has two arguments.
- **url**: RequestInfo (required)
- **options**: [`IClientOptions`](#IClientOptions) (optional)

This function returns the function.

Returned function arguments;
- **query**: string (required)
- **variables**:  object (optional)
- **fetchOptions**:  [`IFetchOptions`](#IFetchOptions) (optional)

This function returns [`GQLRequest`](#GQLRequest) class. 
 
Sample Usage:
```ts

const gqlFetch = getFetch('http://localhost:5000/graphql');
const request = gqlFetch(`{                                   
                    users {
                      id 
                      name
                    }                      
                  }`);              
request.then(...);
```

---
#### GQLRequest

This class is created to manage the requests.

Class Properties:

| Property          | Type          | Argument                                                |
| ----------------- |---------------| ----------------------------------------------------- |
| response          | Promise       |  -                                                      |
| aborted           | boolean       |  -                                                      |
| abort             | void          |  -                                                      |
| on                | this          |  event: string, listener: (...args: any[]) => void  |
| then              | Promise       |  onfulfilled: IPromiseResolved< [`GQLResponse`](#GQLResponse) >, onrejected: IPromiseRejected |
| catch             | Promise       |  onrejected                                             |
| finally           | Promise       |  onfinally                                              |

---
#### GQLResponse

Class Properties:

| Property          | Type          | Argument                 |
| ----------------- |---------------| ------------------------ |
| json              | any           |  -                       |
| status            | number        |  -                       |
| statusText        | string        |  -                       |
| ok                | boolean       |  -                       |
| url               | string        |  -                       |
| headers           | Headers       |  -                       |

---
#### IClientOptions

Interface Properties:

| Property          | Type          | Argument                 |
| ----------------- |---------------| ------------------------ |
| headers           | object        |  -                       |

---
#### IFetchOptions

Interface Properties:

| Property          | Type          | Argument                 |
| ----------------- |---------------| ------------------------ |
| headers           | object        |  -                       |
| operationName     | string        |  -                       |
| redirect          | RedirectType  |  -                       |
| follow            | number        |  -                       |
| timeout           | number        |  -                       |
| compress          | boolean       |  -                       |
| size              | number        |  -                       |
| agent             | Http(s)Agent  |  -                       |

## Node Compatibility

  - node `>= 6.x`;
  
### License
[MIT](LICENSE)

[npm-image]: https://img.shields.io/npm/v/gql-fetch.svg
[npm-url]: https://npmjs.org/package/gql-fetch
[travis-image]: https://img.shields.io/travis/gqlorg/gql-fetch/master.svg
[travis-url]: https://travis-ci.org/gqlorg/gql-fetch
[coveralls-image]: https://img.shields.io/coveralls/gqlorg/gql-fetch/master.svg
[coveralls-url]: https://coveralls.io/r/gqlorg/gql-fetch
[downloads-image]: https://img.shields.io/npm/dm/gql-fetch.svg
[downloads-url]: https://npmjs.org/package/gql-fetch
[gitter-image]: https://badges.gitter.im/gqlorg/gql-fetch.svg
[gitter-url]: https://gitter.im/gqlorg/gql-fetch?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
[dependencies-image]: https://david-dm.org/gqlorg/gql-fetch/status.svg
[dependencies-url]:https://david-dm.org/gqlorg/gql-fetch
[devdependencies-image]: https://david-dm.org/gqlorg/gql-fetch/dev-status.svg
[devdependencies-url]:https://david-dm.org/gqlorg/gql-fetch?type=dev
[quality-image]: http://npm.packagequality.com/shield/gql-fetch.png
[quality-url]: http://packagequality.com/#?package=gql-fetch

---
_Source: https://npm.io/package/gql-fetch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
