forked from jmandel/gra-fhir-ql
-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema.js
405 lines (345 loc) · 11.5 KB
/
schema.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
var fs = require('fs');
var graphql = require('graphql');
var merge = require('merge');
var Promise = require('es6-promise').Promise;
var request = require('request');
var util = require('util');
var GraphQLEnumType = graphql.GraphQLEnumType;
var GraphQLInterfaceType = graphql.GraphQLInterfaceType;
var GraphQLObjectType = graphql.GraphQLObjectType;
var GraphQLList = graphql.GraphQLList;
var GraphQLNonNull = graphql.GraphQLNonNull;
var GraphQLSchema = graphql.GraphQLSchema;
var GraphQLString = graphql.GraphQLString;
var GraphQLInt = graphql.GraphQLInt;
var GraphQLBoolean = graphql.GraphQLBoolean;
var GraphQLFloat = graphql.GraphQLFloat;
var GraphQLUnionType = graphql.GraphQLUnionType;
var fhirSearchParamFromField = {};
var searchParams = clone(require("./search-parameters.json"))
.entry.map(plain).map(function(p){
p.fieldArgName = asFieldArgName(p.name);
return p;
});
function asFieldArgName(s){
var ret = s.replace(/-/g, "_");
if (ret[0] === '_'){
ret = ret.slice(1);
}
fhirSearchParamFromField[ret] = s;
return ret;
}
function asFhirSearchParam(k){
return fhirSearchParamFromField[k];
}
var searchParamsByType = searchParams.reduce(function(coll, item){
if (item.base[0] === item.base[0].toUpperCase()) {
coll[item.base] = coll[item.base] || [];
coll[item.base].push(item);
}
return coll;
}, {});
Object.keys(searchParamsByType).forEach(function(k){
searchParamsByType[k].push({name: "_id", fieldArgName: "id"});
});
// searchParamsByType['Patient'][55]
function clone(o){
return JSON.parse(JSON.stringify(o));
}
function plain(r){
delete r.resource.text;
return r.resource;
}
function addItem(coll, item){
coll[item.path] = item;
return coll;
}
function firstToUpper(s){
return s[0].toUpperCase() + s.substr(1);
}
function handleX(e){
if (e.path.match(/\[x\]/)){
return e.type.map(function(t){
var oneType = clone(e);
oneType.path = oneType.path.replace("[x]", firstToUpper(t.code));
oneType.type = [t];
return oneType;
});
} else {
return e;
}
}
Array.prototype.flatMap = function(lambda) {
return Array.prototype.concat.apply([], this.map(lambda));
};
var structures = {};
function importStructures(file, structures) {
return JSON.parse(JSON.stringify(require(file))).
entry.
map(plain).
filter(function(item){return !!item.snapshot && item.snapshot.element.length> 0;}).
flatMap(function(sd){ return sd.snapshot.element; }).
flatMap(handleX).
reduce(addItem, structures);
}
structures = importStructures("./profiles-resources.json", structures);
structures = importStructures("./profiles-types.json", structures);
Object.keys(structures).forEach(function(item){
var path = structures[item].path;
if (path.match(/\.contained$/)) return;
var prefix = path.split(".").slice(0,-1).join(".");
if (prefix.length === 0) return;
structures[prefix].children = structures[prefix].children || [];
structures[prefix].children.push(path);
});
structures.code = {
"path":"code",
"short":"Primitive Type id",
"definition":"Short code (no def)",
"comments":"RFC 4122",
"min":0,
"max":"*",
"base":{"path":"string","min":0,"max":"*"},
"type":[{"code":"Element"}],
"children":["code.extension","code.value","code.id"]
};
allResourceNames = Object.keys(structures).
filter(function(k){
return structures[k].type && structures[k].type[0].code === "DomainResource";
});
function resourceFromUri(p){
return p.split("/").slice(-1)[0];
}
var namesToPaths = Object.keys(structures).
filter(function(s){
s = structures[s];
return !!s.name;
}).
map(function(s){
s = structures[s];
return [s.path.split(".")[0] + " " + s.name, s.path];
}).
reduce(function(coll, item){
coll[item[0]] = item[1];
return coll;
}, {});
function makeSchema (server) {
if(server.slice(-1)[0] !== '/') server += '/';
var gtypes = {};
['time', 'base64Binary', 'instant', 'uri', 'string', 'dateTime', 'date', 'xhtml'].
forEach(function(t){
gtypes[t] = GraphQLString;
});
gtypes.decimal = GraphQLFloat;
gtypes.boolean = GraphQLBoolean;
gtypes.integer = GraphQLInt;
Object.keys(structures).
sort(function(a, b){
return a.replace(/[^\.]/gi, "").length - b.replace(/[^\.]/gi, "").length;
}).
forEach(toGtype);
var dtypes = JSON.parse(JSON.stringify(require("./profiles-types.json"))).
entry.
map(plain).
filter(function(x){return x.kind=='datatype' && !!x.constrainedType;}).
forEach(function(t){
gtypes[t.id] = gtypes[t.constrainedType];
});
function typeOfResource(r){
var ret = r.resourceType;
var ret = gtypes[ret];
console.log("resolved to", r.resourceType);
return ret;
}
function toGtype(path){
var struct = structures[path];
var ret;
if (struct.type === undefined){
ret = null;
} else if (struct.type.length > 1) {
ret = new GraphQLUnionType({
name: path,
resolveType: typeOfResource,
types: struct.type.
flatMap(function(t) {
if (t.code === 'Reference') {
return t.profile.
map(function(p){
return resourceFromUri(p);
});
} else {
return t.code;
}
}).
filter(function(r){return ['any', 'Resource'].indexOf(r) === -1;}).
map(function(r){
// console.log("map", path, r, !!cache[r])
return gtypes[r];
})
});
} else {
ret = new GraphQLObjectType({
name: struct.path,
description: struct.definition,
fields: function(){
//console.log("field sfor", struct)
return (struct.children || []).map(function(subpath){
var name = subpath.split(".").slice(-1)[0];
var substruct = structures[subpath];
if (!!substruct.nameReference){
subpath = namesToPaths[subpath.split(".")[0] + " " + substruct.nameReference];
substruct = structures[subpath];
}
var wrapList = (substruct.max === '*') ? function(x){return new GraphQLList(x);} : function(x){return x;};
// console.log("eval substr", substruct, substruct.type)
var nextType = gtypes[subpath] || 'default no such ' + path + subpath;
var isReference = substruct.type.
map(function(t){return t.code;}).
filter(function(c){return c === "Reference";}).
length > 0;
if (substruct.type.length === 1 && substruct.type[0].code == 'Reference') {
var profileUrl = substruct.type[0].profile ? substruct.type[0].profile[0] : "/any" ;
nextType = gtypes[resourceFromUri(profileUrl)];
} else if (substruct.type.length === 1 && substruct.type[0].code!=="Element" && substruct.type[0].code!=="BackboneElement" ){
var code = substruct.type[0].code;
if (code === '*') {
code = 'Resource';
}
nextType = gtypes[code] || "one-type no such " +code +"(code)" + path + subpath +substruct.type[0].code;
}
return [name, {
type: wrapList(nextType),
description: substruct.definition,
resolve: isReference ? resolveByGet : undefined
}];
}).reduce(function(coll, item){
coll[item[0]] = item[1];
return coll;
}, extraFieldsFor(struct.path));
}
});
}
gtypes[path] = gtypes[path] || ret;
return ret;
}
function resolveBySearch(root, args, ctx){
globalRoot = root;
globalArgs = args;
globalCtx = ctx;
var resource = globalCtx.returnType.ofType.name;
var urlParams = Object.keys(args).reduce(function(coll, item){
var k = encodeURIComponent(item + (args[item].length === 1 ? '' : ":" + args[item][0]));
k = asFhirSearchParam(k);
var v = encodeURIComponent(args[item].length === 1 ? args[item][0] : args[item][1] );
coll.push(k + '=' + v);
return coll;
}, (root && root.extraArgs || [])).join("&");
// console.log("params", urlParams);
var promise = new Promise(function(resolve, reject){
var url = server + resource + "?" + urlParams;
console.log("search GET " + url)
req.get(url, function(err, response, body){
body = JSON.parse(body);
var matches = body.entry ? body.entry.map(function(e){return e.resource;}) : [];
//console.log("Matches", matches)
resolve(matches);
});
});
return promise;
}
function resolveByGet(root, args, ctx){
console.log("Resolve by get ", root.resourceType, root.id);
byGetArgs = {root:root, args:args, ctx:ctx};
var refs = root[ctx.fieldName];
if(!util.isArray(refs)) refs = [refs];
var promises = refs.map(function(ref){
return new Promise(function(resolve, reject){
var url = server + ref.reference;
console.log("read GET " + url)
req.get(url, function(err, response, body){
body = JSON.parse(body);
//console.log("Matches", body)
resolve(body);
});
});
});
return Promise.all(promises).then(function(r){
if (ctx.returnType instanceof GraphQLList) {
return r;
}
if (r.length > 1) {throw "Got list when expected a singleton " + root + "," + ctx.fieldName;}
if (r.length === 1) {return r[0];}
console.log("No result at");
return null;
});
}
gtypes.Resource = gtypes.any = new GraphQLUnionType({
name: "Any Resource",
resolveType: typeOfResource,
types: allResourceNames.map(function(r){ return gtypes[r];})});
var withFields;
function extraFieldsFor(path){
if (path === "Patient"){
return {
"with": {
"description": "Resources related to this patient",
"type": gtypes.root,
"resolve": function(root, args, ctx) {
withFields = {root: root, args: args, ctx: ctx};
// console.log("Resolve with fields at", root)
return {"extraArgs": ["patient=" + root.id]};
}
}
};
}
if (path === "Encounter"){
return {
"with": {
"description": "Resources related to this encounter",
"type": gtypes.root,
"resolve": function(root, args, ctx) {
withFields = {root: root, args: args, ctx: ctx};
//console.log("Resolve with fields at", root)
return {"extraArgs": ["encounter=" + root.id]};
}
}
};
}
return {};
}
gtypes.SearchParam = new GraphQLList(GraphQLString);
function rootFields(){
return allResourceNames.
map(function(r){
return [
r, {
type: new GraphQLList(gtypes[r]),
description: "Get all resources of type " + r,
args: (searchParamsByType[r] || []).reduce(function(coll, item){
// console.log("Check", r, item.name, item.type, !!searchParamGtypes[item.type])
coll[item.fieldArgName] = {
description: item.description,
type: gtypes.SearchParam
};
return coll;
}, { }),
resolve: resolveBySearch
}
];}).
reduce(function(coll, item){
coll[item[0]] = item[1];
return coll;
}, {});
}
gtypes.root = new GraphQLObjectType({
name: 'Query',
fields: rootFields
});
var req = request.defaults({
headers: {'Accept': 'application/json'}
});
return new GraphQLSchema({
query: gtypes.root
});
}
module.exports = makeSchema;