zaife v3.5.1
zaife /zīf/
A simple GraphQL Koa server using MongoDB as database.
Quickstart
Install
npm install --save zaifeExample
Given the following arbitrary* file structure for the examples below:
+-- 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,
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 functionMongoose 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,
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,
type User {
_id: ID!
email: String!
}
type Mutation {
createUser(email: String!): User!
}
type Query {
getUser(_id: ID!): User
}In file types/scalar.gql,
scalar DateTime
scalar EmailResolvers 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,
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,
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,
import Scalars from 'zaife';
export default {
DateTime: Scalars.GraphQLDateTime,
};In file resolvers/scalars/email.js,
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.
import { Scalars } from 'zaife';
const {
GraphQLEmail,
GraphQLURL,
GraphQLDateTime,
GraphQLLimitedString,
GraphQLPassword,
GraphQLUUID,
GraphQLJSON,
} = Scalars;7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago