npm package: @r26d/utils-graphql
Description: GraphQL utilities
Latest version: 1.0.4

# @r26d/utils-graphql

This package also includes updates to other util functions ao

> GraphQL utilities

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
<!-- END doctoc -->

- [Installation](#installation)
  - [Using npm](#using-npm)
  - [Using yarn](#using-yarn)
- [Types](#types)
- [API](#api)
  - [arrayAppendFn](#arrayappendfn)
    - [Parameters](#parameters)
  - [arrayRemoveFn](#arrayremovefn)
    - [Parameters](#parameters-1)
  - [arrayReplaceFn](#arrayreplacefn)
    - [Parameters](#parameters-2)
  - [booleanize](#booleanize)
    - [Parameters](#parameters-3)
  - [get](#get)
    - [Parameters](#parameters-4)
  - [getIn](#getin)
    - [Parameters](#parameters-5)
  - [compositeHasInFn](#compositehasinfn)
    - [Parameters](#parameters-6)
  - [compositeMap](#compositemap)
    - [Parameters](#parameters-7)
  - [createDeferred](#createdeferred)
  - [errorsToString](#errorstostring)
    - [Parameters](#parameters-8)
    - [Examples](#examples)
  - [getOperationType](#getoperationtype)
    - [Parameters](#parameters-9)
    - [Examples](#examples-1)
  - [hasSubscription](#hassubscription)
    - [Parameters](#parameters-10)
  - [requestFromCompat](#requestfromcompat)
    - [Parameters](#parameters-11)
    - [Examples](#examples-2)
  - [requestToCompat](#requesttocompat)
    - [Parameters](#parameters-12)
    - [Examples](#examples-3)
- [License](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Installation

### Using [npm](https://docs.npmjs.com/cli/npm)

    $ npm install --save @r26d/utils-graphql

### Using [yarn](https://yarnpkg.com)

    $ yarn add @r26d/utils-graphql

## Types

```javascript
export type {DocumentNode} from "graphql/language/ast";

type GqlErrorLocation = {|
  line: number,
  column: number
|};

type GqlError = {|
  message: string,
  locations?: Array<GqlErrorLocation>
|};

type GqlRequest<Variables: void | Object = void> = {|
  operation: string,
  variables?: Variables
|};

type GqlRequestCompat<Variables: void | Object = void> = {|
  query: string,
  variables?: Variables
|};

type GqlResponse<Data> = {|
  data?: Data,
  errors?: Array<GqlError>
|};

type GqlOperationType = "mutation" | "query" | "subscription";
```

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### arrayAppendFn

Returns a new Array with elements appended to the one given.

#### Parameters

*   `elements` &#x20;
*   `array` &#x20;

### arrayRemoveFn

Returns a new Array with the result of having removed the specified amount
(count) of elements at the given index.

#### Parameters

*   `index` &#x20;
*   `count` &#x20;
*   `array` &#x20;

### arrayReplaceFn

Returns a new Array with the result of having replaced the elements at the
given index with the ones specified.

#### Parameters

*   `index` &#x20;
*   `elements` &#x20;
*   `array` &#x20;

### booleanize

Returns a new promise which follows the one given returning true in case
there was no error, or false otherwise

#### Parameters

*   `promise` &#x20;

### get

Get property value of given key.

#### Parameters

*   `key` &#x20;
*   `composite` &#x20;

### getIn

Returns value located at the given path or undefined otherwise.

#### Parameters

*   `path` &#x20;
*   `composite` &#x20;

### compositeHasInFn

Returns true if value located at given path is deeply equal to the one
specified.

#### Parameters

*   `path` &#x20;
*   `value` &#x20;
*   `composite` &#x20;

### compositeMap

Maps values of the given composite using mapper

#### Parameters

*   `mapper` &#x20;
*   `composite` &#x20;

### createDeferred

Creates a Deferred

### errorsToString

Transforms an array of GqlError into a string.

#### Parameters

*   `gqlErrors` &#x20;

#### Examples

```javascript
const gqlRespose = {
  errors: [
    {message: "First Error", locations: [{column: 10, line: 2}]},
    {message: "Second Error", locations: [{column: 2, line: 4}]}
  ]
}

const error = errorsToString(gqlRespose.errors);
// string with the following:
// First Error (2:10)
// Second Error (4:2)
```

### getOperationType

Returns the type (query, mutation, or subscription) of the given operation

#### Parameters

*   `operation` &#x20;

#### Examples

```javascript
const operation = `
  subscription userSubscription($userId: ID!) {
    user(userId: $userId) {
      id
      name
    }
  }
`;

const operationType = getOperationType(operation);

console.log(operationType); // "subscription"
```

### hasSubscription

Returns true if documentNode has a subscription or false otherwise

#### Parameters

*   `documentNode` &#x20;

### requestFromCompat

Creates a GqlRequest using given GqlRequestCompat

#### Parameters

*   `gqlRequestCompat` **GqlRequestCompat\<Variables>**&#x20;

    *   `gqlRequestCompat.query` &#x20;
    *   `gqlRequestCompat.variables` &#x20;

#### Examples

```javascript
const query = `
  query userQuery($userId: ID!) {
    user(userId: $userId) {
      id
      email
    }
  }
`;

console.log(requestFromCompat({query, variables: {userId: 10}}));
// {operation: "...", variables: {userId: 10}}
```

Returns **GqlRequest\<Variables>**&#x20;

### requestToCompat

Creates a GqlRequest using given GqlRequestCompat

#### Parameters

*   `gqlRequest` **GqlRequest\<Variables>**&#x20;

    *   `gqlRequest.operation` &#x20;
    *   `gqlRequest.variables` &#x20;

#### Examples

```javascript
const operation = `
  query userQuery($userId: ID!) {
    user(userId: $userId) {
      id
      email
    }
  }
`;

console.log(requestToCompat({operation, variables: {userId: 10}}));
// {query: "...", variables: {userId: 10}}
```

Returns **GqlRequestCompat\<Variables>**&#x20;

## License

[MIT](LICENSE.txt) :copyright: \*\*r26D,LLC \*\*