Custom Modules

With this guide, you can extend your app with extra modules and functionality. Let's create a Articles module in this example.

With your configuration, you need to pass modules with modules property. Let's create module for it.


{
  ...YOUR_CONFIGURATION,
  modules: [
    {
     name: 'Article'     
    }
  ]
}

Database options:

To enable database, you need to pass useDatabase as true and provide database structure. Please see.


{
  ...YOUR_CONFIGURATION,
  modules: [
    {
     name: 'Article',
+    useDatabase: true,
    }
  ]
}

Great, Now you need to assign datbase fields, right now we are using Mysql with Sequelize. You can do:s


{
  ...YOUR_CONFIGURATION,
  modules: [
    {
     name: 'Article',
     useDatabase: true,
+    fields: {
+      sql: {
+        title: {
+          type: 'String'
+        }
+      }
+    }
    }
  ]
}

Great, you have setup fields for your module table, The object for title is same as for Sequelize table field, please see, Model Definition for Sequlize.

GraphQL

You have setup basics of your module, Now setup GraphQL:


{
  ...YOUR_CONFIGURATION,
  modules: [
    {
      name: 'Article',
      useDatabase: true,
      fields: {
        sql: {
          title: {
            type: 'String'
          }
        }
      },
+      graphql: {
+        crud: {
+          query: {
+            generate: true,
+            operations: "*"
+          }
+          mutation: {
+            generate: true,
+            operations: "*"
+          }
+        },
+        // Main schema for Article
+        schema: `
+          type Article {
+           title: String
+          }
+          type ArticleInput {
+           title: String
+          }
+        `,
+        mutation: {
+          schema ``, // Write mutation schema for your module Article
+          resolvers: {} // Resolvers for article mutations
+        },
+        query: {
+          schema: ``, // Write query schema for your module Article
+          resolvers: {} // Resolvers for article query
+        }
+      }
    }
  ]
}

Great you have enable GraphQL on your side. Now write a great GraphQL app. If you enable crud part it will generate following Queries and Mutations:


# mutations
createArticle(input: ArticleInput): Article
updateArticle(input: ArticleInput): Article
deleteArticle(input: ArticleInput): Article
bulkCreateArticle(input: [ArticleInput]): [Article]
bulkUpdateArticle(input: [ArticleInput]): [Article]
bulkDeleteArticle(input: [ArticleInput]): [Article]
# queries
viewArticle(id: Int): Article
listArticle(pagination: PaginationInput, filters: [FilterInput]): ArticleList
# ArticleList is autogenerated by Wertik

type ArticleList {
  list: [Article],
  pagination: Pagination
  filters: [Filters]
}

GraphQL is alive!

Rest API:

Sometimes GraphQL is not enough and you need to use Rest API and sometimes Rest API is not enough and you need to use GraphQL, In this part lets configure Rest API, This is more easier than GraphQL. Let's setup rest API:


{
  ...YOUR_CONFIGURATION,
  modules: [
    {
      name: 'Article',
      useDatabase: true,
      fields: {...},
+      restApi: {
+        endpoints: [
+          {
+            path: '/people/', // Will be available under http://localhost:port/api/v1/article/people/
+            methodType: 'get', // get,post,put,delete,copy,head,options,link,unlink,purge,lock,unlock,view
+            handler: function (req, res) {
+              res.json({
+                message: "Message from people"
+              })
+            }
+          }
+        ]
+      },
      graphql: {...}
    }
  ]
}