# dyson-graphql

> Simplify GraphQL stubbing with Dyson 🔧

Latest version **1.0.2** (published 2021-06-07) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-06-07 |
| First published | 2021-06-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 21.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Wealth Wizards Engineering |
| Maintainers | wealthwizards |

## Links

- npm: https://www.npmjs.com/package/dyson-graphql
- Repository: https://github.com/WealthWizardsEngineering/dyson-graphql
- npm.io page: https://npm.io/package/dyson-graphql

## Dependencies (2)

- [graphql](https://npm.io/package/graphql.md) ^15.5.0
- [@types/express](https://npm.io/package/@types/express.md) ^4.17.12

## Recent versions

- 1.0.2 (latest) — 2021-06-07
- 1.0.1 — 2021-06-07
- 1.0.0 — 2021-06-07

## README

# dyson-graphql

> Simplify GraphQL stubbing with Dyson 🔧

Dyson is great at making development simpler when different endpoints provide different responses. 
However as GraphQL uses the same endpoint for all requests, stubbing requires adding logic and 
complexity. Especially if you want fast feedback when executing invalid or incomplete GraphQL 
queries.

`dyson-graphql` wraps your stubbed data in the [`graphql`](https://www.npmjs.com/package/graphql) 
reference implementation for reliable and accurate results.

## Getting Started

```bash
$ yarn add --dev dyson-graphql
```

Add a new file to your dyson stubs directory for your GraphQL endpoint, use a schema and stubbed 
responses to provide a built resolver to the dyson `render` key;

```javascript
const dysonGraphQl = require("dyson-graphql");

const schema = `
  type User {
    id: Int!
    name: String!
  }

  type Query {
    currentUser: User!
  }

  type Mutation {
    createUser(name: String!): User!
    updateUser(id: Int!, name: String!): User!
  }
`;

module.exports = {
  path: "/graphql",
  method: "POST",
  render: dysonGraphQl(schema)
    .query("currentUser", { id: 987, name: "Jane Smart" })
    .mutation("createUser", ({ name }) => ({ id: 456, name }))
    .mutation("updateUser", ({ id, name }) => {
      if (id < 1000) {
        return { id, name };
      }

      throw new Error("Can't update user");
    })
    .build()
};
```

## Configuration

Specify your Dyson `path` and `method` properties as normal, for most GraphQL endpoints this will 
end with `/graphql` and be a `POST` method.

Supply your schema to the default export as above, this is necessary so `graphql` can validate 
queries.

Chain `query` and `mutation` for each stubbed GraphQL method.

- `query` and `mutation` methods can have a static response by supplying the response
- dynamic response by supplying a function (can be used with libraries like `faker`)
- errors by supplying a function that throws an error

Build the resolver by finishing the chain by calling `build`.

Provide this the built resolver to the Dyson stub as the `render` property.

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