-
Notifications
You must be signed in to change notification settings - Fork 44
/
products.js
57 lines (53 loc) · 2.16 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
// 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 } = require('@apollo/subgraph');
const { readFileSync } = require('fs');
const port = process.env.APOLLO_PORT || 4000;
const products = [
{ id: 'apollo-federation', sku: 'federation', package: '@apollo/federation', variation: "OSS" },
{ id: 'apollo-studio', sku: 'studio', package: '', variation: "platform" },
]
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);
}
},
Product: {
variation: (reference) => {
if (reference.variation) return { id: reference.variation };
return { id: products.find(p => p.id == reference.id).variation }
},
dimensions: () => {
return { size: "1", weight: 1 }
},
createdBy: (reference) => {
return { email: 'support@apollographql.com', totalProductsCreated: 1337 }
},
__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 server = new ApolloServer({ schema: buildSubgraphSchema({ typeDefs, resolvers }) });
server.listen( {port: port} ).then(({ url }) => {
console.log(`🚀 Products subgraph ready at ${url}`);
}).catch(err => {console.error(err)});