1、什么是graphql
graphql 是一個(gè)用來管理api接口的工具。
2参淫、為什么使用graphql
管理接口返回的數(shù)據(jù),有些數(shù)據(jù)前端有時(shí)候是不需要的愧杯,有時(shí)候又需要涎才,使用graphql管理接口數(shù)據(jù),減少后端邏輯力九。
3耍铜、graphql的使用(采用koa環(huán)境)
npm i koa mongoose koa-mount graphql koa-graphql -S復(fù)制代碼
以上就是需要安裝的基礎(chǔ)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...');
});復(fù)制代碼
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
})));復(fù)制代碼
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
});復(fù)制代碼
一個(gè)graphql的基本使用就完成了跌前,了解下基本的概念
GraphqlObjectType 是 graphql 定義的對(duì)象類型棕兼,包括name, description,fields 三個(gè)屬性抵乓,其中name, description 為非必填伴挚,fields是解析函數(shù),可理解為查詢方法臂寝。
每個(gè)fields 又有name, descrition, type, args, resolve參數(shù)章鲤, 這里的type是返回的數(shù)據(jù)類型,args為參數(shù)咆贬,resolve為具體的處理方法败徊。
結(jié)果驗(yàn)證
這就是graphql的入門基礎(chǔ)了。下面再介紹幾個(gè)列子
在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
}
}
}}復(fù)制代碼
以上就是graphql的基礎(chǔ)使用內(nèi)容掏缎,下面我們引入mongoose皱蹦,實(shí)現(xiàn)數(shù)據(jù)查詢,增加
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;復(fù)制代碼
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);復(fù)制代碼
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
});