/
schemaMgr.ts
148 lines (134 loc) · 4.03 KB
/
schemaMgr.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const makeExecutableSchema = require("graphql-tools").makeExecutableSchema;
import { readFileSync } from "fs";
import { QueryResolverMgr } from "./queryResolverMgr";
import { EdgeResolverMgr } from "./edgeResolverMgr";
import { SwarmMgr } from "./swarmMgr";
import { HashResolverMgr } from "./hashResolverMgr";
export class SchemaMgr {
queryResolverMgr: QueryResolverMgr;
edgeResolverMgr: EdgeResolverMgr;
hashResolverMgr: HashResolverMgr;
constructor(
queryResolverMgr: QueryResolverMgr,
edgeResolverMgr: EdgeResolverMgr,
hashResolverMgr: HashResolverMgr
) {
this.queryResolverMgr = queryResolverMgr;
this.edgeResolverMgr = edgeResolverMgr;
this.hashResolverMgr = hashResolverMgr;
}
_getTypeDefs() {
return readFileSync(__dirname + "/schema.graphqls", "utf8");
}
_getResolvers() {
return {
Query: {
/**
* Retorna la identidad del usuario dueño del token
*/
me: async (parent: any, args: any, context: any, info: any) => {
const res = await this.queryResolverMgr.me(context.headers);
return res;
},
/**
* Retorna el hash de recuperacion para obtener el backup de la base de datos en swarm
* solo funciona si el did se corresponde con el del dueño del token o el didi-server
*/
hash: async (parent: any, args: any, context: any, info: any) => {
const res = await this.hashResolverMgr.getHash(context.headers, args.did);
return res ? res.hash : null;
},
// Retorna el jwt a partir del hash
/*
edgeByHash: async (parent: any, args: any, context: any, info: any) => {
const res = await this.queryResolverMgr.edgeByHash(
context.headers,
args.hash,
args.did
);
return res;
},
*/
/**
* Permite validar si el certificado se encuentra en mouro
* solo funciona si el did se corresponde con el del dueño del token o el didi-server
*/
edgeByJwt: async (parent: any, args: any, context: any, info: any) => {
const res = await this.queryResolverMgr.edgeByJwt(
context.headers,
args.edgeJWT,
args.did
);
return res;
},
/**
* Retorna los jwts de los certificados que posee el dueño del token
*/
findEdges: async (parent: any, args: any, context: any, info: any) => {
// console.log(context.headers);
const res = await this.queryResolverMgr.findEdges(context.headers, args);
return res;
}
},
Mutation: {
/**
* Almacena un nuevo jwt en mouro
* solo funciona si el did se corresponde con el del dueño del token o el didi-server
*/
addEdge: async (parent: any, args: any, context: any, info: any) => {
const res = await this.edgeResolverMgr.addEdge(
context.headers,
args.edgeJWT,
args.did
);
const filename = this.getFilename(args.did);
const hash = await SwarmMgr.uploadFile(filename);
if (hash) {
await this.hashResolverMgr.addHash(context.headers, hash, args.did);
}
return res;
},
/**
* Revoca un jwt previamente emitido
* solo funciona si el did se corresponde con el del dueño del token o el didi-server
*/
removeEdge: async (parent: any, args: any, context: any, info: any) => {
const cert = await this.queryResolverMgr.edgeByHash(
context.headers,
args.hash,
args.did
);
if (cert) {
const res = await this.edgeResolverMgr.removeEdge(
context.headers,
args.hash,
args.did
);
const filename = this.getFilename(args.did);
const hash = await SwarmMgr.uploadFile(filename);
if (hash) {
await this.hashResolverMgr.addHash(context.headers, hash, args.did);
}
return res;
}
}
},
VisibilityEnum: {
TO: "TO",
BOTH: "BOTH",
ANY: "ANY"
}
};
}
getFilename(did: string) {
return process.env.SQLITE_FILE ? process.env.SQLITE_FILE : "./db/" + did;
}
getSchema() {
const typeDefs = this._getTypeDefs();
const resolvers = this._getResolvers();
return makeExecutableSchema({
typeDefs,
resolvers
});
}
}