-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcomposer.js
864 lines (714 loc) · 25.2 KB
/
composer.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
'use strict'
const { buildClientSchema, printSchema } = require('graphql')
const { getIntrospectionQuery } = require('graphql')
const fastify = require('fastify')
const mercurius = require('mercurius')
const { fetchSubgraphSchema, makeGraphqlRequest } = require('./network')
const { validateComposerOptions } = require('./validation')
const { QUERY_TYPE, MUTATION_TYPE, mergeTypes, getMainType, createType, createField, createFieldId } = require('./fields')
const { buildQuery, queryParentResult } = require('./query-builder')
const { collectQueries } = require('./query-lookup')
const { unwrapFieldTypeName, objectDeepClone, pathJoin, schemaTypeName } = require('./utils')
const { mergeResult } = require('./result')
const { entityKeys } = require('./entity')
const { createDefaultHeadersAdapter } = require('./headers')
const COMPOSER_SUBGRAPH_NAME = '__composer__'
/**
* @typedef {Object} ComposerField
* @property {Object} src - graphql field definition
* @property {string} typeName - field type name, for example "Author"
* @property {Resolver} resolver
*/
/**
* fields[typeName.fieldName][subgraphName] = { src, typeName, resolver }
* @typedef {Object.<string, Object.<string, ComposerField>>} ComposerFields
* @example fields['Book.id']['books-subgraph'] = { src, typeName, resolver }
*/
/**
* @typedef {Object} ComposerType
* @property {Object} src - graphql type definition
* @property {Map<string, ComposerField>} fields - type field's, where the key is the field name
* @property {Entity} entity
*/
/**
* types[typeName][subgraphName] = { src, fields, entity }
* @typedef {Object.<string, Object.<string, ComposerType>>} ComposerTypes
* @example types['Book']['books-subgraph'] = { src, fields, entity }
*/
class Composer {
constructor (options = {}) {
this.mergedSchema = { _built: false }
this.schemas = []
this.mainTypes = new Set()
this.resolvers = Object.create(null)
const v = validateComposerOptions(options)
this.logger = v.logger
this.schemaOptions = {
[QUERY_TYPE]: {
schemaPropertyName: 'queryType',
name: v.queryTypeName
},
[MUTATION_TYPE]: {
schemaPropertyName: 'mutationType',
name: v.mutationTypeName
}
}
this.addEntitiesResolvers = v.addEntitiesResolvers
this.onSubgraphError = v.onSubgraphError
this.subgraphs = v.subgraphs
this.defaultArgsAdapter = v.defaultArgsAdapter
this.headersAdapter = createDefaultHeadersAdapter()
if (this.addEntitiesResolvers) {
this.entities = v.entities
} else {
this.entities = undefined
}
/** @type ComposerTypes */
this.types = {}
/** @type ComposerFields */
this.fields = {}
this.aliases = {}
}
toSchema () {
const schema = {
queryType: undefined,
mutationType: undefined,
types: [],
// TODO support directives
directives: []
}
for (const mainType of this.mainTypes.values()) {
const s = this.schemaOptions[mainType]
schema[s.schemaPropertyName] = { name: s.name }
}
for (const type of this.mergedSchema.types.values()) {
schema.types.push(type.src)
}
return { __schema: schema }
}
toSdl () {
return printSchema(buildClientSchema(this.toSchema()))
}
async compose () {
this.schemas = await this.fetchSubgraphSchemas()
for (let i = 0; i < this.schemas.length; ++i) {
this.mergeSchema(this.schemas[i])
}
if (this.addEntitiesResolvers) {
await this.setupComposerSubgraph()
}
this.buildMergedSchema()
}
// TODO return a copy to avoid writes on resolvers
getResolvers () {
return { ...this.resolvers }
}
// TODO memoize
deferredSubgraph (fieldId) {
const field = this.fields[fieldId]
if (!field) {
throw new Error('DEFERRED_SUBGRAPH_NO_FIELD')
}
// TODO can the field be resolved by multiple subgraphs?
const subgraphNames = Object.keys(field)
if (subgraphNames.length === 1) {
return subgraphNames[0]
}
// ignore COMPOSER_SUBGRAPH_NAME because can't resolve fields
return subgraphNames.filter(s => s !== COMPOSER_SUBGRAPH_NAME)[0]
}
buildMergedSchema () {
this.mergedSchema = {
types: new Map(),
_built: false
}
const typeNames = Object.keys(this.types)
// TODO handle different Query or Mutation type name between subgraphs
for (let i = 0; i < typeNames.length; ++i) {
const typeName = typeNames[i]
const subgraphNames = Object.keys(this.types[typeName])
for (let j = 0; j < subgraphNames.length; ++j) {
const subgraphName = subgraphNames[j]
const type = this.types[typeName][subgraphName]
const t = this.mergedSchema.types.get(typeName)
// TODO handle conflicts by name or type, add option to rename, mask or hide
this.mergedSchema.types.set(typeName, t ? mergeTypes(t, type) : objectDeepClone(type))
}
}
this.mergedSchema._built = true
}
/**
* setup a dry service as composer node
*/
async setupComposerSubgraph () {
const { schema, resolvers, entities } = this.resolveEntities()
// no entities to add, no need the composer subgraph
if (!schema) { return }
const instance = fastify()
instance.register(mercurius, { schema, resolvers })
instance.get('/i', (_, reply) => reply.graphql(getIntrospectionQuery()))
await instance.ready()
const subgraphName = COMPOSER_SUBGRAPH_NAME
// set entities both in composer and each subgraph
const subgraph = {
name: subgraphName,
server: {
instance,
graphqlEndpoint: '/graphql'
},
entities: new Map(Object.entries(entities))
}
this.subgraphs.set(subgraphName, subgraph)
// collect all the alias
// ! aliases works only in options.entities with "addEntitiesResolvers"
for (const entityName of Object.keys(this.entities)) {
const entity = this.entities[entityName]
// fill entities in composer
this.setEntity(entityName, entity.subgraph, entity)
this.types[entityName][entity.subgraph].entity = entity
if (entity.fkeys) {
for (const fkey of entity.fkeys) {
if (!fkey.as) { continue }
const fieldId = createFieldId(entityName, fkey.as)
this.setAlias(fieldId, subgraphName, { ...fkey, parentType: entityName, entity, mode: 'fkey' })
}
}
if (entity.many) {
for (const many of entity.many) {
if (!many.as) { continue }
const fieldId = createFieldId(entityName, many.as)
this.setAlias(fieldId, subgraphName, { ...many, parentType: entityName, entity, mode: 'many' })
}
}
}
const introspection = await instance.inject('/i')
const subgraphSchema = JSON.parse(introspection.body).data
subgraphSchema.subgraphName = subgraphName
this.mergeSchema(subgraphSchema)
// add aliases in fields and types
for (const fieldId of Object.keys(this.aliases)) {
const alias = this.aliases[fieldId][subgraphName]
const t = this.types[alias.parentType][alias.entity.subgraph]
const field = this.fields[fieldId][subgraphName].src
const f = this.addField(alias.parentType, field, alias.subgraph, t, alias.resolver)
t.fields.set(alias.as, f)
f.type = this.types[f.typeName][subgraphName]
}
}
async fetchSubgraphSchemas () {
const subgraphs = Array.from(this.subgraphs.values())
const requests = subgraphs.map((subgraph) => {
return fetchSubgraphSchema(subgraph.server)
})
const responses = await Promise.allSettled(requests)
const schemas = []
for (let i = 0; i < responses.length; ++i) {
const { status, value: introspection } = responses[i]
const subgraph = subgraphs[i]
if (status !== 'fulfilled') {
const msg = `Could not process schema for subgraph '${subgraph.name}' from '${subgraph.server.host}'`
this.onSubgraphError(new Error(msg, { cause: responses[i].reason }), subgraph.name)
continue
}
introspection.subgraphName = subgraph.name
schemas.push(introspection)
}
return schemas
}
// TODO test subgraph with different Query or Mutation names
mergeSchema ({ __schema: schema, subgraphName }) {
if (!schema) {
return
}
for (let i = 0; i < schema.types.length; ++i) {
const schemaType = schema.types[i]
const typeName = schemaType.name
// Ignore built in types
if (typeName.startsWith('__')) {
continue
}
// Query or Mutation
const mainType = getMainType(schema, schemaType)
if (mainType) {
this.mainTypes.add(mainType)
}
if (!Array.isArray(schemaType.fields)) {
this.addType(typeName, subgraphName, schemaType)
continue
}
const entity = this.getEntity(typeName, subgraphName)
const type = this.addType(typeName, subgraphName, schemaType, entity)
type.fields = new Map()
for (let i = 0; i < schemaType.fields.length; ++i) {
const field = schemaType.fields[i]
let resolver
if (mainType) {
resolver = this.createResolver({
typeName,
subgraphName,
fieldSrc: field
})
this.resolvers[typeName] ??= Object.create(null)
this.resolvers[typeName][field.name] = resolver
}
// TODO alias for conflicting types, for example
// subgraph#1: type Pizza { id: ID }
// subgraph#2: type Pizza { id: Int! }
// options: { ... subgraph#1: { entities: { Pizza: { id: { as: 'optionalId' ...
// result: type Pizza { optionalId: ID (from subgraph#1), id: Int! (from subgraph#2) }
// TODO option to hide fields
// note: type may not be available at this point, only typeName
const f = this.addField(typeName, field, subgraphName, type, resolver)
type.fields.set(field.name, f)
}
}
// fill types in fields by typename
for (const fieldId of Object.keys(this.fields)) {
for (const subgraphName of Object.keys(this.fields[fieldId])) {
const f = this.fields[fieldId][subgraphName]
f.type = this.types[f.typeName][subgraphName]
}
}
}
addType (typeName, subgraphName, type, entity) {
if (this.types[typeName] && this.types[typeName][subgraphName]) {
return this.types[typeName][subgraphName]
}
const t = createType({ name: typeName, src: type, entity })
if (!this.types[typeName]) {
this.types[typeName] = { [subgraphName]: t }
return t
}
this.types[typeName][subgraphName] = t
return t
}
addField (parentTypeName, field, subgraphName, parent, resolver) {
const fieldId = createFieldId(parentTypeName, field.name)
if (this.fields[fieldId] && this.fields[fieldId][subgraphName]) {
this.logger.warn('TODO field already exists on subgraph')
return
}
const typeName = unwrapFieldTypeName(field)
const f = createField({ name: field.name, typeName, src: field, parent, resolver })
if (!this.fields[fieldId]) {
this.fields[fieldId] = { [subgraphName]: f }
return f
}
this.fields[fieldId][subgraphName] = f
return f
}
setEntity (typeName, subgraphName, entity) {
const subgraph = this.subgraphs.get(subgraphName)
if (subgraph) {
subgraph.entities.set(typeName, entity)
}
}
getEntity (typeName, subgraphName) {
const subgraph = this.subgraphs.get(subgraphName)
if (subgraph) {
return subgraph.entities.get(typeName)
}
}
/**
* get existing types on subgraph
*/
getTypes (subgraphName) {
// TODO memoize
const types = {}
const typeNames = Object.keys(this.types)
for (let i = 0; i < typeNames.length; ++i) {
const typeName = typeNames[i]
const subgraphNames = Object.keys(this.types[typeName])
for (let j = 0; j < subgraphNames.length; ++j) {
const s = subgraphNames[j]
if (s !== subgraphName) { continue }
types[typeName] = this.types[typeName][s]
}
}
return types
}
/**
* get existing types on subgraph
*/
getFields (subgraphName) {
// TODO memoize
const fields = {}
const fieldIds = Object.keys(this.fields)
for (let i = 0; i < fieldIds.length; ++i) {
const fieldName = fieldIds[i]
const subgraphNames = Object.keys(this.fields[fieldName])
for (let j = 0; j < subgraphNames.length; ++j) {
const s = subgraphNames[j]
if (s !== subgraphName) { continue }
fields[fieldName] = this.fields[fieldName][s]
}
}
return fields
}
// an alias must:
// - exists in gql schema/s
// - have a resolver
// alias key: Type.field#sourceSubgraph where sourceSubgraph is the subgraph where it will be resolved/replaced
// note! alias.subgraph is the subgraph for the resolver, see that as alias[subgraphSource].subgraphTarget
setAlias (fieldId, subgraphName, alias) {
if (!this.aliases[fieldId]) {
this.aliases[fieldId] = { [subgraphName]: alias }
return
}
this.aliases[fieldId][subgraphName] = alias
}
// TODO memoize
// subgraphName is the subgraph that resolves the alias
getAlias (fieldId, subgraphName) {
const alias = this.aliases[fieldId]
if (!alias) { return }
return Object.values(alias).find(a => a.subgraph === subgraphName)
}
createResolver ({ typeName, subgraphName, fieldSrc }) {
return async (parent, args, context, info) => {
return this.runResolver({ typeName, subgraphName, fieldSrc, parent, args, context, info })
}
}
async runResolver ({ typeName, subgraphName, fieldSrc, parent, args, context, info }) {
const { queries, order } = this.collectQueries({
typeName,
subgraphName,
fieldSrc,
parent,
args,
context,
info
})
const result = await this.runQueries(queries, order, context)
return result[fieldSrc.name]
}
/**
* collect queries starting from the resolver fn
*/
collectQueries ({
// resolver generator
typeName, subgraphName, fieldSrc,
// resolver args
parent, args, context, info
}) {
const types = this.getTypes(subgraphName)
const fields = this.getFields(subgraphName)
const queries = new Map()
const order = new Set()
for (const queryFieldNode of info.fieldNodes) {
const fieldId = createFieldId(info.parentType.name, queryFieldNode.name.value)
// TODO createContext fn
const context = {
info,
done: [],
logger: this.logger
}
const q = collectQueries({
subgraphName,
queryFieldNode,
fieldId,
args,
types,
fields,
aliases: this.aliases,
root: true,
context
})
this.buildQueries(q, queries, order, context)
}
return { queries, order }
}
// TODO add resolution strategy here
// - traverse horizontally, collect by subgraph/parent resolution
buildQueries (collectedQueries, queries, order, context) {
for (const deferred of collectedQueries.deferreds.values()) {
// in case of alias on deferred subgraph is not possible to get the field on lookup stage
if (!deferred.queryNode.field) {
deferred.queryNode.field = deferred.queryNode.parent.field
const field = Object.values(this.fields[deferred.queryNode.fieldId])[0]
deferred.keys = entityKeys({ field, entity: field.typeName })
} else {
deferred.keys = entityKeys({ field: deferred.queryNode.field, entity: deferred.queryNode.field.typeName })
}
const deferredQueriesBatch = this.buildDeferredQueries(deferred, context)
for (const deferredQueries of deferredQueriesBatch) {
this.buildQueries(deferredQueries, queries, order, context)
}
}
// fill queries info
for (let [path, query] of collectedQueries.queries) {
const currentQuery = queries.get(path)
if (currentQuery) {
query = mergeQueries(currentQuery, query)
}
// TODO calculate if entity keys are needed: keys are not needed when there are no deferred queries depending on quering entity
// TODO add entities data here: node, parent, as, resolver...
query.query.selection.push(
...entityKeys({ field: query.field, subgraph: query.subgraphName }).map(key => ({ key })))
// append deferred queries
queries.set(path, query)
order.add(path)
}
}
buildDeferredQueries (deferred, context) {
this.logger.debug(' > composer.buildDeferredQueries')
const queries = []
const subgraphs = {}
// group deferred fields by fields parent, and indirectly by subgraph
for (const { fieldName } of deferred.fields) {
const fieldId = createFieldId(deferred.queryNode.field.typeName, fieldName)
const subgraphName = this.deferredSubgraph(fieldId)
// TODO throw if no subgraph
const alias = this.getAlias(fieldId, subgraphName)
if (alias) {
this.addDeferredSubgraph(subgraphs, alias.subgraph, alias.type, deferred.keys, deferred.queryNode)
} else {
this.addDeferredSubgraph(subgraphs, subgraphName, deferred.queryNode.field.typeName, deferred.keys, deferred.queryNode)
subgraphs[subgraphName].fields.push({ fieldName })
}
}
for (const subgraphName of Object.keys(subgraphs)) {
const d = subgraphs[subgraphName]
const types = this.getTypes(subgraphName)
const fields = this.getFields(subgraphName)
const selection = d.fields.map(f => f.fieldName)
const fieldId = 'Query.' + d.resolver.name
// collectQueries on deferred subgraph
const q = collectQueries({
subgraphName,
parent: deferred.queryNode,
queryFieldNode: deferred.queryNode.queryFieldNode,
path: deferred.queryNode.path,
fieldId,
selection: selection.length > 0 ? selection : null,
types,
fields,
aliases: this.aliases,
context,
// args?
resolver: d.resolver
})
for (const query of q.queries.values()) {
query.query.selection.push({ field: d.entity.pkey })
}
queries.push(q)
}
return queries
}
addDeferredSubgraph (subgraphs, subgraphName, typeName, keys, queryNode) {
if (subgraphs[subgraphName]) { return }
const entity = this.getEntity(typeName, subgraphName)
if (!entity) {
this.logger.error({ entityName: typeName, subgraphName }, 'missing entity, unable to compute deferred query')
throw new Error('UNABLE_BUILD_DEFERRED_QUERY_MISSING_ENTITY')
}
const resolver = deferredResolver(entity, keys, subgraphName)
subgraphs[subgraphName] = { fields: [], resolver, entity, queryNode }
}
/**
* run queries to fullfil a request
*
* TODO setup queries plan here
* TODO collect queries by: subgraph, non-dependent, fields, keys for entities involved
* TODO run parallel / same subgraph when possible
* @param {Map<String, QueryNode>} queries map (path, queryNode)
* @param {Set<String>} order paths
* @param {Object} context context
* @returns {*} merged result
*/
async runQueries (queries, order, context) {
const result = {}
const headers = this.headersAdapter(context.reply.request.headers)
// ! Set need to be reversed from inserting order
order = Array.from(order).reverse()
for (const p of order) {
const q = queries.get(p)
const path = p.split('#')[0]
const parentResult = this.getParentResult(q, path)
const { query, fieldName, keys } = buildQuery(q.query, parentResult)
// TODO query can have variables
this.logger.debug({ subgraph: q.subgraphName, path, query, headers }, 'run subgraph query')
const server = this.subgraphs.get(q.subgraphName).server
const data = await makeGraphqlRequest({ query, server, headers })
this.logger.debug({ path, query, data }, 'query result')
q.result = data[fieldName]
q.keys = keys
mergeResult(result, path, q, parentResult)
}
return result
}
/**
* collect parent info by result to build query and merge the result
*/
getParentResult (queryNode, path) {
const q = queryParentResult(queryNode)
const result = {
path: [],
data: undefined,
keys: {
self: queryNode.field.type.entity?.pkey,
parent: undefined
}
}
if (!q) {
return result
}
result.data = q.result
// keys here are from "buildQuery"
if (q.keys.length > 0) {
const queryParentKey = parentKey(q.keys, path, queryNode.field.typeName)
if (queryParentKey) {
result.path = pathJoin(queryParentKey.path, queryParentKey.key).split('.')
result.keys.parent = result.path.at(-1)
if (queryParentKey.as) {
result.as = queryParentKey.as
result.many = queryParentKey.many
}
}
}
if (!result.keys.parent) {
result.keys.parent = q.field.type.entity?.pkey
result.path = [pathJoin(path, result.keys.parent)]
}
return result
}
/**
* generate schema and resolvers to resolve subgraphs entities
* from sugraphs schemas and entities configuration
*/
resolveEntities () {
const topSchema = []
const topResolvers = { Query: {} }
const topEntities = new Map()
const topSchemaQueries = []
const topQueriesResolvers = {}
const entityNames = Object.keys(this.entities)
if (entityNames.length < 1) {
return { schema: undefined, resolvers: undefined, entities: undefined }
}
for (const entityName of entityNames) {
const entity = this.entities[entityName]
const e = {
subgraph: entity.subgraph,
resolver: entity.resolver,
pkey: entity.pkey,
fkeys: new Map(),
many: new Map()
}
const entitySchemaFields = {}
const entityResolverFields = {}
// pkey
const type = schemaTypeName(this.types, entity.subgraph, entityName, entity.pkey)
entitySchemaFields[entity.pkey] = type
// fkeys
if (entity.fkeys) {
for (const fkey of entity.fkeys) {
setEntityFKey(e.fkeys, fkey)
entitySchemaFields[fkey.as] = fkey.type
}
}
// many
if (entity.many) {
for (const many of entity.many) {
setEntityMany(e.many, many)
entitySchemaFields[many.as] = `[${many.type}]`
}
}
const fields = Object.entries(entitySchemaFields)
.map(([k, v]) => `${k}: ${v}`)
.join(', ')
topEntities.set(entityName, e)
topSchema.push(`type ${entityName} { ${fields} }`)
topResolvers[entityName] = entityResolverFields
}
// cleanup outcome
for (const entity of topEntities.values()) {
entity.fkeys = Array.from(entity.fkeys.values())
entity.many = Array.from(entity.many.values())
}
if (topSchemaQueries.length > 0) {
topSchema.push(`type Query {\n ${topSchemaQueries.join('\n ')}\n}`)
topResolvers.Query = topQueriesResolvers
} else {
topSchema.push('type Query {\n _composer: String \n}')
topResolvers.Query = { _composer: function _composer () { return '_composer' } }
}
for (const name of Object.keys(topEntities)) {
const entity = topEntities[name]
entity.fkeys = Array.from(entity.fkeys.values())
entity.many = Array.from(entity.many.values())
}
return {
schema: topSchema.join('\n\n'),
resolvers: topResolvers,
entities: Object.fromEntries(topEntities)
}
}
}
// TODO improve code logic, add unit tests
// key should be selected by parent type, as it is for parent/many
function parentKey (keys, path, type) {
let pkey
const fkeys = []
for (const k of keys) {
// ok-ish
if (k.pkey) {
pkey = k.pkey
continue
}
if (path.indexOf(k.resolverPath) !== 0) { continue }
// ok
if (k.parent && type === k.typeName) {
if (k.parent.many) {
return {
as: k.many.as,
many: k.many,
key: k.many.fkey,
path: pathJoin(k.resolverPath, k.many.as)
}
}
}
// TODO improve
if (k.fkey) {
fkeys.push(k)
}
}
// TODO should be removed and fkey should be returned in the loop above as soon as the fkey is been detected
if (fkeys.length > 0) {
const k = fkeys[0]
return { key: k.fkey.field ?? k.fkey.pkey, path: k.resolverPath }
}
if (pkey) { return { key: pkey, path } }
}
function deferredResolver (entity, deferredKeys, subgraphName) {
if (!deferredKeys || deferredKeys.length < 1) {
return entity.resolver
}
for (const key of deferredKeys) {
if (key.fkey && key.fkey.subgraph === subgraphName) {
return key.fkey.resolver
}
if (key.parent && key.parent.fkey && key.parent.fkey.subgraph === subgraphName) {
return key.parent.fkey.resolver
}
if (key.parent && key.parent.many && key.parent.many.subgraph === subgraphName) {
return key.parent.many.resolver
}
}
return entity.resolver
}
// TODO merge more query parts, for example args values
function mergeQueries (currentQuery, query) {
// no worries, fields will be filtered later on building
currentQuery.query.selection = currentQuery.query.selection.concat(query.query.selection)
return currentQuery
}
function setEntityFKey (fkeys, fkey) {
const index = `${fkey.type}.${fkey.field}`
fkeys.set(index, fkey)
}
function setEntityMany (manys, many) {
const index = `${many.type}.${many.field || many.pkey}`
manys.set(index, many)
}
module.exports = { Composer }