# mock-relay-server

> a simple library for generating functions to call in relay server mocks

Latest version **0.1.2** (published 2018-02-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install mock-relay-server
pnpm add mock-relay-server
yarn add mock-relay-server
bun add mock-relay-server
```

## 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.2 |
| Published | 2018-02-14 |
| First published | 2018-01-02 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 160.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Clay Branch |
| Maintainers | cbranch101 |

## Links

- npm: https://www.npmjs.com/package/mock-relay-server
- Repository: https://github.com/cbranch101/mock-relay-server
- Homepage: https://github.com/cbranch101/mock-relay-server#readme
- Issues: https://github.com/cbranch101/mock-relay-server/issues
- npm.io page: https://npm.io/package/mock-relay-server

## Dependencies (5)

- [chance](https://npm.io/package/chance.md) ^1.0.12
- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [graphql-relay](https://npm.io/package/graphql-relay.md) ^0.5.4
- [babel-polyfill](https://npm.io/package/babel-polyfill.md) ^6.26.0
- [babel-preset-es2016](https://npm.io/package/babel-preset-es2016.md) ^6.24.1

## Recent versions

- 0.1.2 (latest) — 2018-02-14
- 0.1.1 — 2018-01-02
- 0.1.0 — 2018-01-02

## README

# mock-relay-server

A tool for using simple data input to mock a GraphQL schema in a way that allow for full CRUD access

## Example

```js
import { getMockedResolvers } from 'mock-relay-server'
import { makeExecutableSchema } from 'graphql-tools'

// Your full schema in GraphQL type language
const typeDefs = `
  type User {
      id : ID!
      name: String
  }
  ...
`

// The data to initialize your mock database
const users = [
    {
        id: '1',
        name: 'User One',
    },
    {
        id: '1',
        name: 'User Two',
    },
]

const mocks = {
    User: {
        // connections are created based on any types that are provided a
        // a data key, in this case only user
        // this is your interface with the mock database
        data: users,
    },
    Query: {
        // this resolver functions similarly to resolvers in graphql-tools
        // with the only additional wrinkle that you're provided connections
        // as a way to interface with the database
        resolver: connections => ({
            getUser: async (query, { userId }) => connections.User.getNode(userId),
            getUsers: connections.User.paginate,
        }),
    },
    Mutation: {
        resolver: connections => ({
            updateUser: connections.Widget.update,
            addUser: connections.Widget.create,
            deleteUser: connections.Widget.delete,
        }),
    },
}

const resolvers = getMockedResolvers(mocks)

// at this point, you have a fully valid graphQL schema that can be
// be used to locally run queries
const schema = makeExecutableSchema({ typeDefs: [typeDefs], resolvers })

```
## Creating mocks
The mocks in `mock-relay-server` are very similar to the [resolver map](https://www.apollographql.com/docs/graphql-tools/resolvers.html) from [`graphql-tools`](https://www.apollographql.com/docs/graphql-tools/) with a couple of key differences.  First, you can provide an optional `data` key which gives you full CRUD on the array of items provided. Second, instead of providing resolver directly, it's provided on a `resolver` key that returns a function that provides you the initialized `connections` and returns the final resolver map.

## Connections API

Connections are used to make it easy to implement resolvers.  Most of the time, you should be able to use one of the helpers on a connection with out needing any configuration.

### create / update / delete
These all should all work out of the box with very little customization.  The only requirement is that the mutation that they're mocking only accepts an `input` object as a variable

### getNode
Accepts a global id(the combination of the id and the nodes type) and returns the associated node

### paginate
Accepts standard relay compliant input variables and returns the expected relay compliant connection format

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