forked from obetame/learn-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.js
More file actions
174 lines (163 loc) · 3.6 KB
/
schema.js
File metadata and controls
174 lines (163 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const {
GraphQLList,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
GraphQLInt,
GraphQLFloat,
GraphQLEnumType,
GraphQLNonNull
} = require('graphql');
const _ = require("underscore");
const PostsList = require('./data/posts');
const AuthorsList = require('./data/authors');
const {CommentList, ReplyList} = require('./data/comments');
const AddressList = require("./data/address");
const Post = new GraphQLObjectType({
name:"Post",
description:"一个文章",
fields:()=>({
_id:{
type:new GraphQLNonNull(GraphQLString)
},
title:{
type:new GraphQLNonNull(GraphQLString)
},
category:{
type:GraphQLString
},
layout:{
type:GraphQLString
},
content:{
type:GraphQLString
},
})
});
const AddressContent = new GraphQLObjectType({
name:"AddressContent",
description:"地址子信息",
fields:()=>({
Id:{
type:GraphQLInt
},
Code:{
type:GraphQLString
},
Name:{
type:GraphQLString
},
FirstStr:{
type:GraphQLString
},
})
});
const Address = new GraphQLObjectType({
name:"Address",
description:"地址信息",
fields:()=>({
ShortKey:{
type:GraphQLString
},
Content:{
type:new GraphQLList(AddressContent),
args:{
limit:{type:GraphQLInt}
},
resolve:(source,{limit})=>{
// console.log(source);
if(limit){
return _.first(source.Content,limit);
}
else{
return source.Content;
}
}
},
})
});
const Query = new GraphQLObjectType({
name: 'BlogSchema',
description: 'Root of the Blog Schema',
fields: () => ({
echo: {
type: GraphQLString,
description: '回应你输入的内容',
args: {
message: {type: GraphQLString}
},
resolve: function(source, {message}) {
return `hello: ${message}`;
}
},
posts:{
type:new GraphQLList(Post),
args:{
index:{type:GraphQLInt}
},
resolve:(source,args)=>{
return [PostsList[args.index]]
}
},
postsnoargs:{
type:new GraphQLList(Post),
resolve:(source)=>{
return [PostsList[0]]
}
},
address:{
type:new GraphQLList(Address),
args:{
nameKey:{type:GraphQLString}
},
resolve:(source,{nameKey})=>{
return [_.find(AddressList,item=>item.ShortKey===nameKey.toUpperCase())]
}
}
})
});
const Mutation = new GraphQLObjectType({
name:"Mutation",
description:"增删改数据",
fields:()=>({
createAddress:{
type:AddressContent,
args:{
Id:{
type:new GraphQLNonNull(GraphQLInt)
},
Code:{
type:new GraphQLNonNull(GraphQLString)
},
Name:{
type:new GraphQLNonNull(GraphQLString)
},
FirstStr:{
type:new GraphQLNonNull(GraphQLString)
}
},
resolve:(source,args)=>{
let address = Object.assign({},args);//获取数据
//改为大写
address.FirstStr = address.FirstStr.toUpperCase();
let queryData = _.find(AddressList,item=>item.ShortKey===address.FirstStr);//查找的数据
//检测是否存在FirstStr开头的
if(queryData){
// 有这个数据
//存储数据
queryData.Content.push(address);
// console.log(address)
return address;//返回新存储的数据
}
else{
return null;
}
}
}
})
})
const Schema = new GraphQLSchema({
query: Query,
mutation:Mutation
});
module.exports = Schema;