npm package: @jumpn/utils-graphql
Description: GraphQL utilities
Author: Mauro Titimoli
Latest version: 0.6.0

# @jumpn/utils-graphql

> 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)
  - [errorsToString](#errorstostring)
    - [Parameters](#parameters)
    - [Examples](#examples)
  - [getOperationType](#getoperationtype)
    - [Parameters](#parameters-1)
    - [Examples](#examples-1)
  - [hasSubscription](#hassubscription)
    - [Parameters](#parameters-2)
  - [requestFromCompat](#requestfromcompat)
    - [Parameters](#parameters-3)
    - [Examples](#examples-2)
  - [requestToCompat](#requesttocompat)
    - [Parameters](#parameters-4)
    - [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 @jumpn/utils-graphql

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

    $ yarn add @jumpn/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. -->

### errorsToString

Transforms an array of GqlError into a string.

#### Parameters

-   `gqlErrors` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;GqlError>** 

#### 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)
```

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 

### getOperationType

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

#### Parameters

-   `operation` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 

#### Examples

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

const operationType = getOperationType(operation);

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

Returns **GqlOperationType** 

### hasSubscription

Returns true if documentNode has a subscription or false otherwise

#### Parameters

-   `documentNode` **DocumentNode** 

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 

### requestFromCompat

Creates a GqlRequest using given GqlRequestCompat

#### Parameters

-   `gqlRequestCompat` **GqlRequestCompat&lt;Variables>** 
    -   `gqlRequestCompat.query`  
    -   `gqlRequestCompat.variables`  

#### 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&lt;Variables>** 

### requestToCompat

Creates a GqlRequest using given GqlRequestCompat

#### Parameters

-   `gqlRequest` **GqlRequest&lt;Variables>** 
    -   `gqlRequest.operation`  
    -   `gqlRequest.variables`  

#### 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&lt;Variables>** 

## License

[MIT](LICENSE.txt) :copyright: **Jumpn Limited**