在微服务下使用GraphQL的BFF 之后,我们开始看看怎么构建个GraphQL服务器
1. 环境
系统上安装了Node.js
2. 创建一个目录
mkdir graphql-server
3.安装服务器环境
3.1 搭建package.json文件
然后转到项目的根目录并运行以下命令以快速搭建package.json文件。
graphql-server> npm init -y
Wrote to ...\graphql-server\package.json:
{
"name": "graphql-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
3.2 安装三个npm软件包
- Express:创建一个简单的Node.js服务器
- GraphQL:Node.js GraphQL服务器库
- Express-GraphQL:用于创建GraphQL服务器的Express中间件
运行以下命令将这些软件包一起安装。
graphql-server> npm install express graphql express-graphql
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN graphql-server@1.0.0 No description
npm WARN graphql-server@1.0.0 No repository field.
+ express-graphql@0.12.0
+ express@4.17.1
+ graphql@15.5.0
added 58 packages from 37 contributors and audited 58 packages in 3.85s
found 0 vulnerabilities
4 安装完成后,构建GraphQL服务器
4.1 根目录下创建一个名为data.js文件
首先,创建一个静态数据存储并将其导出。在项目的根目录下创建一个名为data.js的文件,然后粘贴以下代码:
/* data.js */
const Users = [
{
id: 1,
name: "Fikayo Adepoju",
email: "fik4christ@yahoo.com",
posts: [
{
id: 1,
title: "Debugging an Ionic Android App Using Chrome Dev Tools",
published: true,
link:
"https://medium.com/@coderonfleek/debugging-an-ionic-android-app-using-chrome-dev-tools-6e139b79e8d2",
author: 1
},
{
id: 2,
title: "Hosting a Laravel Application on Azure Web App",
published: true,
link:
"https://medium.com/@coderonfleek/hosting-a-laravel-application-on-azure-web-app-b55e12514c46",
author: 1
}
]
},
{
id: 3,
name: "Jane Paul",
email: "jane@company.com",
posts: []
}
];
module.exports = {
Users
};
该文件为每个用户导出用户数据和文章的集合。
4.2 根目录下创建一个名为schema.js文件
然后构建并导出架构。在项目的根目录下创建一个名为schema.js的文件,然后粘贴以下代码:
/* schema.js */
const { buildSchema } = require("graphql");
const schema = buildSchema(`
type Query {
users: [User!]!,
user(id: Int!): User!
}
type User {
id: ID!
name: String!
email: String
posts: [Post!]
}
type Post {
id: ID!
title: String!
published: Boolean!
link: String
author: User!
}
`);
module.exports = schema;
该文件使用Node.js的GraphQL库中的buildSchema方法来设置架构。创建两个自定义类型,用户和发布,以在查询定义中公开用户和用户查询点。
4.3 根目录中创建一个名为resolvers.js的文件
接下来,创建一个解析器来处理这些查询。在项目的根目录中创建一个名为resolvers.js的文件,然后粘贴以下代码:
/* resolvers.js*/
const {Users} = require('./data')
const resolvers = {
users: async (_) => {
return Users;
},
user: async ({ id }, context) => {
return Users.find((user) => user.id == id)
}
};
module.exports = resolvers;
该文件从data.js导入Users集合,然后将适当的数据返回给用户和用户查询点的解析器。
然后将架构连接到解析器,并公开GraphQL端点。
4.4 根目录下创建一个名为index.js的文件
在项目的根目录下创建一个名为index.js的文件,然后粘贴以下代码:
/* index.js */
const express = require("express");
const graphqlHTTP = require("express-graphql").graphqlHTTP;
const schema = require("./schema");
const resolvers = require("./resolvers");
const app = express();
app.use(
"/graphql",
graphqlHTTP({
schema,
rootValue: resolvers,
graphiql: true
})
);
const port = process.env.PORT || 4200;
app.listen(port);
console.log(`🚀 Server ready at http://localhost:4200/graphql`);
该文件创建一个ExpressJS应用程序,使用express-graphql中间件软件包将架构连接到解析器,然后在端点/ graphql上公开GraphQL API。
将第三个参数graphiql设置为true。您可以使用此GraphiQL使用该工具。GraphiQL(您是否注意到i的包含)是用于测试GraphQL查询的基于Web的GUI。该工具包含在GraphQL软件包中。
最后,配置服务器以侦听端口4200。
5 运行GraphQL服务器
完成到目前为止的步骤之后,该运行GraphQL服务器了。 通过在项目的根目录中运行以下命令来启动服务器
node index.js
� Server ready at http://localhost:4200/graphql