@contember/binding 1.1.0-alpha.6 → 1.1.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/accessorTree/AccessorTreeStateContext.js +1 -3
- package/dist/development/accessorTree/AccessorTreeStateContext.js.map +1 -1
- package/dist/development/accessorTree/NormalizedPersistedData.js.map +1 -1
- package/dist/development/accessorTree/accessorTreeStateReducer.js +7 -1
- package/dist/development/accessorTree/accessorTreeStateReducer.js.map +1 -1
- package/dist/development/accessorTree/useAccessorTreeState.js +8 -1
- package/dist/development/accessorTree/useAccessorTreeState.js.map +1 -1
- package/dist/development/accessorTree/useDataBinding.js +18 -16
- package/dist/development/accessorTree/useDataBinding.js.map +1 -1
- package/dist/development/core/AccessorErrorManager.js +2 -2
- package/dist/development/core/AccessorErrorManager.js.map +1 -1
- package/dist/development/core/DataBinding.js +14 -13
- package/dist/development/core/DataBinding.js.map +1 -1
- package/dist/development/core/ErrorsPreprocessor.js +9 -13
- package/dist/development/core/ErrorsPreprocessor.js.map +1 -1
- package/dist/development/core/MutationGenerator.js +79 -43
- package/dist/development/core/MutationGenerator.js.map +1 -1
- package/dist/development/core/RequestResponseNormalizer.js +93 -130
- package/dist/development/core/RequestResponseNormalizer.js.map +1 -1
- package/dist/development/core/StateInitializer.js +5 -1
- package/dist/development/core/StateInitializer.js.map +1 -1
- package/dist/development/core/TreeAugmenter.js +4 -4
- package/dist/development/core/TreeAugmenter.js.map +1 -1
- package/dist/development/core/TreeStore.js +4 -4
- package/dist/development/core/TreeStore.js.map +1 -1
- package/dist/development/core/VariableInputTransformer.js.map +1 -1
- package/dist/development/core/requestAliases/MutationAlias.js +2 -40
- package/dist/development/core/requestAliases/MutationAlias.js.map +1 -1
- package/dist/development/helperComponents/helpers/evaluateCondition.js.map +1 -1
- package/dist/production/accessorTree/AccessorTreeStateContext.js +1 -3
- package/dist/production/accessorTree/AccessorTreeStateContext.js.map +1 -1
- package/dist/production/accessorTree/NormalizedPersistedData.js.map +1 -1
- package/dist/production/accessorTree/accessorTreeStateReducer.js +7 -1
- package/dist/production/accessorTree/accessorTreeStateReducer.js.map +1 -1
- package/dist/production/accessorTree/useAccessorTreeState.js +8 -1
- package/dist/production/accessorTree/useAccessorTreeState.js.map +1 -1
- package/dist/production/accessorTree/useDataBinding.js +18 -16
- package/dist/production/accessorTree/useDataBinding.js.map +1 -1
- package/dist/production/core/AccessorErrorManager.js +2 -2
- package/dist/production/core/AccessorErrorManager.js.map +1 -1
- package/dist/production/core/DataBinding.js +14 -13
- package/dist/production/core/DataBinding.js.map +1 -1
- package/dist/production/core/ErrorsPreprocessor.js +9 -13
- package/dist/production/core/ErrorsPreprocessor.js.map +1 -1
- package/dist/production/core/MutationGenerator.js +79 -43
- package/dist/production/core/MutationGenerator.js.map +1 -1
- package/dist/production/core/RequestResponseNormalizer.js +93 -130
- package/dist/production/core/RequestResponseNormalizer.js.map +1 -1
- package/dist/production/core/StateInitializer.js +5 -1
- package/dist/production/core/StateInitializer.js.map +1 -1
- package/dist/production/core/TreeAugmenter.js +4 -4
- package/dist/production/core/TreeAugmenter.js.map +1 -1
- package/dist/production/core/TreeStore.js +4 -4
- package/dist/production/core/TreeStore.js.map +1 -1
- package/dist/production/core/VariableInputTransformer.js.map +1 -1
- package/dist/production/core/requestAliases/MutationAlias.js +2 -33
- package/dist/production/core/requestAliases/MutationAlias.js.map +1 -1
- package/dist/production/helperComponents/helpers/evaluateCondition.js.map +1 -1
- package/dist/types/accessorTree/AccessorTreeState.d.ts +4 -0
- package/dist/types/accessorTree/AccessorTreeState.d.ts.map +1 -1
- package/dist/types/accessorTree/AccessorTreeStateAction.d.ts +4 -0
- package/dist/types/accessorTree/AccessorTreeStateAction.d.ts.map +1 -1
- package/dist/types/accessorTree/AccessorTreeStateContext.d.ts +1 -1
- package/dist/types/accessorTree/AccessorTreeStateContext.d.ts.map +1 -1
- package/dist/types/accessorTree/NormalizedPersistedData.d.ts +2 -2
- package/dist/types/accessorTree/NormalizedPersistedData.d.ts.map +1 -1
- package/dist/types/accessorTree/QueryRequestResponse.d.ts +2 -2
- package/dist/types/accessorTree/QueryRequestResponse.d.ts.map +1 -1
- package/dist/types/accessorTree/accessorTreeStateReducer.d.ts.map +1 -1
- package/dist/types/accessorTree/useAccessorTreeState.d.ts.map +1 -1
- package/dist/types/accessorTree/useDataBinding.d.ts.map +1 -1
- package/dist/types/core/AccessorErrorManager.d.ts +2 -1
- package/dist/types/core/AccessorErrorManager.d.ts.map +1 -1
- package/dist/types/core/DataBinding.d.ts +1 -1
- package/dist/types/core/DataBinding.d.ts.map +1 -1
- package/dist/types/core/ErrorsPreprocessor.d.ts +3 -1
- package/dist/types/core/ErrorsPreprocessor.d.ts.map +1 -1
- package/dist/types/core/MutationGenerator.d.ts +25 -1
- package/dist/types/core/MutationGenerator.d.ts.map +1 -1
- package/dist/types/core/RequestResponseNormalizer.d.ts +6 -7
- package/dist/types/core/RequestResponseNormalizer.d.ts.map +1 -1
- package/dist/types/core/StateInitializer.d.ts.map +1 -1
- package/dist/types/core/TreeAugmenter.d.ts +3 -2
- package/dist/types/core/TreeAugmenter.d.ts.map +1 -1
- package/dist/types/core/TreeStore.d.ts +3 -2
- package/dist/types/core/TreeStore.d.ts.map +1 -1
- package/dist/types/core/VariableInputTransformer.d.ts +2 -2
- package/dist/types/core/VariableInputTransformer.d.ts.map +1 -1
- package/dist/types/core/requestAliases/MutationAlias.d.ts +0 -3
- package/dist/types/core/requestAliases/MutationAlias.d.ts.map +1 -1
- package/dist/types/core/requestAliases/index.d.ts +0 -3
- package/dist/types/core/requestAliases/index.d.ts.map +1 -1
- package/dist/types/core/state/FieldState.d.ts +2 -2
- package/dist/types/core/state/FieldState.d.ts.map +1 -1
- package/dist/types/coreComponents/Field.d.ts +1 -1
- package/dist/types/coreComponents/Field.d.ts.map +1 -1
- package/dist/types/entityEvents/useDerivedField.d.ts +2 -2
- package/dist/types/entityEvents/useDerivedField.d.ts.map +1 -1
- package/dist/types/helperComponents/FieldView.d.ts +7 -7
- package/dist/types/helperComponents/FieldView.d.ts.map +1 -1
- package/dist/types/helperComponents/SugaredField.d.ts +1 -1
- package/dist/types/helperComponents/SugaredField.d.ts.map +1 -1
- package/dist/types/helperComponents/helpers/evaluateCondition.d.ts +2 -2
- package/dist/types/helperComponents/helpers/evaluateCondition.d.ts.map +1 -1
- package/dist/types/markers/FieldMarker.d.ts +3 -3
- package/dist/types/markers/FieldMarker.d.ts.map +1 -1
- package/dist/types/treeParameters/primitives/FieldValue.d.ts +2 -2
- package/dist/types/treeParameters/primitives/FieldValue.d.ts.map +1 -1
- package/dist/types/treeParameters/primitives/Json.d.ts +7 -0
- package/dist/types/treeParameters/primitives/Json.d.ts.map +1 -0
- package/dist/types/treeParameters/primitives/index.d.ts +1 -0
- package/dist/types/treeParameters/primitives/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/accessorTree/AccessorTreeState.ts +4 -0
- package/src/accessorTree/AccessorTreeStateAction.ts +4 -0
- package/src/accessorTree/AccessorTreeStateContext.ts +1 -3
- package/src/accessorTree/NormalizedPersistedData.ts +2 -2
- package/src/accessorTree/QueryRequestResponse.ts +2 -2
- package/src/accessorTree/accessorTreeStateReducer.ts +7 -1
- package/src/accessorTree/useAccessorTreeState.ts +8 -1
- package/src/accessorTree/useDataBinding.ts +15 -15
- package/src/core/AccessorErrorManager.ts +3 -2
- package/src/core/DataBinding.ts +17 -15
- package/src/core/ErrorsPreprocessor.ts +14 -16
- package/src/core/MutationGenerator.ts +112 -56
- package/src/core/RequestResponseNormalizer.ts +110 -161
- package/src/core/StateInitializer.ts +10 -6
- package/src/core/TreeAugmenter.ts +5 -4
- package/src/core/TreeStore.ts +5 -4
- package/src/core/VariableInputTransformer.ts +2 -2
- package/src/core/requestAliases/MutationAlias.ts +2 -57
- package/src/core/requestAliases/index.ts +0 -3
- package/src/core/state/FieldState.ts +2 -2
- package/src/helperComponents/helpers/evaluateCondition.ts +2 -2
- package/src/treeParameters/primitives/FieldValue.ts +2 -2
- package/src/treeParameters/primitives/Json.ts +7 -0
- package/src/treeParameters/primitives/index.ts +1 -0
- package/dist/development/core/requestAliases/mutationOperationSubTreeType.js +0 -6
- package/dist/development/core/requestAliases/mutationOperationSubTreeType.js.map +0 -1
- package/dist/development/core/requestAliases/mutationOperationType.js +0 -7
- package/dist/development/core/requestAliases/mutationOperationType.js.map +0 -1
- package/dist/production/core/requestAliases/mutationOperationSubTreeType.js +0 -6
- package/dist/production/core/requestAliases/mutationOperationSubTreeType.js.map +0 -1
- package/dist/production/core/requestAliases/mutationOperationType.js +0 -7
- package/dist/production/core/requestAliases/mutationOperationType.js.map +0 -1
- package/dist/types/core/requestAliases/TopLevelMutationOperation.d.ts +0 -11
- package/dist/types/core/requestAliases/TopLevelMutationOperation.d.ts.map +0 -1
- package/dist/types/core/requestAliases/mutationOperationSubTreeType.d.ts +0 -5
- package/dist/types/core/requestAliases/mutationOperationSubTreeType.d.ts.map +0 -1
- package/dist/types/core/requestAliases/mutationOperationType.d.ts +0 -6
- package/dist/types/core/requestAliases/mutationOperationType.d.ts.map +0 -1
- package/src/core/requestAliases/TopLevelMutationOperation.ts +0 -11
- package/src/core/requestAliases/mutationOperationSubTreeType.ts +0 -4
- package/src/core/requestAliases/mutationOperationType.ts +0 -5
|
@@ -4,18 +4,19 @@ import { CrudQueryBuilder, GraphQlBuilder } from '@contember/client'
|
|
|
4
4
|
import { ClientGeneratedUuid, ServerId } from '../accessorTree'
|
|
5
5
|
import { BindingError } from '../BindingError'
|
|
6
6
|
import { PRIMARY_KEY_NAME, TYPENAME_KEY_NAME } from '../bindingTypes'
|
|
7
|
-
import { FieldMarker, HasManyRelationMarker, HasOneRelationMarker } from '../markers'
|
|
8
|
-
import type { EntityId, EntityName, FieldValue, PlaceholderName
|
|
7
|
+
import { EntityFieldMarkers, FieldMarker, HasManyRelationMarker, HasOneRelationMarker } from '../markers'
|
|
8
|
+
import type { EntityId, EntityName, FieldValue, PlaceholderName } from '../treeParameters'
|
|
9
9
|
import { assertNever, isEmptyObject } from '../utils'
|
|
10
10
|
import { QueryGenerator } from './QueryGenerator'
|
|
11
|
-
import { MutationAlias
|
|
11
|
+
import { MutationAlias } from './requestAliases'
|
|
12
12
|
import {
|
|
13
13
|
EntityListState,
|
|
14
14
|
EntityRealmState,
|
|
15
15
|
EntityRealmStateStub,
|
|
16
16
|
EntityState,
|
|
17
17
|
FieldState,
|
|
18
|
-
getEntityMarker,
|
|
18
|
+
getEntityMarker,
|
|
19
|
+
StateIterator,
|
|
19
20
|
} from './state'
|
|
20
21
|
import type { TreeStore } from './TreeStore'
|
|
21
22
|
|
|
@@ -23,14 +24,35 @@ type QueryBuilder = Omit<CrudQueryBuilder.CrudQueryBuilder, CrudQueryBuilder.Que
|
|
|
23
24
|
|
|
24
25
|
type ProcessedPlaceholdersByEntity = Map<EntityState, Set<PlaceholderName>>
|
|
25
26
|
|
|
27
|
+
export type SubMutationOperation =
|
|
28
|
+
& {
|
|
29
|
+
alias: string,
|
|
30
|
+
subTreePlaceholder: PlaceholderName,
|
|
31
|
+
subTreeType: 'list' | 'single'
|
|
32
|
+
}
|
|
33
|
+
& (
|
|
34
|
+
| { type: 'delete', id: EntityId }
|
|
35
|
+
| { type: 'update', markers: EntityFieldMarkers, id: EntityId }
|
|
36
|
+
| { type: 'create', markers: EntityFieldMarkers, id: EntityId }
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
export type PersistMutationResult = {
|
|
40
|
+
query: string
|
|
41
|
+
operations: SubMutationOperation[]
|
|
42
|
+
}
|
|
43
|
+
|
|
26
44
|
// TODO enforce correct expected mutations in dev mode.
|
|
27
45
|
export class MutationGenerator {
|
|
46
|
+
|
|
47
|
+
private aliasCounter = 1
|
|
48
|
+
|
|
28
49
|
public constructor(private readonly treeStore: TreeStore) {
|
|
29
50
|
}
|
|
30
51
|
|
|
31
|
-
public getPersistMutation():
|
|
52
|
+
public getPersistMutation(): PersistMutationResult | undefined {
|
|
32
53
|
try {
|
|
33
54
|
let builder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder()
|
|
55
|
+
const operations: SubMutationOperation[] = []
|
|
34
56
|
const processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity = new Map()
|
|
35
57
|
|
|
36
58
|
|
|
@@ -40,30 +62,36 @@ export class MutationGenerator {
|
|
|
40
62
|
continue
|
|
41
63
|
}
|
|
42
64
|
if (subTreeState.type === 'entityRealm') {
|
|
43
|
-
|
|
65
|
+
const subMutation = this.addSubMutation(
|
|
44
66
|
processedPlaceholdersByEntity,
|
|
45
|
-
treeRootId,
|
|
46
67
|
placeholderName,
|
|
47
|
-
|
|
68
|
+
'single',
|
|
48
69
|
subTreeState.entity.id.value,
|
|
49
70
|
subTreeState,
|
|
50
71
|
builder,
|
|
51
72
|
)
|
|
73
|
+
if (subMutation) {
|
|
74
|
+
builder = subMutation[0]
|
|
75
|
+
operations.push(subMutation[1])
|
|
76
|
+
}
|
|
52
77
|
} else if (subTreeState.type === 'entityList') {
|
|
53
78
|
for (const childState of subTreeState.children.values()) {
|
|
54
79
|
if (childState.type === 'entityRealmStub') {
|
|
55
80
|
// TODO there can be a forceCreate somewhere in there that we're hereby ignoring.
|
|
56
81
|
continue
|
|
57
82
|
}
|
|
58
|
-
|
|
83
|
+
const subMutation = this.addSubMutation(
|
|
59
84
|
processedPlaceholdersByEntity,
|
|
60
|
-
treeRootId,
|
|
61
85
|
placeholderName,
|
|
62
|
-
|
|
86
|
+
'list',
|
|
63
87
|
childState.entity.id.value,
|
|
64
88
|
childState,
|
|
65
89
|
builder,
|
|
66
90
|
)
|
|
91
|
+
if (subMutation) {
|
|
92
|
+
builder = subMutation[0]
|
|
93
|
+
operations.push(subMutation[1])
|
|
94
|
+
}
|
|
67
95
|
}
|
|
68
96
|
if (subTreeState.plannedRemovals) {
|
|
69
97
|
for (const [removedId, removalType] of subTreeState.plannedRemovals) {
|
|
@@ -71,18 +99,14 @@ export class MutationGenerator {
|
|
|
71
99
|
continue
|
|
72
100
|
}
|
|
73
101
|
if (removalType === 'delete') {
|
|
102
|
+
const alias = this.createAlias()
|
|
74
103
|
builder = this.addDeleteMutation(
|
|
75
104
|
subTreeState.entityName,
|
|
76
105
|
removedId,
|
|
77
|
-
|
|
78
|
-
treeRootId,
|
|
79
|
-
subTreePlaceholder: placeholderName,
|
|
80
|
-
type: mutationOperationType.delete,
|
|
81
|
-
subTreeType: mutationOperationSubTreeType.entityList,
|
|
82
|
-
entityId: removedId,
|
|
83
|
-
}),
|
|
106
|
+
alias,
|
|
84
107
|
builder,
|
|
85
108
|
)
|
|
109
|
+
operations.push({ alias, subTreePlaceholder: placeholderName, subTreeType: 'list', type: 'delete', id: removedId })
|
|
86
110
|
} else {
|
|
87
111
|
assertNever(removalType)
|
|
88
112
|
}
|
|
@@ -94,7 +118,8 @@ export class MutationGenerator {
|
|
|
94
118
|
}
|
|
95
119
|
}
|
|
96
120
|
|
|
97
|
-
|
|
121
|
+
const query = builder.inTransaction(undefined, { deferForeignKeyConstraints: true }).getGql()
|
|
122
|
+
return { query, operations }
|
|
98
123
|
} catch (e) {
|
|
99
124
|
return undefined
|
|
100
125
|
}
|
|
@@ -102,66 +127,91 @@ export class MutationGenerator {
|
|
|
102
127
|
|
|
103
128
|
private addSubMutation(
|
|
104
129
|
processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity,
|
|
105
|
-
treeRootId: TreeRootId | undefined,
|
|
106
130
|
subTreePlaceholder: PlaceholderName,
|
|
107
|
-
subTreeType:
|
|
131
|
+
subTreeType: 'list' | 'single',
|
|
108
132
|
entityId: EntityId,
|
|
109
133
|
realmState: EntityRealmState,
|
|
110
134
|
queryBuilder: QueryBuilder,
|
|
111
|
-
): QueryBuilder {
|
|
135
|
+
): [QueryBuilder, SubMutationOperation] | undefined {
|
|
112
136
|
if (realmState.unpersistedChangesCount === 0) {
|
|
113
137
|
// Bail out early
|
|
114
|
-
return
|
|
138
|
+
return undefined
|
|
115
139
|
}
|
|
140
|
+
const fieldMarkers = getEntityMarker(realmState).fields.markers
|
|
141
|
+
|
|
142
|
+
const alias = this.createAlias()
|
|
143
|
+
|
|
116
144
|
if (realmState.entity.isScheduledForDeletion) {
|
|
117
|
-
|
|
145
|
+
const builder = this.addDeleteMutation(
|
|
118
146
|
realmState.entity.entityName,
|
|
119
147
|
entityId,
|
|
120
|
-
|
|
121
|
-
treeRootId,
|
|
122
|
-
subTreePlaceholder,
|
|
123
|
-
type: mutationOperationType.delete,
|
|
124
|
-
subTreeType,
|
|
125
|
-
entityId,
|
|
126
|
-
}),
|
|
148
|
+
alias,
|
|
127
149
|
queryBuilder,
|
|
128
150
|
)
|
|
151
|
+
return [
|
|
152
|
+
builder,
|
|
153
|
+
{
|
|
154
|
+
alias,
|
|
155
|
+
subTreePlaceholder,
|
|
156
|
+
subTreeType,
|
|
157
|
+
type: 'delete',
|
|
158
|
+
id: entityId,
|
|
159
|
+
},
|
|
160
|
+
]
|
|
129
161
|
} else if (!realmState.entity.id.existsOnServer) {
|
|
130
|
-
|
|
162
|
+
const builder = this.addCreateMutation(
|
|
131
163
|
processedPlaceholdersByEntity,
|
|
132
164
|
realmState,
|
|
133
|
-
|
|
134
|
-
treeRootId,
|
|
135
|
-
subTreePlaceholder,
|
|
136
|
-
type: mutationOperationType.create,
|
|
137
|
-
subTreeType,
|
|
138
|
-
entityId,
|
|
139
|
-
}),
|
|
165
|
+
alias,
|
|
140
166
|
this.getNodeFragmentName(subTreePlaceholder, subTreeType, realmState),
|
|
141
167
|
queryBuilder,
|
|
168
|
+
fieldMarkers,
|
|
142
169
|
)
|
|
170
|
+
if (!builder) {
|
|
171
|
+
return undefined
|
|
172
|
+
}
|
|
173
|
+
return [
|
|
174
|
+
builder,
|
|
175
|
+
{
|
|
176
|
+
alias,
|
|
177
|
+
subTreePlaceholder,
|
|
178
|
+
subTreeType,
|
|
179
|
+
type: 'create',
|
|
180
|
+
markers: fieldMarkers,
|
|
181
|
+
id: entityId,
|
|
182
|
+
},
|
|
183
|
+
]
|
|
143
184
|
}
|
|
144
|
-
|
|
185
|
+
const builder = this.addUpdateMutation(
|
|
145
186
|
processedPlaceholdersByEntity,
|
|
146
187
|
realmState,
|
|
147
|
-
|
|
148
|
-
treeRootId,
|
|
149
|
-
subTreePlaceholder,
|
|
150
|
-
type: mutationOperationType.update,
|
|
151
|
-
subTreeType,
|
|
152
|
-
entityId,
|
|
153
|
-
}),
|
|
188
|
+
alias,
|
|
154
189
|
this.getNodeFragmentName(subTreePlaceholder, subTreeType, realmState),
|
|
155
190
|
queryBuilder,
|
|
191
|
+
fieldMarkers,
|
|
156
192
|
)
|
|
193
|
+
if (!builder) {
|
|
194
|
+
return undefined
|
|
195
|
+
}
|
|
196
|
+
return [
|
|
197
|
+
builder,
|
|
198
|
+
{
|
|
199
|
+
alias,
|
|
200
|
+
subTreePlaceholder,
|
|
201
|
+
subTreeType,
|
|
202
|
+
type: 'update',
|
|
203
|
+
id: entityId,
|
|
204
|
+
markers: fieldMarkers,
|
|
205
|
+
},
|
|
206
|
+
]
|
|
157
207
|
}
|
|
158
208
|
|
|
159
209
|
private getNodeFragmentName(
|
|
160
210
|
subTreePlaceholder: PlaceholderName,
|
|
161
|
-
subTreeType:
|
|
211
|
+
subTreeType: 'single' | 'list',
|
|
162
212
|
realmState: EntityRealmState,
|
|
163
213
|
): string | undefined {
|
|
164
|
-
if (subTreeType !==
|
|
214
|
+
if (subTreeType !== 'list') {
|
|
165
215
|
return undefined
|
|
166
216
|
}
|
|
167
217
|
return `${realmState.entity.entityName}_${subTreePlaceholder}`
|
|
@@ -192,7 +242,8 @@ export class MutationGenerator {
|
|
|
192
242
|
alias: string,
|
|
193
243
|
nodeFragmentName: string | undefined,
|
|
194
244
|
queryBuilder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder(),
|
|
195
|
-
|
|
245
|
+
fieldMarkers: EntityFieldMarkers,
|
|
246
|
+
): QueryBuilder | undefined {
|
|
196
247
|
const updateBuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Update> =
|
|
197
248
|
this.registerUpdateMutationPart(
|
|
198
249
|
processedPlaceholdersByEntity,
|
|
@@ -200,17 +251,17 @@ export class MutationGenerator {
|
|
|
200
251
|
new CrudQueryBuilder.WriteDataBuilder(),
|
|
201
252
|
)
|
|
202
253
|
if (updateBuilder.data === undefined || isEmptyObject(updateBuilder.data)) {
|
|
203
|
-
return
|
|
254
|
+
return undefined
|
|
204
255
|
}
|
|
205
256
|
|
|
206
257
|
const runtimeId = entityRealm.entity.id
|
|
207
258
|
|
|
208
259
|
if (!runtimeId.existsOnServer) {
|
|
209
|
-
return
|
|
260
|
+
return undefined
|
|
210
261
|
}
|
|
211
262
|
|
|
212
263
|
const readBuilder = QueryGenerator.registerQueryPart(
|
|
213
|
-
|
|
264
|
+
fieldMarkers,
|
|
214
265
|
CrudQueryBuilder.ReadBuilder.instantiate(),
|
|
215
266
|
)
|
|
216
267
|
|
|
@@ -239,7 +290,8 @@ export class MutationGenerator {
|
|
|
239
290
|
alias: string,
|
|
240
291
|
nodeFragmentName: string | undefined,
|
|
241
292
|
queryBuilder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder(),
|
|
242
|
-
|
|
293
|
+
fieldMarkers: EntityFieldMarkers,
|
|
294
|
+
): QueryBuilder | undefined {
|
|
243
295
|
const createBuilder: CrudQueryBuilder.WriteDataBuilder<CrudQueryBuilder.WriteOperation.Create> =
|
|
244
296
|
this.registerCreateMutationPart(
|
|
245
297
|
processedPlaceholdersByEntity,
|
|
@@ -247,10 +299,10 @@ export class MutationGenerator {
|
|
|
247
299
|
new CrudQueryBuilder.WriteDataBuilder(),
|
|
248
300
|
)
|
|
249
301
|
if (createBuilder.data === undefined || isEmptyObject(createBuilder.data)) {
|
|
250
|
-
return
|
|
302
|
+
return undefined
|
|
251
303
|
}
|
|
252
304
|
const readBuilder = QueryGenerator.registerQueryPart(
|
|
253
|
-
|
|
305
|
+
fieldMarkers,
|
|
254
306
|
CrudQueryBuilder.ReadBuilder.instantiate(),
|
|
255
307
|
)
|
|
256
308
|
if (nodeFragmentName) {
|
|
@@ -720,4 +772,8 @@ export class MutationGenerator {
|
|
|
720
772
|
}
|
|
721
773
|
return fieldSchema.enumName === null ? value : new GraphQlBuilder.GraphQlLiteral(value)
|
|
722
774
|
}
|
|
775
|
+
|
|
776
|
+
private createAlias(): string {
|
|
777
|
+
return `op_${this.aliasCounter++}`
|
|
778
|
+
}
|
|
723
779
|
}
|
|
@@ -1,212 +1,161 @@
|
|
|
1
1
|
import {
|
|
2
|
-
EntityFieldPersistedData,
|
|
3
2
|
NormalizedPersistedData,
|
|
4
3
|
PersistedEntityDataStore,
|
|
5
4
|
ReceivedDataTree,
|
|
6
5
|
ReceivedEntityData,
|
|
7
|
-
ReceivedFieldData,
|
|
8
6
|
ServerId,
|
|
9
|
-
SingleEntityPersistedData,
|
|
10
7
|
} from '../accessorTree'
|
|
11
8
|
import { BindingError } from '../BindingError'
|
|
12
9
|
import { PRIMARY_KEY_NAME } from '../bindingTypes'
|
|
13
|
-
import { assertNever } from '../utils'
|
|
14
|
-
import { MutationAlias, mutationOperationSubTreeType, mutationOperationType } from './requestAliases'
|
|
15
10
|
import { EntityId } from '../treeParameters'
|
|
11
|
+
import {
|
|
12
|
+
EntityFieldMarkers,
|
|
13
|
+
EntityListSubTreeMarker,
|
|
14
|
+
EntitySubTreeMarker,
|
|
15
|
+
FieldMarker,
|
|
16
|
+
HasManyRelationMarker,
|
|
17
|
+
HasOneRelationMarker,
|
|
18
|
+
MarkerTreeRoot,
|
|
19
|
+
} from '../markers'
|
|
20
|
+
import { SubMutationOperation } from './MutationGenerator'
|
|
21
|
+
import { assertNever } from '../utils'
|
|
16
22
|
|
|
17
23
|
export class RequestResponseNormalizer {
|
|
18
24
|
public static mergeInQueryResponse(
|
|
19
25
|
original: NormalizedPersistedData,
|
|
20
26
|
newPersistedData: ReceivedDataTree,
|
|
21
|
-
|
|
27
|
+
markerTree: MarkerTreeRoot,
|
|
28
|
+
): void {
|
|
22
29
|
const { subTreeDataStore, persistedEntityDataStore } = original
|
|
23
|
-
const presentIds: Set<string> = new Set()
|
|
24
|
-
|
|
25
|
-
for (const placeholderName in newPersistedData) {
|
|
26
|
-
const treeDatum = newPersistedData[placeholderName]
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
for (const [placeholder, marker] of markerTree.subTrees) {
|
|
32
|
+
const data = newPersistedData[placeholder] ?? null
|
|
33
|
+
if (marker instanceof EntitySubTreeMarker) {
|
|
34
|
+
if (data === null) {
|
|
35
|
+
subTreeDataStore.delete(placeholder)
|
|
36
|
+
} else {
|
|
37
|
+
if (Array.isArray(data)) {
|
|
38
|
+
throw new BindingError()
|
|
39
|
+
}
|
|
40
|
+
const id = this.processEntityData(persistedEntityDataStore, data, marker.fields.markers)
|
|
41
|
+
subTreeDataStore.set(placeholder, id)
|
|
42
|
+
}
|
|
43
|
+
} else if (EntityListSubTreeMarker) {
|
|
44
|
+
const subTreeListIds = new Set<EntityId>()
|
|
45
|
+
if (!Array.isArray(data)) {
|
|
46
|
+
throw new BindingError()
|
|
47
|
+
}
|
|
48
|
+
for (const row of data) {
|
|
49
|
+
const id = this.processEntityData(persistedEntityDataStore, row, marker.fields.markers)
|
|
50
|
+
subTreeListIds.add(id.value)
|
|
51
|
+
}
|
|
52
|
+
subTreeDataStore.set(placeholder, subTreeListIds)
|
|
34
53
|
} else {
|
|
35
|
-
|
|
54
|
+
throw new BindingError()
|
|
36
55
|
}
|
|
37
56
|
}
|
|
38
|
-
return presentIds
|
|
39
57
|
}
|
|
40
58
|
|
|
41
59
|
public static mergeInMutationResponse(
|
|
42
60
|
original: NormalizedPersistedData,
|
|
43
61
|
newPersistedData: ReceivedDataTree,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const presentIds: Set<string> = new Set()
|
|
62
|
+
operations: SubMutationOperation[],
|
|
63
|
+
): void {
|
|
47
64
|
|
|
48
|
-
|
|
49
|
-
const operation = MutationAlias.decodeTopLevel(operationAlias)
|
|
65
|
+
const { subTreeDataStore, persistedEntityDataStore } = original
|
|
50
66
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
const { type, entityId, subTreeType, subTreePlaceholder } = operation
|
|
67
|
+
for (const operation of operations) {
|
|
68
|
+
const { subTreePlaceholder, subTreeType, alias } = operation
|
|
55
69
|
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return this.rejectData()
|
|
70
|
+
const receivedData = newPersistedData[alias]
|
|
71
|
+
if (!receivedData || Array.isArray(receivedData)) {
|
|
72
|
+
throw new BindingError()
|
|
60
73
|
}
|
|
61
74
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
} else if (list === undefined) {
|
|
77
|
-
// That's fine. This is probably just an isCreating sub-tree.
|
|
78
|
-
subTreeDataStore.set(subTreePlaceholder, new Set([fieldData.value]))
|
|
79
|
-
} else {
|
|
80
|
-
return this.rejectData()
|
|
81
|
-
}
|
|
82
|
-
}
|
|
75
|
+
if (operation.type === 'update' || operation.type === 'create') {
|
|
76
|
+
const id = this.processEntityData(persistedEntityDataStore, receivedData, operation.markers)
|
|
77
|
+
if (subTreeType === 'single') {
|
|
78
|
+
subTreeDataStore.set(subTreePlaceholder, id)
|
|
79
|
+
} else if (subTreeType === 'list') {
|
|
80
|
+
const list = subTreeDataStore.get(subTreePlaceholder)
|
|
81
|
+
|
|
82
|
+
if (list instanceof Set) {
|
|
83
|
+
// TODO this is somewhat dubious because we're essentially just guessing the order of the entities
|
|
84
|
+
// and just carelessly put the new one at the end.
|
|
85
|
+
list.add(id.value)
|
|
86
|
+
} else if (list === undefined) {
|
|
87
|
+
// That's fine. This is probably just an isCreating sub-tree.
|
|
88
|
+
subTreeDataStore.set(subTreePlaceholder, new Set([id.value]))
|
|
83
89
|
} else {
|
|
84
|
-
|
|
90
|
+
throw new BindingError()
|
|
85
91
|
}
|
|
86
|
-
|
|
87
|
-
|
|
92
|
+
} else {
|
|
93
|
+
return assertNever(subTreeType)
|
|
88
94
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
list.delete(entityId)
|
|
102
|
-
} else {
|
|
103
|
-
return assertNever(subTreeType)
|
|
95
|
+
} else if (operation.type === 'delete') {
|
|
96
|
+
const id = this.extractId(receivedData)
|
|
97
|
+
// TODO there are potentially some references to entityId that this whole process won't quite remove.
|
|
98
|
+
// That's a memory leak. Probably not particularly severe in most cases but still.
|
|
99
|
+
persistedEntityDataStore.delete(id.uniqueValue)
|
|
100
|
+
if (subTreeType === 'single') {
|
|
101
|
+
subTreeDataStore.delete(subTreePlaceholder)
|
|
102
|
+
} else if (subTreeType === 'list') {
|
|
103
|
+
const list = subTreeDataStore.get(subTreePlaceholder)
|
|
104
|
+
|
|
105
|
+
if (!(list instanceof Set)) {
|
|
106
|
+
throw new BindingError()
|
|
104
107
|
}
|
|
105
|
-
|
|
108
|
+
list.delete(id.value)
|
|
109
|
+
} else {
|
|
110
|
+
return assertNever(subTreeType)
|
|
106
111
|
}
|
|
107
|
-
|
|
108
|
-
return assertNever(
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return presentIds
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
private static createFieldData(
|
|
115
|
-
entityMap: PersistedEntityDataStore,
|
|
116
|
-
fieldData: ReceivedFieldData,
|
|
117
|
-
): EntityFieldPersistedData {
|
|
118
|
-
if (Array.isArray(fieldData)) {
|
|
119
|
-
const subTreeListIds = new Set<EntityId>()
|
|
120
|
-
for (const entityDatum of fieldData) {
|
|
121
|
-
subTreeListIds.add(this.createEntityData(entityMap, entityDatum).value)
|
|
122
|
-
}
|
|
123
|
-
return subTreeListIds
|
|
124
|
-
} else if (fieldData !== null && typeof fieldData === 'object') {
|
|
125
|
-
return this.createEntityData(entityMap, fieldData)
|
|
126
|
-
} else {
|
|
127
|
-
return fieldData
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
private static createEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData): ServerId {
|
|
132
|
-
const primaryKey: EntityId | undefined = entityData[PRIMARY_KEY_NAME]
|
|
133
|
-
|
|
134
|
-
if (primaryKey === undefined) {
|
|
135
|
-
throw new BindingError(`The server has responded with an entity that lacks a primary key.`)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const id = new ServerId(primaryKey, entityData.__typename)
|
|
139
|
-
|
|
140
|
-
const presentEntityData = entityMap.get(id.uniqueValue)
|
|
141
|
-
|
|
142
|
-
if (presentEntityData === undefined) {
|
|
143
|
-
const fieldsMap: SingleEntityPersistedData = new Map()
|
|
144
|
-
entityMap.set(id.uniqueValue, fieldsMap)
|
|
145
|
-
for (const field in entityData) {
|
|
146
|
-
const fieldDatum = entityData[field]
|
|
147
|
-
|
|
148
|
-
fieldsMap.set(field, this.createFieldData(entityMap, fieldDatum))
|
|
112
|
+
} else {
|
|
113
|
+
return assertNever(operation)
|
|
149
114
|
}
|
|
150
|
-
} else {
|
|
151
|
-
this.mergeInEntityData(entityMap, presentEntityData, entityData)
|
|
152
115
|
}
|
|
153
|
-
return id
|
|
154
116
|
}
|
|
155
117
|
|
|
156
|
-
private static
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const newId = newDatum[PRIMARY_KEY_NAME]
|
|
169
|
-
const target = entityMap.get(ServerId.formatUniqueValue(newId, newDatum.__typename))
|
|
170
|
-
if (target === undefined) {
|
|
171
|
-
return this.createEntityData(entityMap, newDatum)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (fromTarget?.value === newId) {
|
|
175
|
-
this.mergeInEntityData(entityMap, target, newDatum)
|
|
176
|
-
return fromTarget
|
|
118
|
+
private static processEntityData(entityMap: PersistedEntityDataStore, entityData: ReceivedEntityData, fields: EntityFieldMarkers): ServerId {
|
|
119
|
+
const id = this.extractId(entityData)
|
|
120
|
+
const presentEntityData = entityMap.get(id.uniqueValue) ?? new Map()
|
|
121
|
+
entityMap.set(id.uniqueValue, presentEntityData)
|
|
122
|
+
|
|
123
|
+
for (const [placeholder, field] of fields) {
|
|
124
|
+
const value = entityData[placeholder]
|
|
125
|
+
if (field instanceof FieldMarker) {
|
|
126
|
+
presentEntityData.set(placeholder, value)
|
|
127
|
+
} else if (field instanceof HasOneRelationMarker) {
|
|
128
|
+
if (value === null) {
|
|
129
|
+
presentEntityData.set(placeholder, null)
|
|
177
130
|
} else {
|
|
178
|
-
|
|
131
|
+
presentEntityData.set(placeholder, this.processEntityData(entityMap, value as ReceivedEntityData, field.fields.markers))
|
|
179
132
|
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
133
|
+
} else if (field instanceof HasManyRelationMarker) {
|
|
134
|
+
const ids = presentEntityData.get(placeholder) ?? new Set()
|
|
135
|
+
presentEntityData.set(placeholder, ids)
|
|
136
|
+
if (!(ids instanceof Set)) {
|
|
137
|
+
throw new BindingError()
|
|
138
|
+
}
|
|
139
|
+
ids.clear()
|
|
140
|
+
for (const row of value as ReceivedEntityData[]) {
|
|
141
|
+
const id = this.processEntityData(entityMap, row, field.fields.markers)
|
|
142
|
+
ids.add(id.value)
|
|
186
143
|
}
|
|
187
|
-
|
|
144
|
+
} else {
|
|
145
|
+
throw new Error()
|
|
188
146
|
}
|
|
189
|
-
} else if (!Array.isArray(newDatum) && (newDatum === null || typeof newDatum !== 'object')) {
|
|
190
|
-
return newDatum
|
|
191
147
|
}
|
|
192
|
-
|
|
148
|
+
|
|
149
|
+
return id
|
|
193
150
|
}
|
|
194
151
|
|
|
195
|
-
private static
|
|
196
|
-
|
|
197
|
-
target: SingleEntityPersistedData,
|
|
198
|
-
newData: ReceivedEntityData,
|
|
199
|
-
): SingleEntityPersistedData {
|
|
200
|
-
for (const field in newData) {
|
|
201
|
-
const fromTarget = target.get(field)
|
|
202
|
-
const newDatum = newData[field]
|
|
152
|
+
private static extractId(entityData: ReceivedEntityData): ServerId {
|
|
153
|
+
const primaryKey: EntityId | undefined = entityData[PRIMARY_KEY_NAME]
|
|
203
154
|
|
|
204
|
-
|
|
155
|
+
if (primaryKey === undefined) {
|
|
156
|
+
throw new BindingError(`The server has responded with an entity that lacks a primary key.`)
|
|
205
157
|
}
|
|
206
|
-
return target
|
|
207
|
-
}
|
|
208
158
|
|
|
209
|
-
|
|
210
|
-
throw new BindingError(`Failed to process data received from the API.${extraMessage ? `\n${extraMessage}` : ''}`)
|
|
159
|
+
return new ServerId(primaryKey, entityData.__typename)
|
|
211
160
|
}
|
|
212
161
|
}
|