1、什么是graphql
graphql 是一个用来管理api接口的工具。
2、为什么使用graphql
管理接口返回的数据,有些数据前端有时候是不需要的,有时候又需要,使用graphql管理接口数据,减少后端逻辑。
3、graphql的使用(采用koa环境)
npm i koa mongoose koa-mount graphql koa-graphql -S复制代码
以上就是需要安装的基础npm包
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
ctx.body = "hello world!";
});
app.listen(9000, ()=>{
console.log('server is start...');
});复制代码
koa的基本使用,然后加入graphql
const graphqlHttp = require('koa-graphql');
const mount = require('koa-mount');
const schema = require('./graphql/schema');
app.use(mount('/graphql', graphqlHttp({
schema: schema,
graphiql: true
})));复制代码
graphql/schema.js
const { GraphQLSchema, GraphQLObjectType, GraphQLString } = require('graphql');
const queryObj = new GraphQLObjectType({
name:"myQuery",
fields:{
hello:{
name:"hello word test",
type: GraphQLString,
args:{
name: GraphQLString,
defaultValue:''
},
async resolve(root, args, request){
return `hello word! ${args.name}`; }
}
}});
module.exports = new GraphQLSchema({
query: queryObj
});复制代码
一个graphql的基本使用就完成了,了解下基本的概念
GraphqlObjectType 是 graphql 定义的对象类型,包括name, description,fields 三个属性,其中name, description 为非必填,fields是解析函数,可理解为查询方法。
每个fields 又有name, descrition, type, args, resolve参数, 这里的type是返回的数据类型,args为参数,resolve为具体的处理方法。
结果验证
这就是graphql的入门基础了。下面再介绍几个列子
在schema.js中添加
fields:{
hello:{},
person:{
type: new GraphQLObjectType({
name:"person",
fields:{
name:{
type: GraphQLString
}, age: {
type: GraphQLInt
},
sex: {
type: GraphQLBoolean
}
}
}),
args:{
name:{
type: GraphQLString
}
},
async resolve(preventDefault, args, request){
return {
name: args.name,
age: args.name.length,
sex: Math.random() > 0.5
}
}
}}复制代码
以上就是graphql的基础使用内容,下面我们引入mongoose,实现数据查询,增加
db.js
const mongoose = require('mongoose');
mongoose.set('debug', true);
mongoose.connect('mongodb://127.0.0.1:27017/demo');
mongoose.connection.on('disconnected', ()=>{
mongoose.connect('mongodb://127.0.0.1:27017/demo');
});
mongoose.connection.on('err', ()=>{
console.info(err);
});
mongoose.connection.on('open', ()=>{
console.log('Connected to MongoDB ', 'mongodb://127.0.0.1:27017/demo')
});
module.exports = mongoose;复制代码
infoSchema.js
const mongoose = require('./db');
const InfoSchema = new mongoose.Schema({
name: {
type: String,
minlength: 3
},
hobby:{
type: String,
},
sex:{
type: Number,
default: 0
},
age:{
type: Number,
default: 0
},
createAt:{
type: Date,
default: Date.now
}
});
mongoose.model('Info', InfoSchema);复制代码
schema.js
const { GraphQLSchema, GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLString, GraphQLBoolean, GraphQLInt } = require('graphql');
const mongoose = require('mongoose');
const info = require('../db/infoSchema');
const objSchema = mongoose.model('Info');
const infoType = new GraphQLObjectType({
name:'info',
fields:{
name:{
type: GraphQLString
},
hobby:{
type: GraphQLString
},
sex:{
type: GraphQLInt
},
age:{
type: GraphQLInt
}
}});
const queryObj = new GraphQLObjectType({
name:"myQuery",
fields:{
infos:{
type: new GraphQLList(infoType),
args: {},
async resolve(preventDefault, args, request){
return await objSchema.find().exec();
}
},
info:{
type: new GraphQLList(infoType),
args:{
name:{
type: GraphQLString
},
hobby:{
type: GraphQLNonNull(GraphQLString)
},
sex:{
type: GraphQLInt
},
age:{
type: GraphQLInt
}
},
async resolve(preventDefault, args, request){
console.log('4444444444');
const Entity = new objSchema({
name: args.name,
hobby: args.hobby,
sex: args.sex,
age: args.age
});
const res = await Entity.save();
console.log(res);
return res;
}
}
}});
module.exports = new GraphQLSchema({
query: queryObj
});