# @simpleview/sv-graphql-client

> Client for communicating with sv-graphql

Latest version **4.1.3** (published 2025-02-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @simpleview/sv-graphql-client
pnpm add @simpleview/sv-graphql-client
yarn add @simpleview/sv-graphql-client
bun add @simpleview/sv-graphql-client
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.3 |
| Published | 2025-02-19 |
| First published | 2019-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 9 |
| Unpacked size | 55.7 KB |
| Known vulnerabilities | 0 (+28 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Owen Allen |
| Maintainers | davidlpons, owenallenaz, qman33, svjoshua, georgeyb-sv, mkes99, colin.booen, ssmith_sv, aglazebrook, rmaynes, csarantidessv, mikesan789, kristian.gonzalez, paul.riding, arkmuntasser, actionawesome, mcutshall, sv-mikael, korychinn_sv, leandrodalmassoglb, dlford, sloutrel |

## Links

- npm: https://www.npmjs.com/package/@simpleview/sv-graphql-client
- Repository: https://github.com/simpleviewinc/sv-graphql-client
- Homepage: https://github.com/simpleviewinc/sv-graphql-client#readme
- Issues: https://github.com/simpleviewinc/sv-graphql-client/issues
- npm.io page: https://npm.io/package/@simpleview/sv-graphql-client

## Dependencies (9)

- [axios](https://npm.io/package/axios.md) 0.24.0
- [lodash](https://npm.io/package/lodash.md) 4.17.21
- [graphql-tag](https://npm.io/package/graphql-tag.md) 2.12.6
- [jsvalidator](https://npm.io/package/jsvalidator.md) 2.0.0
- [p-memoize-cjs](https://npm.io/package/p-memoize-cjs.md) 7.1.0
- [@apollo/server](https://npm.io/package/@apollo/server.md) 4.11.3
- [browser-or-node](https://npm.io/package/browser-or-node.md) 2.0.0
- [@graphql-tools/utils](https://npm.io/package/@graphql-tools/utils.md) 8.6.5
- [@graphql-tools/schema](https://npm.io/package/@graphql-tools/schema.md) 8.3.5

## Recent versions

- 4.1.3 (latest) — 2025-02-19
- 4.1.2 — 2024-12-06
- 4.1.1 — 2024-12-06
- 4.1.0 — 2024-09-23
- 4.0.0 — 2023-04-27
- 3.0.14 — 2023-02-19
- 3.0.13 — 2022-11-03
- 3.0.12 — 2022-11-01
- 3.0.11 — 2022-10-25
- 3.0.10 — 2022-10-25
- 3.0.9 — 2022-10-07
- 3.0.8 — 2022-10-07
- 3.0.6 — 2022-09-27
- 3.0.5 — 2022-09-26
- 3.0.4 — 2022-09-26
- … 22 more at https://npm.io/package/@simpleview/sv-graphql-client/versions

## README

# sv-graphql-client
Client and tools for communicating with sv-graphql.

# installation

```
npm install @simpleview/sv-graphql-client
```

# Package API

## GraphServer

`GraphServer` is an for communicating with the `sv-graphql` system. It helps eliminate some of the bloat of individual graphQL calls and makes it a little easier to work with.


* args
	* graphUrl - string - Fully qualified URL pointing to the graphURL server.
	* context - object - Context object used for handling token/acct_id
		* acct_id - string - The acct_id that the user is attempting to access. `acct_id` is required for any endpoints on `admin`.
		* token - `string` | `async () => string` - The token returned from the auth system. Token is required for accessing any of the non-login mechanics.
	* prefixes - array of `class` - Prefixes which encasulate the behavior of the graphQL apis.

In most cases you will be setting the `context` at runtime. If so, manually update the context via setting `graphServer.context.acct_id = "x"`.

Available prefixes, see the following packages to install the prefixes.

* [auth](https://github.com/simpleviewinc/sv-auth-client)
* [admin](https://github.com/simpleviewinc/sv-auth-client)
* [email](https://github.com/simpleviewinc/sv-email-client)

```
const { GraphServer } = require("@simpleview/sv-graphql-client");
const graphServer = new GraphServer({
	graphUrl : GRAPH_URL,
	prefixes : [PrefixOne, PrefixTwo]
});
```

The endpoints on the graphServer prefix should, in general, match 1-to-1 with the arguments and syntax of the GraphQL schema browser.

## TestServer

`TestServer` is a tool for spinning up a simple GraphQL server that listens on a port and loads some graphql files via `schemaLoader` for it's operations. It's usually used to simplify unit tests.

See [TestServer](src/TestServer.js) for arguments.

```
const testServer = new TestServer({
	port: 8080,
	paths: [
		"test/graphqlTest"
	]
});

// start the server, listen on the port
await testServer.boot();

// ... run tests ... //

// close the server
await testServer.close();
```

## nullToUndefined

This function will take a graphQL response and convert `null` values to `undefined`. It can be helpful for trimming away fields that weren't returned from graph.

This does an by reference modification of the object. It does not return a clone.

* obj - object - The object that you wish to clean.

```js
const { nullToUndefined } = require("@simpleview/sv-graphql-client");
const result = await someGraphCall();
nullToUndefined(result);
```


## query

Wrapper function to make it easier to talk to `sv-graphql` directly.

* args
	* query - string - The graphQL query string. Usually best passed with JS template tag syntax.
	* variables - obj - If you're query utilizes variables, pass them on this object.
	* url - string - The URL of the graphQL endpoint.
	* token - `string` | `async () => string` - The token which will be passed on the `Authorization` header as a `Bearer` token. If using a function it will be called to return the token.
	* headers - obj - An object of headers to append to the request
	* key - string - Whether to reach and return a specific sub-key of the return.
	* clean - boolean - Whether to automatically run nullToUndefined on the result set to clean it.

```js
const { query } = require("@simpleview/sv-graphql-client");
const result = await query({
	query : `
		query($token: String) {
			auth {
				current
			}
		}
	`,
	variables : {
		token : "my fake token"
	},
	url : "https://graphql.simpleviewinc.com/"
});
```

## isPlainObject

Simple function for testing if a function is a plain JS object.

## schemaLoader

schemaLoader takes all of the files present in `paths` or `loaders` and merges them into a single schema. This allows you to make your larger schema more maintainable by splitting it up into logical units. An example of a modular schema is outlined in [Modularizing Your GraphQL Schema](https://www.apollographql.com/blog/modularizing-your-graphql-schema-code-d7f71d5ed5f2/).

* args
  * paths - string[] - Folders containing graphql schema files which export a `GraphModule`.
  * loaders - function[] - Loaders that will return a `GraphModule`. Use this when you need to load graph definitions that are dynamically generated.

A `GraphModule` is an object of `{ typeDefs, resolvers, schemaTransformers }`, all keys are optional.

Example `GraphModule`:
```js
const { gql } = require("apollo-server");

const typeDefs = gql`
	extend type Query {
		test_path2: Boolean
	}
`;

const resolvers = {
	Query: {
		test_path2: function() {
			return true;
		}
	}
}

module.exports = {
	typeDefs,
	resolvers
}
```

```js
const { schemaLoader } = require("@simpleview/sv-graphql-client");

const schema = await schemaLoader({
	paths : ['/app/lib/graphql']
});

const server = new ApolloServer({
  schema,
  ...
});
```
# Publishing

`sudo npm run docker`
`yarn run build`
`exit`
`sudo npm run publish VERSION`

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