-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproducts.js
83 lines (76 loc) · 1.55 KB
/
products.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
const { ApolloServer } = require('apollo-server')
const { buildFederatedSchema } = require('@apollo/federation')
const { post } = require('httpie')
const { parse } = require('graphql')
const typeDefs = /* GraphQL */ `
extend type Query {
topProducts(first: Int = 5): [Product]
}
type Product @key(fields: "upc") {
upc: String!
name: String
price: Int
weight: Int
}
`
async function main() {
// Push schema to registry
await post(`http://localhost:3000/schema/push`, {
body: {
typeDefs: typeDefs,
graphName: 'my_graph',
serviceName: 'products',
routingUrl: 'http://localhost:4003/graphql',
},
})
startServer()
}
function startServer() {
const resolvers = {
Product: {
__resolveReference(object) {
return products.find((product) => product.upc === object.upc)
},
},
Query: {
topProducts(_, args) {
return products.slice(0, args.first)
},
},
}
const server = new ApolloServer({
schema: buildFederatedSchema([
{
typeDefs: parse(typeDefs),
resolvers,
},
]),
})
server.listen({ port: 4003 }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
})
const products = [
{
upc: '1',
name: 'Table',
price: 899,
weight: 100,
},
{
upc: '2',
name: 'Couch',
price: 1299,
weight: 1000,
},
{
upc: '3',
name: 'Chair',
price: 54,
weight: 50,
},
]
}
main().catch((err) => {
console.error(err)
process.exit(1)
})