1.你要确保已经安装node.js
打开cmd 输入查看是否已经安装
node -v
2.初始化node.js环境
node init
3.安装
相关文档 GraphQL 最简单的方法是使用 Express(一个 Node.js 上流行的 web 应用框架)
npm install express express-graphql graphql --save
4.新建server.js,将代码复制进去
var express = require('express');
//注意有个小坑,使用var定义会报错,这里需要修改成const
//var graphqlHTTP = require('express-graphql');
const { graphqlHTTP } = require('express-graphql');
var { buildSchema } = require('graphql');
// 使用 GraphQL Schema Language 创建一个 schema
var schema = buildSchema(`
type Query {
hello: String
}
`);
// root 提供所有 API 入口端点相应的解析器函数
var root = {
hello: () => {
return 'Hello world!';
},
};
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at http://localhost:4000/graphql');
5.运行
node server.js
6.在浏览器中访问,大功告成
http://localhost:4000/graphql