1. You have to make sure that node.js has been installed
Open cmd input to see if it has been installed
node -v
2. Initialize the node.js environment
node init
3. Installation
Related documentation GraphQL The easiest way is to use Express (a popular web application framework on Node.js)
npm install express express-graphql graphql --save
4. Create a new server.js and copy the code into it
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. Run
node server.js
6. Visit in the browser and you're done
http://localhost:4000/graphql