-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphqlSchema.js
More file actions
96 lines (93 loc) · 2.35 KB
/
graphqlSchema.js
File metadata and controls
96 lines (93 loc) · 2.35 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
const {
ProductType,
ProductInputType,
ProductReviewType,
ProductReviewInputType,
ProductSpecType,
ProductSpecInputType } = require('./api/v1/products/products.scheme');
const productCtrl = require('./api/v1/products/products.controller');
const {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLList,
GraphQLNonNull
} = require('graphql');
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
products: {
type: new GraphQLList(ProductType),
args: {},
resolve() {
return new Promise((resolve, reject) => {
productCtrl.getProducts((err, result) => {
if (err) {
reject(err);
}
resolve(result);
})
})
}
},
product: {
type: ProductType,
args: {
code: { type: GraphQLString }
},
resolve(parentValue, args) {
return new Promise((resolve, reject) => {
productCtrl.findProductByCode(args.code, (err, result) => {
if (err) {
reject(err);
}
resolve(result);
})
})
}
}
}
}),
mutation: new GraphQLObjectType({
name: 'Mutation',
fields: {
addNewProduct: {
type: ProductType,
args: {
product: { type: new GraphQLNonNull(ProductInputType) }
},
resolve(parentValue, args) {
return new Promise((resolve, reject) => {
productCtrl.addNewProduct(args.product, (err, result) => {
if (err) {
reject(err);
}
resolve(result);
})
})
}
},
submitReview: {
type: new GraphQLList(ProductReviewType),
args: {
code: { type: new GraphQLNonNull(GraphQLString) },
review: { type: new GraphQLNonNull(ProductReviewInputType) }
},
resolve(parentValue, args) {
return new Promise((resolve, reject) => {
productCtrl.submitReview(args.code, args.review, (err, result) => {
if (err) {
reject(err);
}
resolve(result.reviews);
})
})
}
}
}
})
})
module.exports = schema;