Node + Egg + TS + Mongodb + Resetful + graphql
This lesson is about graphQL and Egg timing tasks
Operating environment: Node, Yarn/NPM, MongoDB
Miss Liang starts class again! Node uses Mongodb for TS operations (link from previous lesson).
As usual, the address of the local tutorial is: github.com/liangwei010…
Egg - Demo Exercises ─ app │ ├─ Controller │ │ │ └ ─ ─ home. Ts ├ ─ ─ model (model of database table structure of abstracting) │ │ └ ─ ─ the User. The ts │ ├ ─ ─ graphql (graphql folder) │ │ └ ─ ─ mutation (all mutation statement folder) │ │ └ ─ ─ schema. Graphql (all the mutation of declaration documents) │ │ └ ─ ─ query all the query statement (folder) │ │ └ ─ ─ schema. Graphql (all of the query statement file) │ │ └ ─ ─ User (user model statements and implementation) │ │ └ ─ ─ resolver. Js (statement function implementation) │ │ └ ─ ─ schema. Graphql (user schema field declarations) │ ├ ─ ─ service (controller The layer is not recommended to carry too many services. When weighing on business service layer) │ │ └ ─ ─ the user. The ts │ ├ ─ ─ the schedule (timing task folder) │ │ └ ─ ─ addUserJob. Ts │ └ ─ ─ the router. Ts related mapping (Url) ├ ─ ─ the config Configuration file (frame) │ ├ ─ ─ config. The default. The ts │ ├ ─ ─ config. Local. Ts │ ├ ─ ─ config. Prod. Ts │ └ ─ ─ plugin. Ts ├ ─ ─test(test folder) │ └ ─ ─ / *. * * test. The ts ├ ─ ─ typings (directory to place which s file) │ └ ─ ─ / *. * * which s ├ ─ ─ the README. Md ├ ─ ─ package. The json ├ ─ ─ tsconfig. Json └ ─ ─ tslint. JsonCopy the code
This tutorial adds the Schedule folder and the GraphQL folder.
An egg using graphql
Graphql is just a solution to a restful problem. It is in the database after operation, the field of the right to return to the user, means that the user wants to return to which field is returned which field, if say, my PC and mobile terminal public a set of interfaces, PC need 100 fields, mobile client need 10 fields, using resetful whether you need not to need, will return to you, Graphql solves this problem perfectly, because the choice lies with the caller.
Isn’t it amazing that JS and TS can coexist? For this tutorial, the js and TS versions coexist, and the next tutorial will use pure TS tutorials. The problem with using JS is that, in resolver.js, you will not be able to enjoy the code hints and compile checks of TS code. However, many projects may be existing projects and such JS cannot be refactored or changed immediately. Coexistence also makes sense, refactoring or rewriting step by step.
// plugin.ts
const plugin: EggPlugin = {
// mongoose
mongoose: {
enable: true,
package: 'egg-mongoose'}, // Add graphql graphQL: {enable: true,
package: 'egg-graphql',}};Copy the code
// config.default.ts
config.graphql = {
router: '/graphql'// Whether to load to app, app is enabled by default:trueBy default, the agent is disabled:false// Whether to load the developer tool graphiQL. It is enabled by default. The same route as the Router field. Use a browser to open the visibility. graphiql:true}; config.middleware = ['graphql'];
Copy the code
Example for using user: Define the return field of user Schema
// schema
type User {
_id: String
userNo: String
userName: String
}
Copy the code
// Query all declarationstypeQuery {// array user: [user]} // Mutation all declarationstype// return user object user: user}Copy the code
The Mutation and Query declarations are implemented as follows:
'use strict';
module.exports = {
Query: {
async user(root, { }, ctx) {
return await ctx.model.User.find();
},
},
Mutation: {
async user(root, { }, ctx) {
return await ctx.model.User.create({userName: 'add user', userNo: 99}); }}},Copy the code
Then open the browser input: http://127.0.0.1:7001/graphql, have no disease, query a see see!!
Egg Timed task
Timed tasks are similar to our regular timed tasks. Scheduled tasks are generally classified into two types:
- One is to perform a task at intervals of several times
- The other is to perform a task at a certain point in time
- Execute at intervals (will be executed every 60 seconds)
static get schedule() {
return {
interval: '60s', // 60s intervaltype: 'all', // specify that all workers need to execute}; } asyncsubscribe() {
const ctx = this.ctx;
console.log('Add User every 60 seconds!! ' + new Date())
const test = await ctx.service.user.addUserByScheduleTest();
console.log(test)}Copy the code
- At a certain point in time (at 00:00:00 on the 15th of each month)
static get schedule() {
return {
cron: '0 0 0 15 * *', // Every month on the 15th :00:00type: 'worker'// Specify only one random process to execute job to prevent data conflictsdisable: false, // Whether to enable cronOptions: {tz:'Asia/Shanghai',}}; } asyncsubscribe () {
const ctx = this.ctx;
console.log('On the 15th of every month at 00:00:00!! ' + new Date())
}
Copy the code
See the egg’s official documentation for the configuration of time. It’s getting late again. Next time I’ll show you the TS version of GraphQL.