@aws-amplify/data-schema 1.3.10 → 1.4.0
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/cjs/ModelType.js +5 -0
- package/dist/cjs/ModelType.js.map +1 -1
- package/dist/cjs/SchemaProcessor.js +58 -1
- package/dist/cjs/SchemaProcessor.js.map +1 -1
- package/dist/cjs/runtime/addSchemaToClient.js +1 -0
- package/dist/cjs/runtime/addSchemaToClient.js.map +1 -1
- package/dist/cjs/runtime/addSchemaToClientWithInstance.js +1 -0
- package/dist/cjs/runtime/addSchemaToClientWithInstance.js.map +1 -1
- package/dist/cjs/runtime/internals/APIClient.js +63 -42
- package/dist/cjs/runtime/internals/APIClient.js.map +1 -1
- package/dist/cjs/runtime/internals/cancellation.js +48 -0
- package/dist/cjs/runtime/internals/cancellation.js.map +1 -0
- package/dist/cjs/runtime/internals/clientUtils.js +45 -3
- package/dist/cjs/runtime/internals/clientUtils.js.map +1 -1
- package/dist/cjs/runtime/internals/index.js +3 -1
- package/dist/cjs/runtime/internals/index.js.map +1 -1
- package/dist/cjs/runtime/internals/operations/custom.js +91 -85
- package/dist/cjs/runtime/internals/operations/custom.js.map +1 -1
- package/dist/cjs/runtime/internals/operations/get.js +71 -65
- package/dist/cjs/runtime/internals/operations/get.js.map +1 -1
- package/dist/cjs/runtime/internals/operations/indexQuery.js +66 -60
- package/dist/cjs/runtime/internals/operations/indexQuery.js.map +1 -1
- package/dist/cjs/runtime/internals/operations/list.js +90 -84
- package/dist/cjs/runtime/internals/operations/list.js.map +1 -1
- package/dist/cjs/runtime/internals/server/generateModelsProperty.js +3 -3
- package/dist/cjs/runtime/internals/server/generateModelsProperty.js.map +1 -1
- package/dist/cjs/runtime/internals/utils/clientProperties/generateModelsProperty.js +3 -3
- package/dist/cjs/runtime/internals/utils/clientProperties/generateModelsProperty.js.map +1 -1
- package/dist/cjs/runtime/utils/index.js +3 -1
- package/dist/cjs/runtime/utils/index.js.map +1 -1
- package/dist/cjs/runtime/utils/selfAwareAsync.js +40 -0
- package/dist/cjs/runtime/utils/selfAwareAsync.js.map +1 -0
- package/dist/esm/Authorization.d.ts +1 -1
- package/dist/esm/ClientSchema/Core/ClientModel.d.ts +20 -1
- package/dist/esm/ClientSchema/utilities/ResolveField.d.ts +5 -1
- package/dist/esm/ModelType.d.ts +6 -0
- package/dist/esm/ModelType.mjs +5 -0
- package/dist/esm/ModelType.mjs.map +1 -1
- package/dist/esm/SchemaProcessor.mjs +58 -1
- package/dist/esm/SchemaProcessor.mjs.map +1 -1
- package/dist/esm/runtime/addSchemaToClient.mjs +2 -0
- package/dist/esm/runtime/addSchemaToClient.mjs.map +1 -1
- package/dist/esm/runtime/addSchemaToClientWithInstance.mjs +2 -0
- package/dist/esm/runtime/addSchemaToClientWithInstance.mjs.map +1 -1
- package/dist/esm/runtime/bridge-types.d.ts +2 -2
- package/dist/esm/runtime/client/index.d.ts +6 -6
- package/dist/esm/runtime/internals/APIClient.d.ts +2 -2
- package/dist/esm/runtime/internals/APIClient.mjs +64 -42
- package/dist/esm/runtime/internals/APIClient.mjs.map +1 -1
- package/dist/esm/runtime/internals/cancellation.d.ts +10 -0
- package/dist/esm/runtime/internals/cancellation.mjs +44 -0
- package/dist/esm/runtime/internals/cancellation.mjs.map +1 -0
- package/dist/esm/runtime/internals/clientUtils.d.ts +78 -1
- package/dist/esm/runtime/internals/clientUtils.mjs +45 -3
- package/dist/esm/runtime/internals/clientUtils.mjs.map +1 -1
- package/dist/esm/runtime/internals/index.d.ts +1 -0
- package/dist/esm/runtime/internals/index.mjs +1 -0
- package/dist/esm/runtime/internals/index.mjs.map +1 -1
- package/dist/esm/runtime/internals/operations/custom.d.ts +1 -1
- package/dist/esm/runtime/internals/operations/custom.mjs +91 -85
- package/dist/esm/runtime/internals/operations/custom.mjs.map +1 -1
- package/dist/esm/runtime/internals/operations/get.d.ts +1 -1
- package/dist/esm/runtime/internals/operations/get.mjs +71 -65
- package/dist/esm/runtime/internals/operations/get.mjs.map +1 -1
- package/dist/esm/runtime/internals/operations/indexQuery.d.ts +1 -1
- package/dist/esm/runtime/internals/operations/indexQuery.mjs +66 -60
- package/dist/esm/runtime/internals/operations/indexQuery.mjs.map +1 -1
- package/dist/esm/runtime/internals/operations/list.d.ts +1 -1
- package/dist/esm/runtime/internals/operations/list.mjs +90 -84
- package/dist/esm/runtime/internals/operations/list.mjs.map +1 -1
- package/dist/esm/runtime/internals/server/generateModelsProperty.mjs +4 -4
- package/dist/esm/runtime/internals/server/generateModelsProperty.mjs.map +1 -1
- package/dist/esm/runtime/internals/utils/clientProperties/generateModelsProperty.mjs +4 -4
- package/dist/esm/runtime/internals/utils/clientProperties/generateModelsProperty.mjs.map +1 -1
- package/dist/esm/runtime/utils/index.d.ts +1 -0
- package/dist/esm/runtime/utils/index.mjs +1 -0
- package/dist/esm/runtime/utils/index.mjs.map +1 -1
- package/dist/esm/runtime/utils/selfAwareAsync.d.ts +19 -0
- package/dist/esm/runtime/utils/selfAwareAsync.mjs +37 -0
- package/dist/esm/runtime/utils/selfAwareAsync.mjs.map +1 -0
- package/dist/meta/cjs.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/ClientSchema/Core/ClientModel.ts +29 -0
- package/src/ClientSchema/utilities/ResolveField.ts +26 -6
- package/src/ModelType.ts +31 -0
- package/src/SchemaProcessor.ts +77 -2
- package/src/runtime/addSchemaToClient.ts +2 -0
- package/src/runtime/addSchemaToClientWithInstance.ts +2 -0
- package/src/runtime/bridge-types.ts +2 -2
- package/src/runtime/client/index.ts +207 -184
- package/src/runtime/internals/APIClient.ts +67 -23
- package/src/runtime/internals/cancellation.ts +57 -0
- package/src/runtime/internals/clientUtils.ts +61 -1
- package/src/runtime/internals/index.ts +1 -0
- package/src/runtime/internals/operations/custom.ts +117 -109
- package/src/runtime/internals/operations/get.ts +111 -95
- package/src/runtime/internals/operations/indexQuery.ts +100 -94
- package/src/runtime/internals/operations/list.ts +132 -119
- package/src/runtime/internals/server/generateModelsProperty.ts +31 -28
- package/src/runtime/internals/utils/clientProperties/generateModelsProperty.ts +36 -33
- package/src/runtime/utils/index.ts +1 -0
- package/src/runtime/utils/selfAwareAsync.ts +39 -0
|
@@ -22,6 +22,8 @@ import {
|
|
|
22
22
|
} from '../APIClient';
|
|
23
23
|
|
|
24
24
|
import { handleListGraphQlError } from './utils';
|
|
25
|
+
import { selfAwareAsync } from '../../utils';
|
|
26
|
+
import { extendCancellability } from '../cancellation';
|
|
25
27
|
|
|
26
28
|
export interface IndexMeta {
|
|
27
29
|
queryField: string;
|
|
@@ -37,7 +39,7 @@ export function indexQueryFactory(
|
|
|
37
39
|
getInternals: ClientInternalsGetter,
|
|
38
40
|
context = false,
|
|
39
41
|
) {
|
|
40
|
-
const indexQueryWithContext =
|
|
42
|
+
const indexQueryWithContext = (
|
|
41
43
|
contextSpec: AmplifyServer.ContextSpec,
|
|
42
44
|
args: QueryArgs,
|
|
43
45
|
options?: ListArgs,
|
|
@@ -56,7 +58,7 @@ export function indexQueryFactory(
|
|
|
56
58
|
);
|
|
57
59
|
};
|
|
58
60
|
|
|
59
|
-
const indexQuery =
|
|
61
|
+
const indexQuery = (args: QueryArgs, options?: ListArgs) => {
|
|
60
62
|
return _indexQuery(
|
|
61
63
|
client,
|
|
62
64
|
modelIntrospection,
|
|
@@ -104,7 +106,7 @@ function processGraphQlResponse(
|
|
|
104
106
|
};
|
|
105
107
|
}
|
|
106
108
|
|
|
107
|
-
|
|
109
|
+
function _indexQuery(
|
|
108
110
|
client: BaseClient,
|
|
109
111
|
modelIntrospection: ModelIntrospectionSchema,
|
|
110
112
|
model: SchemaModel,
|
|
@@ -113,108 +115,112 @@ async function _indexQuery(
|
|
|
113
115
|
args?: ListArgs & AuthModeParams,
|
|
114
116
|
contextSpec?: AmplifyServer.ContextSpec,
|
|
115
117
|
) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
'INDEX_QUERY',
|
|
122
|
-
args,
|
|
123
|
-
indexMeta,
|
|
124
|
-
);
|
|
125
|
-
const variables = buildGraphQLVariables(
|
|
126
|
-
model,
|
|
127
|
-
'INDEX_QUERY',
|
|
128
|
-
args,
|
|
129
|
-
modelIntrospection,
|
|
130
|
-
indexMeta,
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
const auth = authModeParams(client, getInternals, args);
|
|
134
|
-
|
|
135
|
-
const modelInitializer = (flattenedResult: any[]) =>
|
|
136
|
-
initializeModel(
|
|
137
|
-
client,
|
|
118
|
+
return selfAwareAsync(async (resultPromise) => {
|
|
119
|
+
const { name } = model;
|
|
120
|
+
|
|
121
|
+
const query = generateGraphQLDocument(
|
|
122
|
+
modelIntrospection,
|
|
138
123
|
name,
|
|
139
|
-
|
|
124
|
+
'INDEX_QUERY',
|
|
125
|
+
args,
|
|
126
|
+
indexMeta,
|
|
127
|
+
);
|
|
128
|
+
const variables = buildGraphQLVariables(
|
|
129
|
+
model,
|
|
130
|
+
'INDEX_QUERY',
|
|
131
|
+
args,
|
|
140
132
|
modelIntrospection,
|
|
141
|
-
|
|
142
|
-
auth.authToken,
|
|
143
|
-
!!contextSpec,
|
|
133
|
+
indexMeta,
|
|
144
134
|
);
|
|
145
135
|
|
|
146
|
-
|
|
147
|
-
const headers = getCustomHeaders(client, getInternals, args?.headers);
|
|
136
|
+
const auth = authModeParams(client, getInternals, args);
|
|
148
137
|
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
138
|
+
const modelInitializer = (flattenedResult: any[]) =>
|
|
139
|
+
initializeModel(
|
|
140
|
+
client,
|
|
141
|
+
name,
|
|
142
|
+
flattenedResult,
|
|
143
|
+
modelIntrospection,
|
|
144
|
+
auth.authMode,
|
|
145
|
+
auth.authToken,
|
|
146
|
+
!!contextSpec,
|
|
147
|
+
);
|
|
154
148
|
|
|
155
|
-
|
|
149
|
+
try {
|
|
150
|
+
const headers = getCustomHeaders(client, getInternals, args?.headers);
|
|
156
151
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
152
|
+
const graphQlParams = {
|
|
153
|
+
...auth,
|
|
154
|
+
query,
|
|
155
|
+
variables,
|
|
156
|
+
};
|
|
160
157
|
|
|
161
|
-
|
|
162
|
-
...requestArgs,
|
|
163
|
-
)) as GraphQLResult;
|
|
158
|
+
const requestArgs: [any, any] = [graphQlParams, headers];
|
|
164
159
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
name,
|
|
169
|
-
response,
|
|
170
|
-
args?.selectionSet,
|
|
171
|
-
modelInitializer,
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
} catch (error: any) {
|
|
175
|
-
/**
|
|
176
|
-
* The `data` type returned by `error` here could be:
|
|
177
|
-
* 1) `null`
|
|
178
|
-
* 2) an empty object
|
|
179
|
-
* 3) "populated" but with a `null` value:
|
|
180
|
-
* `data: { listByExampleId: null }`
|
|
181
|
-
* 4) an actual record:
|
|
182
|
-
* `data: { listByExampleId: items: [{ id: '1', ...etc } }]`
|
|
183
|
-
*/
|
|
184
|
-
const { data, errors } = error;
|
|
185
|
-
|
|
186
|
-
// `data` is not `null`, and is not an empty object:
|
|
187
|
-
if (data !== undefined && Object.keys(data).length !== 0 && errors) {
|
|
188
|
-
const [key] = Object.keys(data);
|
|
189
|
-
|
|
190
|
-
if (data[key]?.items) {
|
|
191
|
-
const flattenedResult = data[key]?.items.map(
|
|
192
|
-
(value: Record<string, any>) =>
|
|
193
|
-
flattenItems(modelIntrospection, name, value),
|
|
194
|
-
);
|
|
160
|
+
if (contextSpec !== undefined) {
|
|
161
|
+
requestArgs.unshift(contextSpec);
|
|
162
|
+
}
|
|
195
163
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
164
|
+
const basePromise = (client as BaseClient).graphql(
|
|
165
|
+
...requestArgs,
|
|
166
|
+
) as Promise<GraphQLResult>;
|
|
167
|
+
const extendedPromise = extendCancellability(basePromise, resultPromise);
|
|
168
|
+
const response = await extendedPromise;
|
|
169
|
+
|
|
170
|
+
if (response.data !== undefined) {
|
|
171
|
+
return processGraphQlResponse(
|
|
172
|
+
modelIntrospection,
|
|
173
|
+
name,
|
|
174
|
+
response,
|
|
175
|
+
args?.selectionSet,
|
|
176
|
+
modelInitializer,
|
|
177
|
+
);
|
|
208
178
|
}
|
|
179
|
+
} catch (error: any) {
|
|
180
|
+
/**
|
|
181
|
+
* The `data` type returned by `error` here could be:
|
|
182
|
+
* 1) `null`
|
|
183
|
+
* 2) an empty object
|
|
184
|
+
* 3) "populated" but with a `null` value:
|
|
185
|
+
* `data: { listByExampleId: null }`
|
|
186
|
+
* 4) an actual record:
|
|
187
|
+
* `data: { listByExampleId: items: [{ id: '1', ...etc } }]`
|
|
188
|
+
*/
|
|
189
|
+
const { data, errors } = error;
|
|
190
|
+
|
|
191
|
+
// `data` is not `null`, and is not an empty object:
|
|
192
|
+
if (data !== undefined && Object.keys(data).length !== 0 && errors) {
|
|
193
|
+
const [key] = Object.keys(data);
|
|
194
|
+
|
|
195
|
+
if (data[key]?.items) {
|
|
196
|
+
const flattenedResult = data[key]?.items.map(
|
|
197
|
+
(value: Record<string, any>) =>
|
|
198
|
+
flattenItems(modelIntrospection, name, value),
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Check exists since `flattenedResult` could be `null`.
|
|
203
|
+
* if `flattenedResult` exists, result is an actual record.
|
|
204
|
+
*/
|
|
205
|
+
if (flattenedResult) {
|
|
206
|
+
return {
|
|
207
|
+
data: args?.selectionSet
|
|
208
|
+
? flattenedResult
|
|
209
|
+
: modelInitializer(flattenedResult),
|
|
210
|
+
nextToken: data[key]?.nextToken,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
}
|
|
209
214
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
215
|
+
// response is of type `data: { listByExampleId: null }`
|
|
216
|
+
return {
|
|
217
|
+
data: data[key],
|
|
218
|
+
nextToken: data[key]?.nextToken,
|
|
219
|
+
};
|
|
220
|
+
} else {
|
|
221
|
+
// `data` is `null` or an empty object:
|
|
222
|
+
return handleListGraphQlError(error);
|
|
223
|
+
}
|
|
218
224
|
}
|
|
219
|
-
}
|
|
225
|
+
});
|
|
220
226
|
}
|
|
@@ -23,6 +23,9 @@ import {
|
|
|
23
23
|
} from '../APIClient';
|
|
24
24
|
|
|
25
25
|
import { handleListGraphQlError } from './utils';
|
|
26
|
+
import { selfAwareAsync } from '../../utils';
|
|
27
|
+
|
|
28
|
+
import { extendCancellability } from '../cancellation';
|
|
26
29
|
|
|
27
30
|
export function listFactory(
|
|
28
31
|
client: BaseClient,
|
|
@@ -31,7 +34,7 @@ export function listFactory(
|
|
|
31
34
|
getInternals: ClientInternalsGetter,
|
|
32
35
|
context = false,
|
|
33
36
|
) {
|
|
34
|
-
const listWithContext =
|
|
37
|
+
const listWithContext = (
|
|
35
38
|
contextSpec: AmplifyServer.ContextSpec,
|
|
36
39
|
args?: ListArgs,
|
|
37
40
|
) => {
|
|
@@ -45,14 +48,14 @@ export function listFactory(
|
|
|
45
48
|
);
|
|
46
49
|
};
|
|
47
50
|
|
|
48
|
-
const list =
|
|
51
|
+
const list = (args?: Record<string, any>) => {
|
|
49
52
|
return _list(client, modelIntrospection, model, getInternals, args);
|
|
50
53
|
};
|
|
51
54
|
|
|
52
55
|
return context ? listWithContext : list;
|
|
53
56
|
}
|
|
54
57
|
|
|
55
|
-
|
|
58
|
+
function _list(
|
|
56
59
|
client: BaseClient,
|
|
57
60
|
modelIntrospection: ModelIntrospectionSchema,
|
|
58
61
|
model: SchemaModel,
|
|
@@ -60,118 +63,65 @@ async function _list(
|
|
|
60
63
|
args?: ListArgs & AuthModeParams,
|
|
61
64
|
contextSpec?: AmplifyServer.ContextSpec,
|
|
62
65
|
) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const query = generateGraphQLDocument(modelIntrospection, name, 'LIST', args);
|
|
66
|
-
const variables = buildGraphQLVariables(
|
|
67
|
-
model,
|
|
68
|
-
'LIST',
|
|
69
|
-
args,
|
|
70
|
-
modelIntrospection,
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
const auth = authModeParams(client, getInternals, args);
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
const headers = getCustomHeaders(client, getInternals, args?.headers);
|
|
77
|
-
|
|
78
|
-
const { data, extensions } = contextSpec
|
|
79
|
-
? ((await (client as BaseSSRClient).graphql(
|
|
80
|
-
contextSpec,
|
|
81
|
-
{
|
|
82
|
-
...auth,
|
|
83
|
-
query,
|
|
84
|
-
variables,
|
|
85
|
-
},
|
|
86
|
-
headers,
|
|
87
|
-
)) as GraphQLResult)
|
|
88
|
-
: ((await (client as BaseBrowserClient).graphql(
|
|
89
|
-
{
|
|
90
|
-
...auth,
|
|
91
|
-
query,
|
|
92
|
-
variables,
|
|
93
|
-
},
|
|
94
|
-
headers,
|
|
95
|
-
)) as GraphQLResult);
|
|
96
|
-
|
|
97
|
-
// flatten response
|
|
98
|
-
if (data !== undefined) {
|
|
99
|
-
const [key] = Object.keys(data);
|
|
100
|
-
|
|
101
|
-
if (data[key].items) {
|
|
102
|
-
const flattenedResult = data[key].items.map(
|
|
103
|
-
(value: Record<string, any>) =>
|
|
104
|
-
flattenItems(modelIntrospection, name, value),
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
// don't init if custom selection set
|
|
108
|
-
if (args?.selectionSet) {
|
|
109
|
-
return {
|
|
110
|
-
data: flattenedResult,
|
|
111
|
-
nextToken: data[key].nextToken,
|
|
112
|
-
extensions,
|
|
113
|
-
};
|
|
114
|
-
} else {
|
|
115
|
-
const initialized = initializeModel(
|
|
116
|
-
client,
|
|
117
|
-
name,
|
|
118
|
-
flattenedResult,
|
|
119
|
-
modelIntrospection,
|
|
120
|
-
auth.authMode,
|
|
121
|
-
auth.authToken,
|
|
122
|
-
!!contextSpec,
|
|
123
|
-
);
|
|
66
|
+
return selfAwareAsync(async (resultPromise) => {
|
|
67
|
+
const { name } = model;
|
|
124
68
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
69
|
+
const query = generateGraphQLDocument(
|
|
70
|
+
modelIntrospection,
|
|
71
|
+
name,
|
|
72
|
+
'LIST',
|
|
73
|
+
args,
|
|
74
|
+
);
|
|
75
|
+
const variables = buildGraphQLVariables(
|
|
76
|
+
model,
|
|
77
|
+
'LIST',
|
|
78
|
+
args,
|
|
79
|
+
modelIntrospection,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const auth = authModeParams(client, getInternals, args);
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
const headers = getCustomHeaders(client, getInternals, args?.headers);
|
|
86
|
+
|
|
87
|
+
const basePromise = contextSpec
|
|
88
|
+
? ((client as BaseSSRClient).graphql(
|
|
89
|
+
contextSpec,
|
|
90
|
+
{
|
|
91
|
+
...auth,
|
|
92
|
+
query,
|
|
93
|
+
variables,
|
|
94
|
+
},
|
|
95
|
+
headers,
|
|
96
|
+
) as Promise<GraphQLResult>)
|
|
97
|
+
: ((client as BaseBrowserClient).graphql(
|
|
98
|
+
{
|
|
99
|
+
...auth,
|
|
100
|
+
query,
|
|
101
|
+
variables,
|
|
102
|
+
},
|
|
103
|
+
headers,
|
|
104
|
+
) as Promise<GraphQLResult>);
|
|
105
|
+
|
|
106
|
+
const extendedPromise = extendCancellability(basePromise, resultPromise);
|
|
107
|
+
const { data, extensions } = await extendedPromise;
|
|
108
|
+
|
|
109
|
+
// flatten response
|
|
110
|
+
if (data !== undefined) {
|
|
111
|
+
const [key] = Object.keys(data);
|
|
112
|
+
|
|
113
|
+
if (data[key].items) {
|
|
114
|
+
const flattenedResult = data[key].items.map(
|
|
115
|
+
(value: Record<string, any>) =>
|
|
116
|
+
flattenItems(modelIntrospection, name, value),
|
|
117
|
+
);
|
|
132
118
|
|
|
133
|
-
return {
|
|
134
|
-
data: data[key],
|
|
135
|
-
nextToken: data[key].nextToken,
|
|
136
|
-
extensions,
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
} catch (error: any) {
|
|
140
|
-
/**
|
|
141
|
-
* The `data` type returned by `error` here could be:
|
|
142
|
-
* 1) `null`
|
|
143
|
-
* 2) an empty object
|
|
144
|
-
* 3) "populated" but with a `null` value `data: { listPosts: null }`
|
|
145
|
-
* 4) actual records `data: { listPosts: items: [{ id: '1', ...etc }] }`
|
|
146
|
-
*/
|
|
147
|
-
const { data, errors } = error;
|
|
148
|
-
|
|
149
|
-
// `data` is not `null`, and is not an empty object:
|
|
150
|
-
if (
|
|
151
|
-
data !== undefined &&
|
|
152
|
-
data !== null &&
|
|
153
|
-
Object.keys(data).length !== 0 &&
|
|
154
|
-
errors
|
|
155
|
-
) {
|
|
156
|
-
const [key] = Object.keys(data);
|
|
157
|
-
|
|
158
|
-
if (data[key]?.items) {
|
|
159
|
-
const flattenedResult = data[key].items.map(
|
|
160
|
-
(value: Record<string, any>) =>
|
|
161
|
-
flattenItems(modelIntrospection, name, value),
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Check exists since `flattenedResult` could be `null`.
|
|
166
|
-
* if `flattenedResult` exists, result is an actual record.
|
|
167
|
-
*/
|
|
168
|
-
if (flattenedResult) {
|
|
169
119
|
// don't init if custom selection set
|
|
170
120
|
if (args?.selectionSet) {
|
|
171
121
|
return {
|
|
172
122
|
data: flattenedResult,
|
|
173
|
-
nextToken: data[key]
|
|
174
|
-
|
|
123
|
+
nextToken: data[key].nextToken,
|
|
124
|
+
extensions,
|
|
175
125
|
};
|
|
176
126
|
} else {
|
|
177
127
|
const initialized = initializeModel(
|
|
@@ -184,27 +134,90 @@ async function _list(
|
|
|
184
134
|
!!contextSpec,
|
|
185
135
|
);
|
|
186
136
|
|
|
187
|
-
// data is full record w/out selection set:
|
|
188
137
|
return {
|
|
189
138
|
data: initialized,
|
|
190
|
-
nextToken: data[key]
|
|
191
|
-
|
|
139
|
+
nextToken: data[key].nextToken,
|
|
140
|
+
extensions,
|
|
192
141
|
};
|
|
193
142
|
}
|
|
194
143
|
}
|
|
195
144
|
|
|
196
145
|
return {
|
|
197
146
|
data: data[key],
|
|
198
|
-
nextToken: data[key]
|
|
199
|
-
|
|
147
|
+
nextToken: data[key].nextToken,
|
|
148
|
+
extensions,
|
|
200
149
|
};
|
|
150
|
+
}
|
|
151
|
+
} catch (error: any) {
|
|
152
|
+
/**
|
|
153
|
+
* The `data` type returned by `error` here could be:
|
|
154
|
+
* 1) `null`
|
|
155
|
+
* 2) an empty object
|
|
156
|
+
* 3) "populated" but with a `null` value `data: { listPosts: null }`
|
|
157
|
+
* 4) actual records `data: { listPosts: items: [{ id: '1', ...etc }] }`
|
|
158
|
+
*/
|
|
159
|
+
const { data, errors } = error;
|
|
160
|
+
|
|
161
|
+
// `data` is not `null`, and is not an empty object:
|
|
162
|
+
if (
|
|
163
|
+
data !== undefined &&
|
|
164
|
+
data !== null &&
|
|
165
|
+
Object.keys(data).length !== 0 &&
|
|
166
|
+
errors
|
|
167
|
+
) {
|
|
168
|
+
const [key] = Object.keys(data);
|
|
169
|
+
|
|
170
|
+
if (data[key]?.items) {
|
|
171
|
+
const flattenedResult = data[key].items.map(
|
|
172
|
+
(value: Record<string, any>) =>
|
|
173
|
+
flattenItems(modelIntrospection, name, value),
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Check exists since `flattenedResult` could be `null`.
|
|
178
|
+
* if `flattenedResult` exists, result is an actual record.
|
|
179
|
+
*/
|
|
180
|
+
if (flattenedResult) {
|
|
181
|
+
// don't init if custom selection set
|
|
182
|
+
if (args?.selectionSet) {
|
|
183
|
+
return {
|
|
184
|
+
data: flattenedResult,
|
|
185
|
+
nextToken: data[key]?.nextToken,
|
|
186
|
+
errors,
|
|
187
|
+
};
|
|
188
|
+
} else {
|
|
189
|
+
const initialized = initializeModel(
|
|
190
|
+
client,
|
|
191
|
+
name,
|
|
192
|
+
flattenedResult,
|
|
193
|
+
modelIntrospection,
|
|
194
|
+
auth.authMode,
|
|
195
|
+
auth.authToken,
|
|
196
|
+
!!contextSpec,
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
// data is full record w/out selection set:
|
|
200
|
+
return {
|
|
201
|
+
data: initialized,
|
|
202
|
+
nextToken: data[key]?.nextToken,
|
|
203
|
+
errors,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return {
|
|
209
|
+
data: data[key],
|
|
210
|
+
nextToken: data[key]?.nextToken,
|
|
211
|
+
errors,
|
|
212
|
+
};
|
|
213
|
+
} else {
|
|
214
|
+
// response is of type `data: { getPost: null }`)
|
|
215
|
+
return handleListGraphQlError(error);
|
|
216
|
+
}
|
|
201
217
|
} else {
|
|
202
|
-
//
|
|
218
|
+
// `data` is `null` or an empty object:
|
|
203
219
|
return handleListGraphQlError(error);
|
|
204
220
|
}
|
|
205
|
-
} else {
|
|
206
|
-
// `data` is `null` or an empty object:
|
|
207
|
-
return handleListGraphQlError(error);
|
|
208
221
|
}
|
|
209
|
-
}
|
|
222
|
+
});
|
|
210
223
|
}
|
|
@@ -8,11 +8,14 @@ import {
|
|
|
8
8
|
ServerClientGenerationParams,
|
|
9
9
|
} from '../../bridge-types';
|
|
10
10
|
|
|
11
|
-
import { ModelOperation
|
|
11
|
+
import { ModelOperation } from '../APIClient';
|
|
12
12
|
import { listFactory } from '../operations/list';
|
|
13
13
|
import { indexQueryFactory } from '../operations/indexQuery';
|
|
14
14
|
import { getFactory } from '../operations/get';
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
getSecondaryIndexesFromSchemaModel,
|
|
17
|
+
excludeDisabledOps,
|
|
18
|
+
} from '../clientUtils';
|
|
16
19
|
|
|
17
20
|
export function generateModelsProperty<T extends Record<any, any> = never>(
|
|
18
21
|
client: BaseClient,
|
|
@@ -42,40 +45,40 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
|
|
|
42
45
|
'ONCREATE',
|
|
43
46
|
'ONUPDATE',
|
|
44
47
|
'ONDELETE',
|
|
45
|
-
'
|
|
48
|
+
'OBSERVEQUERY',
|
|
46
49
|
];
|
|
47
50
|
|
|
48
51
|
for (const model of Object.values(modelIntrospection.models)) {
|
|
49
52
|
const { name } = model;
|
|
50
53
|
models[name] = {} as Record<string, any>;
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
const enabledModelOps = excludeDisabledOps(modelIntrospection, name);
|
|
56
|
+
|
|
57
|
+
Object.entries(enabledModelOps).forEach(([key, { operationPrefix }]) => {
|
|
58
|
+
const operation = key as ModelOperation;
|
|
55
59
|
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
// subscriptions are not supported in SSR
|
|
61
|
+
if (SSR_UNSUPORTED_OPS.includes(operation)) return;
|
|
58
62
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
);
|
|
63
|
+
if (operation === 'LIST') {
|
|
64
|
+
models[name][operationPrefix] = listFactory(
|
|
65
|
+
client,
|
|
66
|
+
modelIntrospection,
|
|
67
|
+
model,
|
|
68
|
+
getInternals,
|
|
69
|
+
useContext,
|
|
70
|
+
);
|
|
71
|
+
} else {
|
|
72
|
+
models[name][operationPrefix] = getFactory(
|
|
73
|
+
client,
|
|
74
|
+
modelIntrospection,
|
|
75
|
+
model,
|
|
76
|
+
operation,
|
|
77
|
+
getInternals,
|
|
78
|
+
useContext,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
79
82
|
|
|
80
83
|
const secondaryIdxs = getSecondaryIndexesFromSchemaModel(model);
|
|
81
84
|
|