# graphql-obj2arg

> Convert a JavaScript object to a GraphQL arguments string

Latest version **0.1.0** (published 2016-11-30) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2016-11-30 |
| First published | 2016-11-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | Branden Horiuchi |
| Maintainers | vbranden |
| Keywords | graphql, arguments, object, convert |

## Links

- npm: https://www.npmjs.com/package/graphql-obj2arg
- Repository: https://github.com/graphql-factory/graphql-obj2arg
- Homepage: https://github.com/graphql-factory/graphql-obj2arg#readme
- Issues: https://github.com/graphql-factory/graphql-obj2arg/issues
- npm.io page: https://npm.io/package/graphql-obj2arg

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2016-11-30

## README

# graphql-obj2arg
Converts a JavaScript Object to a GraphQL arguments string

---

GraphQL is great, but it can sometimes be a pain to programatically create arguments for a query. This library can help create an argument string from a JavaScript object.

## Example (ES6)

```js
import graphql from 'graphql'
import obj2arg from 'graphql-obj2arg'
import PeopleSchema from './PeopleSchema'

let args = {
  id: 'jd8675309',
  name: 'John',
  age: 20,
  gpa: 3.9,
  role: 'Enum::STUDENT',
  courses: [
    {
      id: 101,
      name: 'CS101'
    },
    {
      id: 203,
      name: 'CS144'
    }
  ]
}

let query = `mutation updateStudentData {
  updateStudent(${obj2arg(args, { noOuterBraces: true })}) {
    id
  }
}`

graphql(PeopleSchema, query).then((result) => {
  ...
})
```

## API

#### `obj2arg` ( `arguments`, [ `options` ] )

Converts an object into a GraphQL arguments string

**Parameters:**

* `arguments` { `Object` } - Object containing arguments to convert
* [ `options` ] { `Object` } - Options hash
  * [ `keepNulls=false` ] { `Boolean` } - Allows nulls to be included in the argument string if `true`
  * [ `noOuterBraces=false` ] { `Boolean` } - Removes the outer `{}` or `[]` from the argument string
  
**Returns:**
`String` - Argument string

#### `obj2arg.Enum` ( `value` )

Creates an `Enum` object. When part of an arguments object the `Enum` will not be enclosed in double quotes

**Parameters:**

* `value` { `String` } - Enum name

**Returns:**
`Enum` - Enum object

## Declaring Types

Types can be declared in the argument object using declaration notation `<Declaration><Value>`. This is useful for data types that either do not exist in JavaScript (i.e. Enum), can be tricky to determine (i.e. Floats), or simply to ensure a type is parsed a certain way.

### Declarations

* Boolean - `Boolean::`
* Date - `Date::`
* Enum - `Enum::`
* Float - `Float::`
* Int - `Int::`

## Example

```js
let args = {
  bool: 'Boolean::true',
  date: 'Date::2100-01-01',
  enum: 'Enum::MYENUM',
  float: 'Float::1.2',
  int: 'Int::100'
}
```

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