GraphQL模块化开发

2023-05-27 0 692

序言

起不断进步。

日月未确定,你我皆是异军突起。澄海三日同风起,一落千丈三千里。品性从磨练出,自苦寒自苦寒来。何须至千里,何须至千里,不积小流何须成连城。

假如有累赘的天数,就秉持自学吧,儿时自学实现理想,长大成人了自学多样涵义,老了自学增添寂静。体认,英语课堂,我TNUMBERV12V4!

简述

《JavaScript全栈班》收费项目表明:

时数:50科白

产品价格:9999元

文本:

NodeJS合作开发后端API

React合作开发H5端

Electron合作开发桌面端软件

ReactNative合作开发Android和iOS跨端APP

听课形式:百度全会低年级课堂教学

第一集该文是《JavaScript全栈班》的第8首诗,后面除了:

《构筑JavaScript全栈合作开发自然环境》

《NodeJS和GraphQL构筑SNS讲义API》

《采用Express合作开发Web应用领域》

《第二个GraphQL API》

《基于GraphQL的讲义增删改查API》

《NodeJS整合MongoDB》

《GraphQL整合MongoDB》

模组化拆分

目标

1、让GraphQL的各个功能模块解耦,能够独立进行维护

2、让代码逻辑更清晰,主代码更简洁

3、让代码更利于维护

Schema模块完整代码

api/src/schema.js

const { gql } = require( apollo-server-express );

// 采用 GraphQL 模式语言编制一个模式

const typeDefs = gql`

type Note {

_id: ID!

content: String

author: String

}

type Query {

notes: [Note]!

note(id: ID): Note!

}

type Mutation {

newNote(content: String): Note!

}

`;

module.exports = typeDefs

Resolver模块完整代码

api/src/resolvers.js

const { ObjectId, add, find, findOne } = require( ./db )

// 为模式字段添加解析函数

const resolvers = {

Query: {

notes: async () =>

{

return await find( {} )

},

note: async ( parent, args ) =>

{

return await findOne( { “_id”: ObjectId( args.id ) } )

}

},

Mutation: {

newNote: async ( parent, args ) =>

{

// 新增

let noteValue = {

content: args.content,

author: “张澄海”,

}

await add( [ noteValue ] )

// 返回数据,新增会直接修改该对象

return noteValue

}

}

};

module.exports = resolvers

数据库模块完整代码

api/src/db.js

require( dotenv ).config();

const { MongoClient, ObjectId } = require( mongodb );

// 从自然环境变量读取相关DB信息

const URL = process.env.DB_URL;

const USERNAME = process.env.DB_USERNAME;

const PASSWORD = process.env.DB_PASSWORD;

const DATABASE = process.env.DB_DATABASE;

// 创建客户端

const client = new MongoClient( URL, options = {

auth: {

username: USERNAME,

password: PASSWORD,

}

} );

// 添加数据

async function add ( data )

{

// 等待连接

await client.connect();

const collection = client.db( DATABASE ).collection( documents );

// 添加数据

// data = [ { a: 1 }, { a: 2 }, { a: 3 } ]

const result = await collection.insertMany( data );

await client.close();

return result

}

// 查询数据

async function find ( data )

{

// 等待连接

await client.connect();

const collection = client.db( DATABASE ).collection( documents );

// 查询数据

// data = {}

const result = await collection.find( data ).toArray();

await client.close();

return result

}

async function findOne ( data )

{

// 等待连接

await client.connect();

const collection = client.db( DATABASE ).collection( documents );

// 查询数据

// data = {}

const result = await collection.findOne( data );

await client.close();

return result

}

// 导出集合操作对象

module.exports = {

ObjectId,

add,

find,

findOne,

};

主模块完整代码

api/src/index.js

require( dotenv ).config(); // 读取.env作为自然环境变量

const express = require( express );

const { ApolloServer } = require( apollo-server-express );

const typeDefs = require( “./schema” )

const resolvers = require( “./resolvers” )

// 读取端口

const PORT = process.env.PORT || 4000;

const app = express();

// 设置Apollo Server

const server = new ApolloServer( { typeDefs, resolvers } );

// 应用领域 Apollo GraphQL 中间件,把路径设为 /api

server.applyMiddleware( { app, path: /api } );

// 监听端口

app.listen( PORT, () =>

console.log( `启动服务成功:http://localhost:${ PORT }${ server.graphqlPath }` )

);

测试

启动服务:

npm run dev

测试GraphQLAPI:http://localhost:4001/api

总结

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务