-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathschema.graphql
535 lines (461 loc) · 15.6 KB
/
schema.graphql
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
# neo4j-graphql-js adds some directives during parsing
# To make it work for other graphql client we need to add them to the schema manually, based on:
# https://github.com/neo4j-graphql/neo4j-graphql-js/blob/master/src/augment/directives.js
directive @relation(
name: String
direction: String
from: String
to: String
) on FIELD_DEFINITION | OBJECT
directive @cypher(statement: String) on FIELD_DEFINITION
directive @index on FIELD_DEFINITION
directive @id on FIELD_DEFINITION
"""
Represents date, time and time zone
"""
scalar DateTime
"""
Arbitrary data
"""
scalar Any
"""
Full path of a given node, e.g. cap.core.type.platform.kubernetes
"""
scalar NodePath
"""
Version in semantic versioning, e.g. 1.1.0
"""
scalar Version
"""
LockOwner defines owner name who locked a given TypeInstance
"""
scalar LockOwnerID
type TypeInstance {
id: ID! @id
createdAt: DateTime
lockedBy: LockOwnerID
"""
Common properties for all TypeInstances which cannot be changed
"""
typeRef: TypeInstanceTypeReference!
@relation(name: "OF_TYPE", direction: "OUT")
"""
Returns TypeInstances that are used. List is sorted by TypeInstance's TypeRef path in ascending order, and then by revision in descending order (newest revision are first).
If both TypeRef path and revision are same, then it's additionally sorted by TypeInstance createdAt field (newly created are first).
"""
uses: [TypeInstance!]!
@cypher(
statement: "MATCH (this)-[:USES]->(ti:TypeInstance)-[:OF_TYPE]-(tr: TypeInstanceTypeReference) RETURN ti ORDER BY tr.path ASC, tr.revision DESC, ti.createdAt DESC"
)
"""
Returns TypeInstances that uses this TypeInstance. List is sorted by TypeInstance's TypeRef path in ascending order, and then by revision in descending order (newest revision are first).
If both TypeRef path and revision are same, then it's additionally sorted by TypeInstance createdAt field (newly created are first).
"""
usedBy: [TypeInstance!]!
@cypher(
statement: "MATCH (this)<-[:USES]-(ti:TypeInstance)-[:OF_TYPE]-(tr: TypeInstanceTypeReference) RETURN ti ORDER BY tr.path ASC, tr.revision DESC, ti.createdAt DESC"
)
backend: TypeInstanceBackendReference! @relation(name: "STORED_IN", direction: "OUT")
latestResourceVersion: TypeInstanceResourceVersion
@cypher(
statement: "MATCH (this)-[:CONTAINS]->(tir:TypeInstanceResourceVersion) RETURN tir ORDER BY tir.resourceVersion DESC LIMIT 1"
)
firstResourceVersion: TypeInstanceResourceVersion
@cypher(
statement: "MATCH (this)-[:CONTAINS]->(tir:TypeInstanceResourceVersion) RETURN tir ORDER BY tir.resourceVersion ASC LIMIT 1"
)
previousResourceVersion: TypeInstanceResourceVersion
@cypher(
statement: "MATCH (this)-[:CONTAINS]->(tir:TypeInstanceResourceVersion) RETURN tir ORDER BY tir.resourceVersion DESC SKIP 1 LIMIT 1"
)
resourceVersion(resourceVersion: Int!): TypeInstanceResourceVersion
@cypher(
statement: "MATCH (this)-[:CONTAINS]->(tir:TypeInstanceResourceVersion {resourceVersion: $resourceVersion}) RETURN tir"
)
resourceVersions: [TypeInstanceResourceVersion!]!
@relation(name: "CONTAINS", direction: "OUT")
}
type TypeInstanceResourceVersion {
resourceVersion: Int! @index
createdBy: String
metadata: TypeInstanceResourceVersionMetadata!
@relation(name: "DESCRIBED_BY", direction: "OUT")
spec: TypeInstanceResourceVersionSpec!
@relation(name: "SPECIFIED_BY", direction: "OUT")
}
type TypeInstanceResourceVersionMetadata {
attributes: [AttributeReference!]
@relation(name: "CHARACTERIZED_BY", direction: "OUT")
}
type TypeInstanceResourceVersionSpec {
value: Any!
@cypher(
statement: """
RETURN apoc.convert.fromJsonMap(this.value)
"""
)
"""
CURRENTLY NOT IMPLEMENTED
"""
instrumentation: TypeInstanceInstrumentation
@relation(name: "INSTRUMENTED_WITH", direction: "OUT")
}
type TypeInstanceBackendReference {
id: String!
abstract: Boolean!
}
type TypeInstanceTypeReference {
path: NodePath!
revision: Version!
}
input AttributeReferenceInput {
path: NodePath!
revision: Version!
}
type AttributeReference {
path: NodePath!
revision: Version!
}
"""
CURRENTLY NOT IMPLEMENTED
"""
type TypeInstanceInstrumentation {
metrics: TypeInstanceInstrumentationMetrics
@relation(name: "MEASURED_BY", direction: "OUT")
health: TypeInstanceInstrumentationHealth
@relation(name: "INDICATED_BY", direction: "OUT")
}
"""
CURRENTLY NOT IMPLEMENTED
"""
type TypeInstanceInstrumentationMetrics {
endpoint: String
regex: String # optional regex for scraping metrics
dashboards: [TypeInstanceInstrumentationMetricsDashboard!]!
@relation(name: "ON", direction: "OUT")
}
"""
CURRENTLY NOT IMPLEMENTED
"""
type TypeInstanceInstrumentationMetricsDashboard {
url: String!
}
"""
CURRENTLY NOT IMPLEMENTED
"""
type TypeInstanceInstrumentationHealth {
url: String
method: HTTPRequestMethod
# resolver, which does a HTTP call on a given URL
# and expects status code greater than or equal to 200
# and less than 400
# TODO implement TypeInstance health check, for resolution of this field
status: TypeInstanceInstrumentationHealthStatus
}
"""
CURRENTLY NOT IMPLEMENTED
"""
enum TypeInstanceInstrumentationHealthStatus {
UNKNOWN
READY
FAILING
}
enum HTTPRequestMethod {
GET
POST
}
input AttributeFilterInput {
path: NodePath!
rule: FilterRule = INCLUDE
"""
If not provided, any revision of the Attribute applies to this filter
"""
revision: Version
}
enum FilterRule {
INCLUDE
EXCLUDE
}
input TypeInstanceFilter {
attributes: [AttributeFilterInput]
typeRef: TypeRefFilterInput
createdBy: String
}
input TypeRefFilterInput {
path: NodePath!
"""
If not provided, it returns TypeInstances for all revisions of given Type
"""
revision: Version
}
input TypeInstanceTypeReferenceInput {
path: NodePath!
revision: Version!
}
input TypeInstanceBackendInput {
id: String!
}
input CreateTypeInstanceInput {
"""
Used to define the relationships, between the created TypeInstances
"""
alias: String
createdBy: String
typeRef: TypeInstanceTypeReferenceInput!
attributes: [AttributeReferenceInput!]
value: Any
"""
If not provided, TypeInstance value is stored as static value in Local Hub core storage.
"""
backend: TypeInstanceBackendInput
}
input TypeInstanceUsesRelationInput {
"""
Can be existing TypeInstance ID or alias of a TypeInstance from typeInstances list
"""
from: String!
"""
Can be existing TypeInstance ID or alias of a TypeInstance from typeInstances list
"""
to: String!
}
input CreateTypeInstancesInput {
typeInstances: [CreateTypeInstanceInput!]!
usesRelations: [TypeInstanceUsesRelationInput!]!
}
type CreateTypeInstanceOutput {
id: ID!
alias: String!
}
"""
At least one property needs to be specified.
"""
input UpdateTypeInstanceInput {
"""
The attributes property is optional. If not provided, previous value is used.
"""
attributes: [AttributeReferenceInput!]
"""
The value property is optional. If not provided, previous value is used.
"""
value: Any
}
input UpdateTypeInstancesInput {
"""
Allows you to update TypeInstances which are locked by a given ownerID. If not provided,
you can update only those TypeInstances which are not locked.
"""
ownerID: LockOwnerID
createdBy: String
id: ID!
typeInstance: UpdateTypeInstanceInput!
}
input LockTypeInstancesInput {
ids: [ID!]!
ownerID: LockOwnerID!
}
input UnlockTypeInstancesInput {
ids: [ID!]!
ownerID: LockOwnerID!
}
type Query {
"""
Returns all TypeInstances. List is sorted by TypeInstance's TypeRef path in ascending order, and then by revision in descending order (newest revision are first).
If both TypeRef path and revision are same, then it's additionally sorted by TypeInstance createdAt field (newly created are first).
"""
typeInstances(filter: TypeInstanceFilter = {}): [TypeInstance!]!
@cypher(
statement: """
WITH [x IN $filter.attributes WHERE x.rule = "EXCLUDE" | x ] AS excluded,
[x IN $filter.attributes WHERE x.rule = "INCLUDE" | x ] AS included
CALL {
WITH excluded
UNWIND excluded AS f
MATCH (ex:AttributeReference {path: f.path})
WHERE (f.revision IS NULL) OR (ex.revision = f.revision)
RETURN collect(ex) as excludedAttributes
}
MATCH (tir:TypeInstanceResourceVersion)-[:DESCRIBED_BY]->(meta:TypeInstanceResourceVersionMetadata)
OPTIONAL MATCH (meta)-[:CHARACTERIZED_BY]->(attr:AttributeReference)
MATCH (ti:TypeInstance)-[:OF_TYPE]->(typeRef:TypeInstanceTypeReference)
MATCH (ti:TypeInstance)-[:CONTAINS]->(tir)
WHERE
$filter = {} OR
(
(
$filter.typeRef IS NULL
OR
(
($filter.typeRef.revision IS NULL AND typeRef.path = $filter.typeRef.path)
OR
(typeRef.path = $filter.typeRef.path AND typeRef.revision = $filter.typeRef.revision)
)
)
AND
($filter.createdBy IS NULL OR tir.createdBy = $filter.createdBy)
AND
(
$filter.attributes IS NULL
OR
(
all(inc IN included WHERE
(tir)-[:DESCRIBED_BY]->(meta:TypeInstanceResourceVersionMetadata)-[:CHARACTERIZED_BY]->(attr:AttributeReference {path: inc.path})
AND
(inc.revision IS NULL OR attr.revision = inc.revision)
)
AND
none(exc IN excludedAttributes WHERE (tir)-[:DESCRIBED_BY]->(meta:TypeInstanceResourceVersionMetadata)-[:CHARACTERIZED_BY]->(exc))
)
)
)
WITH DISTINCT ti, typeRef
ORDER BY typeRef.path ASC, typeRef.revision DESC, ti.createdAt DESC
RETURN ti
"""
)
typeInstance(id: ID!): TypeInstance
@cypher(
statement: """
MATCH (this:TypeInstance {id: $id})
RETURN this
"""
)
}
type Mutation {
createTypeInstances(
in: CreateTypeInstancesInput!
): [CreateTypeInstanceOutput!]!
# TODO extend input with TypeInstanceInstrumentation
createTypeInstance(in: CreateTypeInstanceInput!): TypeInstance!
@cypher(
statement: """
CREATE (ti:TypeInstance {id: apoc.create.uuid(), createdAt: datetime()})
// Backend
WITH *
CALL apoc.do.when(
$in.backend.id IS NOT NULL,
'
WITH false as abstract
RETURN $in.backend.id as id, abstract
',
'
// TODO(storage): this should be resolved by Local Hub server during the insertion, not in cypher.
WITH true as abstract
MATCH (backend:TypeInstance)-[:OF_TYPE]->(typeRef {path: "cap.core.type.hub.storage.neo4j"})
RETURN backend.id as id, abstract
',
{in: $in}
) YIELD value as backend
MATCH (backendTI:TypeInstance {id: backend.id})
CREATE (ti)-[:USES]->(backendTI)
// TODO(storage): It should be taken from the uses relation but we don't have access to the TypeRef.additionalRefs to check
// if a given type is a backend or not. Maybe we will introduce a dedicated property to distinguish them from others.
MERGE (storageRef:TypeInstanceBackendReference {abstract: backend.abstract, id: backendTI.id})
CREATE (ti)-[:STORED_IN]->(storageRef)
// TypeRef
MERGE (typeRef:TypeInstanceTypeReference {path: $in.typeRef.path, revision: $in.typeRef.revision})
CREATE (ti)-[:OF_TYPE]->(typeRef)
// Revision
CREATE (tir: TypeInstanceResourceVersion {resourceVersion: 1, createdBy: $in.createdBy})
CREATE (ti)-[:CONTAINS]->(tir)
CREATE (tir)-[:DESCRIBED_BY]->(metadata: TypeInstanceResourceVersionMetadata)
CREATE (tir)-[:SPECIFIED_BY]->(spec: TypeInstanceResourceVersionSpec {value: apoc.convert.toJson($in.value)})
FOREACH (attr in $in.attributes |
MERGE (attrRef: AttributeReference {path: attr.path, revision: attr.revision})
CREATE (metadata)-[:CHARACTERIZED_BY]->(attrRef)
)
RETURN ti
"""
)
updateTypeInstances(in: [UpdateTypeInstancesInput]!): [TypeInstance!]!
@cypher(
statement: """
CALL {
UNWIND $in AS item
RETURN collect(item.id) as allInputIDs
}
// Check if all TypeInstances were found
WITH *
CALL {
WITH allInputIDs
MATCH (ti:TypeInstance)
WHERE ti.id IN allInputIDs
WITH collect(ti.id) as foundIDs
RETURN foundIDs
}
CALL apoc.util.validate(size(foundIDs) < size(allInputIDs), apoc.convert.toJson({code: 404, ids: foundIDs}), null)
// Check if given TypeInstances are not already locked by others
WITH *
CALL {
WITH *
UNWIND $in AS item
MATCH (tic:TypeInstance {id: item.id})
WHERE tic.lockedBy IS NOT NULL AND (item.ownerID IS NULL OR tic.lockedBy <> item.ownerID)
WITH collect(tic.id) as lockedIDs
RETURN lockedIDs
}
CALL apoc.util.validate(size(lockedIDs) > 0, apoc.convert.toJson({code: 409, ids: lockedIDs}), null)
UNWIND $in as item
MATCH (ti: TypeInstance {id: item.id})
CALL {
WITH ti
MATCH (ti)-[:CONTAINS]->(latestRevision:TypeInstanceResourceVersion)
RETURN latestRevision
ORDER BY latestRevision.resourceVersion DESC LIMIT 1
}
CREATE (tir: TypeInstanceResourceVersion {resourceVersion: latestRevision.resourceVersion + 1, createdBy: item.createdBy})
CREATE (ti)-[:CONTAINS]->(tir)
// Handle the `spec.value` property
CREATE (spec: TypeInstanceResourceVersionSpec)
CREATE (tir)-[:SPECIFIED_BY]->(spec)
WITH ti, tir, spec, latestRevision, item
CALL apoc.do.when(
item.typeInstance.value IS NOT NULL,
'
SET spec.value = apoc.convert.toJson(item.typeInstance.value) RETURN spec
',
'
MATCH (latestRevision)-[:SPECIFIED_BY]->(latestSpec: TypeInstanceResourceVersionSpec)
SET spec.value = latestSpec.value RETURN spec
',
{spec:spec, latestRevision: latestRevision, item: item}) YIELD value
// Handle the `metadata.attributes` property
CREATE (metadata: TypeInstanceResourceVersionMetadata)
CREATE (tir)-[:DESCRIBED_BY]->(metadata)
WITH ti, tir, latestRevision, metadata, item
CALL apoc.do.when(
item.typeInstance.attributes IS NOT NULL,
'
FOREACH (attr in item.typeInstance.attributes |
MERGE (attrRef: AttributeReference {path: attr.path, revision: attr.revision})
CREATE (metadata)-[:CHARACTERIZED_BY]->(attrRef)
)
RETURN metadata
',
'
OPTIONAL MATCH (latestRevision)-[:DESCRIBED_BY]->(TypeInstanceResourceVersionMetadata)-[:CHARACTERIZED_BY]->(latestAttrRef: AttributeReference)
WHERE latestAttrRef IS NOT NULL
WITH *, COLLECT(latestAttrRef) AS latestAttrRefs
FOREACH (attr in latestAttrRefs |
CREATE (metadata)-[:CHARACTERIZED_BY]->(attr)
)
RETURN metadata
',
{metadata: metadata, latestRevision: latestRevision, item: item}
) YIELD value
RETURN ti
"""
)
deleteTypeInstance(id: ID!, ownerID: LockOwnerID): ID!
"""
Mark given TypeInstances as locked by a given owner.
If at least one TypeInstance is already locked with different OwnerID, an error is returned.
"""
lockTypeInstances(in: LockTypeInstancesInput!): [ID!]!
"""
Remove lock from given TypeInstances.
If at least one TypeInstance was not locked by a given owner, an error is returned.
"""
unlockTypeInstances(in: UnlockTypeInstancesInput!): [ID!]!
}
# TODO: Prepare directive for user authorization in https://github.com/capactio/capact/issues/508