一、在下面代码中添加如下代码,然后创建一个public文件夹,在文件夹中建一个html文件,然后发送post请求获取数据
例如:创建的是index.html,然后执行js文件,然后访问http://localhost:3000/index.html
// 公共文件夹,供用户访问静态资源
app.use(express.static('public'))
const express = require('express');
const {buildSchema} = require('graphql');
const {graphqlHTTP} = require('express-graphql');
// 定义Schema, 查询方法和返回值类型
const schema = buildSchema(`
// Account 为自定义类型
type Account {
name: String
age: Int
sex: String
department: String
salary(city: String): Int
}
type Query {
getClassMates(classNo: Int!): [String]
account(username: String): Account
}
`)
//定义查询对应的处理器
const root = {
getClassMates({classNo}) {
const obj = {
11: ['张三', '李四', '王五'],
12: ['张飞', '曹操', '关羽']
}
return obj[classNo];
},
account({username}) {
const name = username;
const age = 12;
const sex = '男';
const department = '测试部';
const salary = ({city}) => {
if (city == '北京' || city == '上海' || city == '广州') {
return 10000;
}
return 4000;
}
return {
name,
age,
sex,
department,
salary
}
}
}
const app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true
}))
// 公共文件夹,供用户访问静态资源
app.use(express.static('public'))
app.listen(3000);