-
Notifications
You must be signed in to change notification settings - Fork 14
/
demo.js
65 lines (48 loc) · 1.57 KB
/
demo.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
const express = require("express");
const { ApolloServer } = require("apollo-server-express");
const staple = require("staple-api");
const ontology = {
file: "./docs/ontology.ttl"
};
const config = {
dataSources: {
default: "defaultSource",
defaultSource: {
type: "mongodb",
url: "mongodb+srv://guest:[email protected]/test",
dbName: "staple",
collectionName: "staple",
description: "MongoDB Atlas Demo instance"
}
}
};
async function Demo() {
// creating an instance of Staple API
const stapleApi = await staple(ontology, config);
const schema = stapleApi.schema;
// creating the list of all people and their names for FE indexing
let people = []
console.log("Fetching people's names...")
await stapleApi.graphql('{ Person { _id label } }').then((response) => {
response.data.Person.forEach(element => {
people.push({id: element._id, text:element.label})
});
console.log("...all fetched!")
});
// exnabling FE, Staple API and people endpoint via express server
const app = express();
app.use(express.static("docs"))
app.get('/people', function (req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.send(people)
})
const server = new ApolloServer({
schema
});
const path = "/graphql";
server.applyMiddleware({ app, path });
app.listen({ port: 5000 }, () =>
console.log("🚀 Server ready")
);
}
Demo()