@aws-amplify/api-graphql 4.0.16-unstable.5e30055.0 → 4.0.16
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/internals/APIClient.js +54 -4
- package/dist/cjs/internals/APIClient.js.map +1 -1
- package/dist/cjs/internals/clientUtils.js +29 -0
- package/dist/cjs/internals/clientUtils.js.map +1 -0
- package/dist/cjs/internals/generateModelsProperty.js +6 -0
- package/dist/cjs/internals/generateModelsProperty.js.map +1 -1
- package/dist/cjs/internals/operations/indexQuery.js +75 -0
- package/dist/cjs/internals/operations/indexQuery.js.map +1 -0
- package/dist/cjs/internals/server/generateModelsProperty.js +6 -0
- package/dist/cjs/internals/server/generateModelsProperty.js.map +1 -1
- package/dist/esm/internals/APIClient.d.ts +34 -29
- package/dist/esm/internals/APIClient.mjs +54 -4
- package/dist/esm/internals/APIClient.mjs.map +1 -1
- package/dist/esm/internals/clientUtils.d.ts +6 -0
- package/dist/esm/internals/clientUtils.mjs +26 -0
- package/dist/esm/internals/clientUtils.mjs.map +1 -0
- package/dist/esm/internals/generateModelsProperty.mjs +6 -0
- package/dist/esm/internals/generateModelsProperty.mjs.map +1 -1
- package/dist/esm/internals/operations/indexQuery.d.ts +9 -0
- package/dist/esm/internals/operations/indexQuery.mjs +73 -0
- package/dist/esm/internals/operations/indexQuery.mjs.map +1 -0
- package/dist/esm/internals/server/generateModelsProperty.mjs +6 -0
- package/dist/esm/internals/server/generateModelsProperty.mjs.map +1 -1
- package/package.json +6 -6
- package/src/internals/APIClient.ts +116 -51
- package/src/internals/clientUtils.ts +37 -0
- package/src/internals/generateModelsProperty.ts +17 -6
- package/src/internals/operations/indexQuery.ts +169 -0
- package/src/internals/server/generateModelsProperty.ts +16 -3
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
|
|
4
|
+
import {
|
|
5
|
+
initializeModel,
|
|
6
|
+
generateGraphQLDocument,
|
|
7
|
+
buildGraphQLVariables,
|
|
8
|
+
flattenItems,
|
|
9
|
+
authModeParams,
|
|
10
|
+
getCustomHeaders,
|
|
11
|
+
} from '../APIClient';
|
|
12
|
+
import {
|
|
13
|
+
AuthModeParams,
|
|
14
|
+
ClientWithModels,
|
|
15
|
+
ListArgs,
|
|
16
|
+
V6ClientSSRRequest,
|
|
17
|
+
GraphQLResult,
|
|
18
|
+
QueryArgs,
|
|
19
|
+
} from '../../types';
|
|
20
|
+
import {
|
|
21
|
+
ModelIntrospectionSchema,
|
|
22
|
+
SchemaModel,
|
|
23
|
+
} from '@aws-amplify/core/internals/utils';
|
|
24
|
+
|
|
25
|
+
export type IndexMeta = {
|
|
26
|
+
queryField: string;
|
|
27
|
+
pk: string;
|
|
28
|
+
sk?: string[];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export function indexQueryFactory(
|
|
32
|
+
client: ClientWithModels,
|
|
33
|
+
modelIntrospection: ModelIntrospectionSchema,
|
|
34
|
+
model: SchemaModel,
|
|
35
|
+
indexMeta: IndexMeta,
|
|
36
|
+
context = false,
|
|
37
|
+
) {
|
|
38
|
+
const indexQueryWithContext = async (
|
|
39
|
+
contextSpec: AmplifyServer.ContextSpec,
|
|
40
|
+
args: QueryArgs,
|
|
41
|
+
options?: ListArgs,
|
|
42
|
+
) => {
|
|
43
|
+
return _indexQuery(
|
|
44
|
+
client,
|
|
45
|
+
modelIntrospection,
|
|
46
|
+
model,
|
|
47
|
+
indexMeta,
|
|
48
|
+
{
|
|
49
|
+
...args,
|
|
50
|
+
...options,
|
|
51
|
+
},
|
|
52
|
+
contextSpec,
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const indexQuery = async (args: QueryArgs, options?: ListArgs) => {
|
|
57
|
+
return _indexQuery(client, modelIntrospection, model, indexMeta, {
|
|
58
|
+
...args,
|
|
59
|
+
...options,
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return context ? indexQueryWithContext : indexQuery;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function processGraphQlResponse(
|
|
67
|
+
result: GraphQLResult<any>,
|
|
68
|
+
selectionSet: undefined | string[],
|
|
69
|
+
modelInitializer: (flattenedResult: any[]) => any[],
|
|
70
|
+
) {
|
|
71
|
+
const { data, extensions } = result;
|
|
72
|
+
|
|
73
|
+
const [key] = Object.keys(data);
|
|
74
|
+
|
|
75
|
+
if (data[key].items) {
|
|
76
|
+
const flattenedResult = flattenItems(data)[key];
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
data: selectionSet ? flattenedResult : modelInitializer(flattenedResult),
|
|
80
|
+
nextToken: data[key].nextToken,
|
|
81
|
+
extensions,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
data: data[key],
|
|
87
|
+
nextToken: data[key].nextToken,
|
|
88
|
+
extensions,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function handleGraphQlError(error: any) {
|
|
93
|
+
if (error.errors) {
|
|
94
|
+
// graphql errors pass through
|
|
95
|
+
return error as any;
|
|
96
|
+
} else {
|
|
97
|
+
// non-graphql errors re re-thrown
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function _indexQuery(
|
|
103
|
+
client: ClientWithModels,
|
|
104
|
+
modelIntrospection: ModelIntrospectionSchema,
|
|
105
|
+
model: SchemaModel,
|
|
106
|
+
indexMeta: IndexMeta,
|
|
107
|
+
args?: ListArgs & AuthModeParams,
|
|
108
|
+
contextSpec?: AmplifyServer.ContextSpec,
|
|
109
|
+
) {
|
|
110
|
+
const { name } = model;
|
|
111
|
+
|
|
112
|
+
const query = generateGraphQLDocument(
|
|
113
|
+
modelIntrospection.models,
|
|
114
|
+
name,
|
|
115
|
+
'INDEX_QUERY',
|
|
116
|
+
args,
|
|
117
|
+
indexMeta,
|
|
118
|
+
);
|
|
119
|
+
const variables = buildGraphQLVariables(
|
|
120
|
+
model,
|
|
121
|
+
'INDEX_QUERY',
|
|
122
|
+
args,
|
|
123
|
+
modelIntrospection,
|
|
124
|
+
indexMeta,
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const auth = authModeParams(client, args);
|
|
128
|
+
|
|
129
|
+
const modelInitializer = (flattenedResult: any[]) =>
|
|
130
|
+
initializeModel(
|
|
131
|
+
client,
|
|
132
|
+
name,
|
|
133
|
+
flattenedResult,
|
|
134
|
+
modelIntrospection,
|
|
135
|
+
auth.authMode,
|
|
136
|
+
auth.authToken,
|
|
137
|
+
!!contextSpec,
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
const headers = getCustomHeaders(client, args?.headers);
|
|
142
|
+
|
|
143
|
+
const graphQlParams = {
|
|
144
|
+
...auth,
|
|
145
|
+
query,
|
|
146
|
+
variables,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const requestArgs: [any, any] = [graphQlParams, headers];
|
|
150
|
+
|
|
151
|
+
if (contextSpec !== undefined) {
|
|
152
|
+
requestArgs.unshift(contextSpec);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const response = (await (
|
|
156
|
+
client as V6ClientSSRRequest<Record<string, any>>
|
|
157
|
+
).graphql(...requestArgs)) as GraphQLResult<any>;
|
|
158
|
+
|
|
159
|
+
if (response.data !== undefined) {
|
|
160
|
+
return processGraphQlResponse(
|
|
161
|
+
response,
|
|
162
|
+
args?.selectionSet,
|
|
163
|
+
modelInitializer,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
} catch (error: any) {
|
|
167
|
+
return handleGraphQlError(error);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -7,7 +7,9 @@ import { V6ClientSSRRequest, V6ClientSSRCookies } from '../../types';
|
|
|
7
7
|
import { ModelIntrospectionSchema } from '@aws-amplify/core/internals/utils';
|
|
8
8
|
|
|
9
9
|
import { listFactory } from '../operations/list';
|
|
10
|
+
import { indexQueryFactory } from '../operations/indexQuery';
|
|
10
11
|
import { getFactory } from '../operations/get';
|
|
12
|
+
import { getSecondaryIndexesFromSchemaModel } from '../clientUtils';
|
|
11
13
|
|
|
12
14
|
export function generateModelsProperty<
|
|
13
15
|
_T extends Record<any, any> = never,
|
|
@@ -59,7 +61,7 @@ export function generateModelsProperty<
|
|
|
59
61
|
client,
|
|
60
62
|
modelIntrospection,
|
|
61
63
|
model,
|
|
62
|
-
useContext
|
|
64
|
+
useContext,
|
|
63
65
|
);
|
|
64
66
|
} else {
|
|
65
67
|
models[name][operationPrefix] = getFactory(
|
|
@@ -67,11 +69,22 @@ export function generateModelsProperty<
|
|
|
67
69
|
modelIntrospection,
|
|
68
70
|
model,
|
|
69
71
|
operation,
|
|
70
|
-
useContext
|
|
72
|
+
useContext,
|
|
71
73
|
);
|
|
72
74
|
}
|
|
73
|
-
}
|
|
75
|
+
},
|
|
74
76
|
);
|
|
77
|
+
|
|
78
|
+
const secondaryIdxs = getSecondaryIndexesFromSchemaModel(model);
|
|
79
|
+
|
|
80
|
+
for (const idx of secondaryIdxs) {
|
|
81
|
+
models[name][idx.queryField] = indexQueryFactory(
|
|
82
|
+
client,
|
|
83
|
+
modelIntrospection,
|
|
84
|
+
model,
|
|
85
|
+
idx,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
75
88
|
}
|
|
76
89
|
|
|
77
90
|
return models;
|