# aido-graphql

> A simple GraphQL client for your Aido applications

Latest version **1.0.2** (published 2020-04-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install aido-graphql
pnpm add aido-graphql
yarn add aido-graphql
bun add aido-graphql
```

## 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 | 1.0.2 |
| Published | 2020-04-16 |
| First published | 2019-11-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Damien BUTY |
| Maintainers | dam-buty |

## Links

- npm: https://www.npmjs.com/package/aido-graphql
- Repository: https://github.com/aidojs/aido-graphql
- Issues: https://github.com/aidojs/aido-graphql/issues
- npm.io page: https://npm.io/package/aido-graphql

## Dependencies (1)

- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.0

## Recent versions

- 1.0.2 (latest) — 2020-04-16
- 1.0.1 — 2019-11-01
- 1.0.0 — 2019-11-01

## README

# aido-graphql

A simple GraphQL client for your Aido applications.

## Installation

The aido-graphql package can be installed with your package manager of choice :

```sh
npm install --save aido-graphql
# or
yarn add aido-graphql
```

To use it in your Aido application, you'll need to import it as a plugin :

```javascript
const aidoGraphQL = require('aido-graphql')

aido.init({
  plugins: [aidoGraphQL],
})
```

## Setup

You can pass a configuration to aido-graphql when you initialize your Aido application :

```javascript
aido.init({
  // ...
  graphQL: {
    apiURL: 'https://your-api/graphql',
    defaultHeaders: {
      'X-special-auth': 'XXXXXXXXX',
    },
    errorManager: (res) => {
      console.log(`GraphQL error : ${res.error}`)
    },
  },
  // ...
})
```

* **apiURL** (*String*) : The URL of your GraphQL endpoint
* **defaultHeaders** (*Object*) : HTTP headers that should be added to every request
* **errorManager** (*Function*) : A callback to handle GraphQL errors. This callback will be called every time a request returns an error object. *Please note that these statuses will not throw an exception, in conformity with [node-fetch's behaviour](https://www.npmjs.com/package/node-fetch#handling-exceptions). Exceptions will only be thrown by system or network errors.*

## Usage

### In a Slash class

```javascript
const { Slash } = require('/aido')

const query = `
query fetchUser($userId: Int!) {
  user(id: $userId) {
    id
    name
  }
}
`

class MySlash extends Slash {
  someAction() {
    const user = this.graphQL.query(query, { userId: 1 })
    this.state.userName = user.name
  }
}
```

### On application startup

```javascript
const startupTime = new Date()

const mutation = `
mutation logStartTime($date: Date!) {
  logStartTime(date: $date)
}
`

aido.start().then(() => {
  aido.helpers.graphQL.mutate(mutation, { date: startupTime })
})
```

### Inside a plugin

```javascript
const startupTime = new Date()

const mutation = `
mutation logPluginStartTime($date: Date!, $plugin: String!) {
  logStartTime(date: $date, plugin: $plugin)
}
`
function pluginFactory(koa, utils) {
  async function initPlugin() {
    utils.helpers.graphQL.mutate(mutation, { date: startupTime, plugin: 'my-plugin' })
  }
}
```

### Headers management

By default, the following HTTP header will be added to every request : `'Content-Type': 'application/json'`. You can change it on application startup, or when initializing a plugin, using the helper `baseHeaders` :

```javascript
aido.start().then(() => {
  aido.helpers.graphQL.baseHeaders['X-extra-special-header'] = 'YYYYYYYYYY'
})
```

You can add default headers, which will be added to every request, by specifying them in the GraphQL configuration (see above).

Finally, you can add additional headers to a specific query or mutation. The headers will be merged in the following order : baseHeaders, defaultHeaders, additionalHeaders.

## API

### query(query, variables, additionalHeaders)

* **query** (*String*) : A GraphQL query
* **variables** (*Object*) : Substitution variables for your query
* **additionalHeaders** (*Object*) : Additional HTTP headers to add to the request

### mutate(query, variables, additionalHeaders)

* **query** (*String*) : A GraphQL mutation
* **variables** (*Object*) : Substitution variables for your mutation
* **additionalHeaders** (*Object*) : Additional HTTP headers to add to the request

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