zaife /zīf/
A simple GraphQL Koa server using MongoDB as database.
Quickstart
Install
npm install --save zaife
Example
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
,
; const server = port: 3000 host: 'localhost' mongoUri: 'mongodb://localhost:27017/appname' pathToModels: './models' pathToResolvers: './resolvers' pathToTypes: './types'; serverstart; // .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
,
; const schema = email: type: String unique: true required: true ; schemaindex email: 1 unique: true ;mongoose;
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 DateTimescalar 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
,
; const UserModel = ; const Mutation = async { return args; }; moduleexports = Mutation ;
In file resolvers/userQueries.js
,
; const UserModel = ; const Query = async { return UserModel; }; moduleexports = Query ;
In file resolvers/scalars/date.js
,
; DateTime: ScalarsGraphQLDateTime;
In file resolvers/scalars/email.js
,
; Email: ScalarsGraphQLEmail;
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.
; const GraphQLEmail GraphQLURL GraphQLDateTime GraphQLLimitedString GraphQLPassword GraphQLUUID GraphQLJSON = Scalars;