Skip to content

This is powerful library to generate whole server with defined (by GraphQL schema) models CRUD, authentication, together with API and GraphQL interface. That's the core of www.graphql.monster online service. #typescript #graphql #appolo #koa #api #authentication #crud #generator

License

Notifications You must be signed in to change notification settings

miuan/graphql-monster-builder

Repository files navigation

GraphQL Monster Builder

Build server with CRUD with GraphQL and Rest API interface. With these features:

  • Generate server base on simple schema file
  • Integrated authentication to secure models with pre-defined rules
  • Hooks to customize the server to your needs without breaking (re)build process
  • Integrated GraphQL documentation and Swagger for Rest API
  • Prepared CRA templates for easy connect to React App (ES6 or Typescipt)

Usage

$ graphql-monster-builder <operation> <schema> <outdir>

Example

Example of simple todo server. Create Schema File file with name todo.schema and folowing content in your local directory.

Note: Be sure on your system is running instance of MongoDB

@all(filter:"user_every.id={{userId}}")
@create("public") @one("public") @update("public") @remove("public")
type Todo @model {
  message: String
  done: Boolean @default(false)
}

Then run these command to actual create graphql-monster-server to directory with name todo-server

npx graphql-monster-builder --backend todo.schema todo-server

# or

npm i -g graphql-monster-builder
graphql-monster-builder --backend todo.schema todo-server

Now you should just move to todo-server directory and run generated server

cd todo-server/ && yarn && yarn start

Now you can check the:

GraphQL

Here is some examples with GraphQL mutations over Todo model Image of graphql Here is some examples with GraphQL mutations over Todo model Image of graphql

Rest API

The same can be acomplish with Rest API what is documented by Swagger Image of swagger

Documentation

For more about schema definition, please visit our documentation:

Explain create modesls and CRUD with GraphQL Monster

You can check video how to create models on www.graphql.monster platform

Hooks

During project development You will probably very often updating the schema (adding new models or updating existing or maybe changing the protecting rules). To customize server functionality is a option of adding a hook. Hooks can be of this two type:

  • before
  • after

and can have handle these actions:

  • One (read one item)
  • All (read all items)
  • My (read all my items)
  • Create (create one item)
  • Update (update one item)
  • Delete (delete one item)

Hooks are setlet in file entryHooks.ts in services/ with export object hooks with your defined overrides

// example of `service/entryHooks.ts`

export const hooks = {
    services: {
        beforeTodoCreate: async (entry, todo) => {
            // do some action before Todo is stored into DB
            todo.data.message = `Created Todo: ${todo.data.message}`
            return todo.data
        },
        beforeTodoUpdate: async (entry, todo) => {
            // do some action before Todo is updated in DB
            todo.data.message = `Updated Todo: ${todo.data.message}`
            return todo.data
        },
        afterTodoCreate: async (entry, todo) => {
            // do some action after Todo is updated in DB
            return todo.data
        },
        afterTodoCreate: async (entry, todo) => {
            // do some action after Todo is updated in DB
            return todo.data
        },
    },
}

Note: what is in gen/ is have fully on control the builder generator, so what ever you will change will be in next (re)build lost!

Contain

About

This is powerful library to generate whole server with defined (by GraphQL schema) models CRUD, authentication, together with API and GraphQL interface. That's the core of www.graphql.monster online service. #typescript #graphql #appolo #koa #api #authentication #crud #generator

Topics

Resources

License

Stars

Watchers

Forks

Languages