-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.js
82 lines (77 loc) · 2.07 KB
/
schema.js
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
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
GraphQLList,
GraphQLNonNull
} = require('graphql');
const env = require('./env.js');
const axios = require('axios');
const {ProductList, Token, Sample} = require('./types');
const md5Hash = require('md5-hash');
const store = require('data-store')('site');
const sample = [
{name:'Mike'},
{name:'Shen'},
{name:'gru'},
{name:'helio'}
]
//Root Query
const RootQuery = new GraphQLObjectType({
name:'RootQueryType',
fields:{
//get Token
token:{
type:Token,
resolve() {
if (!store.hasOwn("site_token")) {
return axios.post(env.url.token_url, {
deviceid: env.deviceId,
hashkey: md5Hash.default(env.SALT + env.deviceId)
})
.then((res) => {
store.set("site_token", res.data.token);
return res.data;
})
}
else{
// console.log(store.get("site_token"));
return {
token:store.get("site_token")
}
}
}
},
productList:{
type:new GraphQLList(ProductList),
args:{
cat_id: {type: GraphQLString},
hub_id: {type: GraphQLInt}
},
resolve(parentValue, args){
// return products;
return axios.get(env.url.pr_list+`cat_id=${args.cat_id}&hub_id=${args.hub_id}`,
{headers:{token:store.get("site_token")}})
.then((res)=>{
let resp = [];
console.log("\n----------------\n",res.data,"\n----------------\n");
if(res.data.status == "success") {
res.data.data.map((x) => {
let temp = {};
Object.assign(temp, x.product_master);
Object.assign(temp, x.product_merchantdising);
Object.assign(temp, x.product_pricing);
Object.assign(temp, x.product_inventory);
resp.push(temp);
});
return resp;
}
})
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery
});