# zaife

> GraphQL Koa Server using Mongoose as a database.

Latest version **3.5.1** (published 2018-05-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install zaife
pnpm add zaife
yarn add zaife
bun add zaife
```

## 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 | 3.5.1 |
| Published | 2018-05-23 |
| First published | 2018-05-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 8 KB |
| Known vulnerabilities | 0 (+4 in 2 direct dependencies) |
| Install scripts | no |
| Author | ccpacillos |
| Maintainers | ccpacillos |
| Keywords | graphql, koa |

## Links

- npm: https://www.npmjs.com/package/zaife
- Repository: https://github.com/ccpacillos/zaife
- Homepage: https://github.com/ccpacillos/zaife#readme
- Issues: https://github.com/ccpacillos/zaife/issues
- npm.io page: https://npm.io/package/zaife

## Dependencies (11)

- [koa](https://npm.io/package/koa.md) ^2.5.1
- [graphql](https://npm.io/package/graphql.md) ^0.13.2
- [mongoose](https://npm.io/package/mongoose.md) ^5.1.2
- [koa2-cors](https://npm.io/package/koa2-cors.md) ^2.0.5
- [koa-router](https://npm.io/package/koa-router.md) ^7.4.0
- [graphql-tools](https://npm.io/package/graphql-tools.md) ^3.0.2
- [koa-bodyparser](https://npm.io/package/koa-bodyparser.md) ^4.2.1
- [apollo-server-koa](https://npm.io/package/apollo-server-koa.md) ^1.3.6
- [graphql-type-json](https://npm.io/package/graphql-type-json.md) ^0.2.1
- [graphql-custom-types](https://npm.io/package/graphql-custom-types.md) ^1.4.0
- [merge-graphql-schemas](https://npm.io/package/merge-graphql-schemas.md) ^1.5.1

## 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

- 3.5.1 (latest) — 2018-05-23
- 3.5.0 — 2018-05-22
- 3.4.3 — 2018-05-22
- 3.4.2 — 2018-05-22
- 3.4.1 — 2018-05-22
- 3.4.0 — 2018-05-22
- 3.3.2 — 2018-05-22
- 3.3.1 — 2018-05-21
- 3.3.0 — 2018-05-21
- 3.2.1 — 2018-05-21
- 3.2.0 — 2018-05-21
- 3.1.1 — 2018-05-13
- 3.1.0 — 2018-05-13
- 3.0.11 — 2018-05-10
- 3.0.10 — 2018-05-10
- … 23 more at https://npm.io/package/zaife/versions

## README

# zaife /zīf/

A simple GraphQL Koa server using MongoDB as database.

## Quickstart

### Install

```bash
npm install --save zaife
```

### Example

Given the following arbitrary* file structure for the examples below:

```filenames
+-- server.js
+-- types
|   +-- user.gql
|   +-- scalar.gql
+-- resolvers
|   +-- userMutations.js
|   +-- userQueries.js
|   +-- scalars
|   |   +-- date.js
|   |   +-- email.js
+-- models
|   +-- user.js
```

**You can design your own file structure to your liking.*

#### Server

In file `server.js`,

```javascript
import { Server } from 'zaife';

const server = new Server({
  port: 3000,
  host: 'localhost',
  mongoUri: 'mongodb://localhost:27017/appname',
  pathToModels: './models',
  pathToResolvers: './resolvers',
  pathToTypes: './types'
});

server.start(); // .start() is an async function
```

#### Mongoose Models

You can have multiple model files in any path structure you like under the directory you specified in `options.pathToModels`. The models are automatically loaded and are accessible from `zaife` module.

In file `models/user.js`,

```javascript
import mongoose, { Schema } from 'mongoose';

const schema = new Schema({
  email: { type: String, unique: true, required: true }
});

schema.index({ email: 1 }, { unique: true });
mongoose.model('User', schema);
```

#### Graphql Schema Files

Your graphql schemas can be structured in separate files in any path structure you like as long as they are under the directory you specified in `options.pathToTypes`.

In file `types/user.gql`,

```gql
type User {
  _id: ID!
  email: String!
}

type Mutation {
  createUser(email: String!): User!
}

type Query {
  getUser(_id: ID!): User
}
```

In file `types/scalar.gql`,

```gql
scalar DateTime
scalar Email
```

#### Resolvers and Accessing Models

You can have multiple resolver files in any path structure you like under the directory you specified in `options.pathToResolvers`.

In file `resolvers/userMutations.js`,

```javascript
import { Models } from 'zaife';

const UserModel = Models('User');

const Mutation = {
  async createUser(parent, args, ctx, info) {
    return new UserModel(args).save();
  }
};

module.exports = { Mutation };
```

In file `resolvers/userQueries.js`,

```javascript
import { Models } from 'zaife';

const UserModel = Models('User');

const Query = {
  async getUser(parent, args, ctx, info) {
    return UserModel.findOne(args);
  }
};

module.exports = { Query };
```

In file `resolvers/scalars/date.js`,

```javascript
import Scalars from 'zaife';

export default {
  DateTime: Scalars.GraphQLDateTime,
};
```

In file `resolvers/scalars/email.js`,

```javascript
import Scalars from 'zaife';

export default {
  Email: Scalars.GraphQLEmail,
};
```

## Docs

### Class: **Zaife.Server**

### **const server = new Server(options)**

Initializes the server configuration.

* **options.port?** `<number>` - HTTP port to be used. Default: `process.env.PORT` || `3000`
* **options.host?** `<string>` - HTTP host to be used. Default: `process.env.HOST` || `'localhost'`
* **options.mongoUri?** `<string>` - Mongo URI. Default: `process.env.MONGO_URI` || `'mongodb://localhost:27017/zaife'`
* **options.pathToResolvers** `<string>` - Directory of resolver files
* **options.pathToTypes** `<string>` - Directory of type (.gql) files
* **options.pathToModels** `<string>` - Directory of mongoose model files

### **server.start()**

Starts the server. An `async` function.

### **server.stop()**

Stops the server. An `async` function.

### Object: **Zaife.Models**

### **const ModelName = Models(modelName)**

Access your mongoose models given the model name.

* **modelName** `<string>` - Name of your mongoose model

### Object: **Zaife.Scalars**

Access custom scalar types.

```javascript
import { Scalars } from 'zaife';

const {
  GraphQLEmail,
  GraphQLURL,
  GraphQLDateTime,
  GraphQLLimitedString,
  GraphQLPassword,
  GraphQLUUID,
  GraphQLJSON,
} = Scalars;
```

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