-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (38 loc) · 1.01 KB
/
index.js
File metadata and controls
50 lines (38 loc) · 1.01 KB
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
const express = require('express');
const { AwesomeGraphQLClient } = require('awesome-graphql-client');
const fetch = require('node-fetch');
const client = new AwesomeGraphQLClient({
endpoint:
'https://api-eu-central-1.hygraph.com/v2/ck8sn5tnf01gc01z89dbc7s0o/master',
fetch,
});
const app = express();
app.set('view engine', 'ejs');
app.get('/', async function (_, res) {
const query = `
{
products {
name
slug
}
}
`;
const { products } = await client.request(query);
res.render('index', { products });
});
app.get('/products/:slug', async function (req, res) {
const query = `
query ProductPageQuery($slug: String!) {
product(where: { slug: $slug }) {
name
description
price
}
}
`;
const { slug } = req.params;
const { product } = await client.request(query, { slug });
res.render('product', { product });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`🚀 Running on port ${PORT}`));