forked from apollographql/supergraph-demo-fed2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
products.js
99 lines (90 loc) · 3.52 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Open Telemetry (optional)
const { ApolloOpenTelemetry } = require('supergraph-demo-opentelemetry');
if (process.env.APOLLO_OTEL_EXPORTER_TYPE) {
new ApolloOpenTelemetry({
type: 'subgraph',
name: 'products',
exporter: {
type: process.env.APOLLO_OTEL_EXPORTER_TYPE, // console, zipkin, collector
host: process.env.APOLLO_OTEL_EXPORTER_HOST,
port: process.env.APOLLO_OTEL_EXPORTER_PORT,
}
}).setupInstrumentation();
}
const { ApolloServer, gql } = require('apollo-server');
const { buildSubgraphSchema, printSubgraphSchema } = require('@apollo/subgraph');
const { readFileSync } = require('fs');
const { printSchema } = require('graphql');
const port = process.env.APOLLO_PORT || 4000;
// Data sources
const products = [
{ id: 'converse-1', sku: 'converse-1', package: 'converse', name: 'Converse Chuck Taylor', oldField: 'deprecated'},
{ id: 'vans-1', sku: 'vans-1', package: 'vans', name: 'Vans Classic Sneaker', oldField: 'deprecated'},
]
const variationByProduct = [
{ id: 'converse-1', variation: { id: 'converse-classic', name: 'Converse Chuck Taylor'}},
{ id: 'vans-1', variation: { id: 'vans-classic', name: 'Vans Classic Sneaker'}},
]
const userByProduct = [
{ id: 'converse-1', user: { email: '[email protected]', totalProductsCreated: 1099}},
{ id: 'vans-1', user: { email: '[email protected]', totalProductsCreated: 1099}},
]
// GraphQL
const typeDefs = gql(readFileSync('./products.graphql', { encoding: 'utf-8' }));
const resolvers = {
Query: {
allProducts: (_, args, context) => {
return products;
},
product: (_, args, context) => {
return products.find(p => p.id == args.id);
}
},
ProductItf: {
__resolveType(obj, context, info){
return 'Product';
},
},
Product: {
variation: (reference) => {
return new Promise(r => setTimeout(() => {
if (reference.id) {
const variation = variationByProduct.find(p => p.id == reference.id).variation;
r(variation);
}
r({ id: 'defaultVariation', name: 'default variation' });
}, 1000));
},
dimensions: () => {
return { size: "1", weight: 1 }
},
createdBy: (reference) => {
if (reference.id) {
return userByProduct.find(p => p.id == reference.id).user;
}
return null;
},
reviewsScore: () => {
return 4.5;
},
__resolveReference: (reference) => {
if (reference.id) return products.find(p => p.id == reference.id);
else if (reference.sku && reference.package) return products.find(p => p.sku == reference.sku && p.package == reference.package);
else return { id: 'rover', package: '@apollo/rover', ...reference };
}
}
}
const schema = buildSubgraphSchema({ typeDefs, resolvers });
// console.log(`---------------------------------------`);
// console.log(`🚀 subgraph-js::printSubgraphSchema 🚀`);
// console.log(`---------------------------------------`);
// console.log(printSubgraphSchema(schema));
//
// console.log(`---------------------------------------`);
// console.log(`🚀 graphql::printSchema 🚀`);
// console.log(`---------------------------------------`);
// console.log(printSchema(schema));
const server = new ApolloServer({ schema: schema });
server.listen( {port: port} ).then(({ url }) => {
console.log(`🚀 Products subgraph ready at ${url}`);
}).catch(err => {console.error(err)});