# @frontendmonster/graphql-utils

> Useful utilities for create graphql server and client

Latest version **4.0.1** (published 2019-09-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @frontendmonster/graphql-utils
pnpm add @frontendmonster/graphql-utils
yarn add @frontendmonster/graphql-utils
bun add @frontendmonster/graphql-utils
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2019-09-29 |
| First published | 2018-09-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | frontendmonster |
| Maintainers | frontendmonster |

## Links

- npm: https://www.npmjs.com/package/@frontendmonster/graphql-utils
- Repository: https://github.com/frontendmonster/graphql-utils
- Homepage: https://github.com/frontendmonster/graphql-utils#readme
- Issues: https://github.com/frontendmonster/graphql-utils/issues
- npm.io page: https://npm.io/package/@frontendmonster/graphql-utils

## Dependencies (2)

- [graphql](https://npm.io/package/graphql.md) *
- [apollo-server-errors](https://npm.io/package/apollo-server-errors.md) *

## Recent versions

- 4.0.1 (latest) — 2019-09-29
- 4.0.0 — 2019-05-28
- 3.1.0 — 2019-02-22
- 3.0.1 — 2019-02-14
- 3.0.0 — 2019-02-13
- 2.0.1 — 2019-02-05
- 2.0.0 — 2019-02-04
- 1.1.7 — 2019-01-18
- 1.1.6 — 2019-01-18
- 1.1.5 — 2018-09-29
- 1.1.4 — 2018-09-27
- 1.1.3 — 2018-09-27
- 1.1.2 — 2018-09-26
- 1.1.1 — 2018-09-26
- 1.1.0 — 2018-09-26
- … 3 more at https://npm.io/package/@frontendmonster/graphql-utils/versions

## README

# @frontendmonster/graphql-utils

## Client Module

### API

The simple way to generate apollo-client with jwt refresh token and authentication helpers.

| api                    | description |
| ---                    | ---         |
|`createApolloClient`    | create zero-config apollo-client with retriable-fetch.|
|`createRetriableFetch`  | create apollo-http-link fetch that refreshes token when jwt token is expired. |

```javascript
import { createApolloClient } from '@frontendmonster/graphql-utils/client';

const apolloClinet = createApolloClient({
    endpoint: 'http://localhost:3000/graphql',
    intialState: {},
    refreshTokenMutation: `graphqlMutationToRefreshToken($userId, $refreshToken) { token, refreshToken } `,
    resolvers: {},
    debug: isDev,
    storage: MyStorage,
    onLogin: (storage, cache, user, accessToken, refreshToken) => console.log(`${user} logged in let's save that on storage and update apollo-cache.`),
    onLogout: (storage, cache) => console.log('user logged out lets reset the apollo-cache and storage'),
  });
};
```

---

## Server Module

### graphql-errors

Extending [apollo-server-errors](https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-errors)

* NotFoundError
* AlreadyExistedError
* ForbiddenError
* AlreadyAuthenticatedError
* UnknownError
* UserInputError
* ForbidenError
* AuthenticationError
* ValidationError

#### Usage

```javascript
import { NotFoundError } from '@frontendmonster/graphql-utils/server';

const resolvers = {
  user: (root, input, context) => {
    // ...
    if(user == null) {
      throw new NotFoundError('user'); // throws NOT_FOUND error with UserNotFound message
    }
    // ...
  }
}
```

### graphql-client

Simple graphql client for testing purpose.
You can inject mongodb models and redis instance.

#### Usage

1. Create a graphql-client instance with schema, mongoose models and redis client.
2. Use `instance.run(query)`;

#### GraphqlClient Instace Api

`run<T>:: (query, [user], [variables]) => Promise<T>`

arguments:

* `query`: graphql query
* `user`: will pass to query context for authorization purpos
* `variables`: query variables

#### Example

```javascript
import { GraphqlClient } from '@frontendmonster/graphql-utils/server';
import { schema, context } from './schema';

const graphqlClient = new GraphqlClient({ schema, context });

const query = `{
  hello
}`;

const result = await graphqlClient.run(query); // result.data.hello > 'world'

```

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