-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathschema.js
167 lines (146 loc) · 3.78 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
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
const immutable = require('immutable')
const graphqlFields = require('graphql-fields')
const graphqlTools = require('graphql-tools')
const {request_bus, response_bus} = require('./message_bus')
const Mpn = `{
manufacturer : String!
part : String!
}`
const Sku = `{
vendor : String!
part : String!
}`
const typeDefs = `
type Mpn ${Mpn}
input MpnInput ${Mpn}
type Sku ${Sku}
input SkuInput ${Sku}
input MpnOrSku {mpn: MpnInput, sku: SkuInput}
type Query {
part(mpn: MpnInput, sku: SkuInput): Part
match(parts: [MpnOrSku]!) : [Part]!
search(term: String!): [Part]!
}
type Part {
mpn : Mpn
image : Image
datasheet : String
description : String
specs : [Spec]
type : String
offers(from: [String]) : [Offer]
}
type Offer {
sku : Sku
prices : Prices
image : Image
description : String
specs : [Spec]
in_stock_quantity : Int
stock_location : String
moq : Int
multipack_quantity : Int
product_url : String
}
type Prices {
USD: [[Float]]
EUR: [[Float]]
GBP: [[Float]]
SGD: [[Float]]
}
type Image {
url : String
credit_string : String
credit_url : String
}
type Spec {
key : String
name : String
value : String
}
`
const resolvers = {
Query: {
part(_, {mpn, sku}, __, info) {
const fields = getFields(info)
return runPart({mpn, sku, fields})
},
match(_, {parts}, __, info) {
const fields = getFields(info)
return Promise.all(parts.map(part => runPart({fields, ...part})))
},
search(_, {term}, __, info) {
const fields = getFields(info)
term = term.trim()
if (!term) {
return []
}
return run({term, fields})
},
},
}
function runPart({mpn, sku, fields}) {
console.info(
`got request for ${(mpn && JSON.stringify(mpn)) ||
(sku && JSON.stringify(sku))}`
)
if (!(mpn || sku)) {
return Promise.reject(Error('Mpn or Sku required'))
}
if (sku && sku.vendor !== 'Digikey') {
sku.part = sku.part.replace(/-/g, '')
}
return run({mpn, sku, fields})
}
function makeId() {
this.id = this.id || 1
return this.id++
}
function run(query) {
const id = makeId()
query.id = id
query = immutable.fromJS(query)
const time_stamped = immutable.Map({
query,
time: Date.now(),
})
return new Promise((resolve, reject) => {
response_bus.once(id, r => {
if (query.get('term')) {
r = r.filter(x => x).filter(x => x.get('mpn'))
} else if (!r.get('mpn')) {
return resolve()
}
console.info(
`request for ${query.get('term') ||
query.getIn(['mpn', 'part']) ||
query.getIn(['sku', 'part'])} took ${Date.now() -
time_stamped.get('time')} ms`
)
resolve(r.toJS())
})
request_bus.emit('request', time_stamped)
})
}
const offerFields = immutable.List.of('prices', '__arguments')
function getFields(info) {
let fields = graphqlFields(info, {}, {processArguments: true})
// only keep things that will actually change partinfo behaviour
fields = immutable
.fromJS(fields)
.filter((_, k) => k === 'offers')
.update(
'offers',
offers => offers && offers.filter((_, k) => offerFields.includes(k))
)
.filter(offers => offers.size > 0)
// coerce and sort to help caching
// XXX needs to be updated if more arguments are added
fields = fields.updateIn(
['offers', '__arguments', 0, 'from', 'value'],
from =>
from && (typeof from === 'string' ? immutable.List.of(from) : from.sort())
)
return fields
}
module.exports = graphqlTools.makeExecutableSchema({typeDefs, resolvers})