-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (53 loc) · 1.47 KB
/
index.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
require('dotenv').config()
const gateway = require('gql-gateway')
const { customerApi, invoiceApi, port } = require('./config')
const endpointsList = [
{ name: customerApi.name, url: customerApi.url },
{ name: invoiceApi.name, url: invoiceApi.url }
]
const localSchema = `
extend type Customer {
invoices: [Invoice]
}
extend type Invoice {
customer: Customer
}
`
const resolvers = {
Customer: {
invoices: {
fragment: '... on Customer {customerId}',
async resolve (invoice, args, context, info) {
const schema = await context.resolveSchema('invoiceApi')
return info.mergeInfo.delegateToSchema({
schema,
operation: 'query',
fieldName: 'getInvoicesByCustomerId',
args: { customerId: invoice.customerId },
context,
info
})
}
}
},
Invoice: {
customer: {
fragment: '... on Invoice {customerId}',
async resolve (customer, args, context, info) {
const schema = await context.resolveSchema('customerApi')
return info.mergeInfo.delegateToSchema({
schema,
operation: 'query',
fieldName: 'getCustomerById',
args: { customerId: customer.customerId },
context,
info
})
}
}
}
}
gateway({ endpointsList, resolvers, localSchema })
.then(server => server.listen(port))
.then(console.log(`Service is now running at port: ${port}`))
.catch(err => console.log(err))