This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (53 loc) · 2 KB
/
index.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
const registry = require('./registryOnChain/registryOnChain')
const connect = require('connect');
const query = require('qs-middleware');
const fs = require("fs");
const { makeExecutableSchema } = require("graphql-tools");
const express = require("express");
const { ApolloServer, gql } = require('apollo-server-express');
const graphql = require("express-graphql");
const sofa = require("sofa-api").default;
const typeDefs = fs.readFileSync("./schema.gql", "utf8");
const resolvers = require("./resolver");
const schema = makeExecutableSchema({ typeDefs, resolvers });
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
const path = '/graphql';
server.applyMiddleware({ app, path });
app.use("/graphiql", graphql({ schema, graphiql: true }));
app.use("/api", sofa({ schema }));
app.get('/api/getContractAddress', (req, res) => {
registry.getContractAddress().then(val => {
res.json({ address: val });
})
})
app.get('/api/getServices', (req, res) => {
registry.getServices().then(val => {
res.json({ services: val });
})
})
app.get('/api/getService/:serviceId', function (req, res) {
const serviceId=parseInt(req.params["serviceId"]);
registry.getService(serviceId).then(val => {
res.json({ serviceId: serviceId, service: val });
}).catch(error => {
console.log(error);
})
})
app.get('/api/getBootstraps/:serviceId', function (req, res) {
const serviceId=parseInt(req.params["serviceId"]);
registry.getBootstraps(serviceId).then(val => {
res.json({ serviceId: serviceId, service: val});
}).catch(error => {
console.log(error);
})
})
app.listen({ port: 4000 }, () =>
{
console.log(`🚀 Apollo GraphQL Server ready at http://localhost:4000${server.graphqlPath}`);
console.log(`🚀 REST API Server ready at http://localhost:4000/api/
(e.g. http://localhost:4000/api/dapps/
http://localhost:4000/api/getContractAddress/)`);
console.log(`🚀 GraphiQL Server ready at http://localhost:4000/graphiql/ `);
}
);