# n-memb-gql-client [![CircleCI](https://circleci.com/gh/Financial-Times/n-memb-gql-client.svg?style=svg&circle-token=9e848c99b5855349388da529fec5322103189b06)](https://circleci.com/gh/Financial-Times/n-memb-gql-client) [![npm version](https://badge.fury.io/js/%40financial-times%2Fn-memb-gql-client.svg)](https://badge.fury.io/js/%40financial-times%2Fn-memb-gql-client)

A tiny wrapper around the membership graph ql api, with some convenient methods.

```
const membQl = require('@financial-times/n-memb-gql-client');

membQl.canned('user-query', { userId: 'XYZ' })
	.then(res => {
		if (!res._ok) {
			// valid response but contains errors
		}
		// handle response
	})
	.catch(err => {
		// handle error
	});
```


## Usage

Ensure `MEMBQL_API_KEY_PROD` and `MEMBQL_API_ENDPOINT_PROD` env variable is set.

By default queries will use production.

You must also set `MEMBQL_API_KEY_TEST` and `MEMBQL_API_ENDPOINT_TEST` if you wish to query using the test environment.

Further more you can set `MEMBQL_API_KEY_MOCK` and `MEMBQL_API_ENDPOINT_MOCK` if you wish to use a mock mode.

### `membQl.query(query, variables [, {testMode, mockMode, keepAlive, timeout}])`

- `query (string)` a raw graph ql query [see below](#custom-queries).

- `variables (object)` contains the variables required for the query.

#### Options

- `testMode (boolean) default=false` whether or not to use the test endpoint and api key.

- `mockMode (boolean) default=false` whether or not to use the mock endpoint and api key.

- `keepAlive (boolean) default=true` whether or not to keep the connection to the GraphQL API alive.

- `timeout (integer) default=5000` connection timeout cutoff, in ms.

- `overrideHost (string) (optional)` an override host to use instead of MEMBQL_API_ENDPOINT_* (remember to include protocol and correct path).

- `apiKeyHeaderName (string) (optional)` an override header to use instead of x-api-key (e.g. ft-next-backend-key or ft-api-key).


### `membQl.canned(queryName, variables [, {testMode, mockMode, keepAlive, timeout}])`

- `queryName (string)` the canned query name [see below](#canned-queries).

- `variables (object)` contains the variables required for the query.

#### Options

- `testMode (boolean) default=false` whether or not to use the test endpoint and api key.

- `mockMode (boolean) default=false` whether or not to use the mock endpoint and api key.

- `keepAlive (boolean) default=true` whether or not to keep the connection to the GraphQL API alive.

- `timeout (integer) default=5000` connection timeout cutoff, in ms.

- `overrideHost (string) (optional)` an override host to use instead of MEMBQL_API_ENDPOINT_* (remember to include protocol and correct path).

- `apiKeyHeaderName (string) (optional)` an override header to use instead of x-api-key (e.g. ft-next-backend-key or ft-api-key).


### Canned queries
```
membQl.canned('user-query', { userId: 'XYZ' });
```

See docs on canned queries [here](https://github.com/Financial-Times/memb-graphql-api#canned-queries). Query names can be found [here](https://github.com/Financial-Times/memb-graphql-api/tree/main/assets/static/query).


### Custom queries
```
const customQuery = `
	query($session: String!) {
		userBySession(session: $session) {
			access {
				isB2c
			}
		}
	}
`;
membQl.query(customQuery, { session: 'XYZ' });

```
