@aws-amplify/api-graphql 4.0.29-unstable.f37faeb.0 → 4.0.29
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/generateClient.js +4 -14
- package/dist/cjs/internals/generateClient.js.map +1 -1
- package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js +3 -8
- package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js.map +1 -1
- package/dist/cjs/internals/v6.js +6 -3
- package/dist/cjs/internals/v6.js.map +1 -1
- package/dist/cjs/types/index.js +11 -1
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/utils/index.js +1 -5
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/esm/GraphQLAPI.d.ts +1 -1
- package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/internals/InternalGraphQLAPI.d.ts +1 -1
- package/dist/esm/internals/generateClient.mjs +5 -15
- package/dist/esm/internals/generateClient.mjs.map +1 -1
- package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs +4 -9
- package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs.map +1 -1
- package/dist/esm/internals/types.d.ts +1 -1
- package/dist/esm/internals/v6.d.ts +1 -1
- package/dist/esm/internals/v6.mjs +7 -4
- package/dist/esm/internals/v6.mjs.map +1 -1
- package/dist/esm/types/index.d.ts +10 -40
- package/dist/esm/types/index.mjs +10 -1
- package/dist/esm/types/index.mjs.map +1 -1
- package/dist/esm/utils/index.d.ts +0 -2
- package/dist/esm/utils/index.mjs +0 -2
- package/dist/esm/utils/index.mjs.map +1 -1
- package/package.json +5 -6
- package/src/GraphQLAPI.ts +1 -1
- package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +4 -1
- package/src/internals/InternalGraphQLAPI.ts +4 -1
- package/src/internals/generateClient.ts +8 -36
- package/src/internals/server/generateClientWithAmplifyInstance.ts +6 -16
- package/src/internals/types.ts +1 -1
- package/src/internals/v6.ts +7 -7
- package/src/server/generateClient.ts +1 -1
- package/src/types/index.ts +36 -60
- package/src/utils/index.ts +0 -2
- package/dist/cjs/internals/APIClient.js +0 -733
- package/dist/cjs/internals/APIClient.js.map +0 -1
- package/dist/cjs/internals/clientUtils.js +0 -29
- package/dist/cjs/internals/clientUtils.js.map +0 -1
- package/dist/cjs/internals/generateCustomOperationsProperty.js +0 -50
- package/dist/cjs/internals/generateCustomOperationsProperty.js.map +0 -1
- package/dist/cjs/internals/operations/custom.js +0 -356
- package/dist/cjs/internals/operations/custom.js.map +0 -1
- package/dist/cjs/internals/operations/get.js +0 -62
- package/dist/cjs/internals/operations/get.js.map +0 -1
- package/dist/cjs/internals/operations/indexQuery.js +0 -75
- package/dist/cjs/internals/operations/indexQuery.js.map +0 -1
- package/dist/cjs/internals/operations/list.js +0 -74
- package/dist/cjs/internals/operations/list.js.map +0 -1
- package/dist/cjs/internals/operations/observeQuery.js +0 -121
- package/dist/cjs/internals/operations/observeQuery.js.map +0 -1
- package/dist/cjs/internals/operations/subscription.js +0 -31
- package/dist/cjs/internals/operations/subscription.js.map +0 -1
- package/dist/cjs/internals/server/generateModelsProperty.js +0 -53
- package/dist/cjs/internals/server/generateModelsProperty.js.map +0 -1
- package/dist/cjs/internals/utils/clientProperties/generateEnumsProperty.js +0 -21
- package/dist/cjs/internals/utils/clientProperties/generateEnumsProperty.js.map +0 -1
- package/dist/cjs/internals/utils/clientProperties/generateModelsProperty.js +0 -45
- package/dist/cjs/internals/utils/clientProperties/generateModelsProperty.js.map +0 -1
- package/dist/cjs/utils/findIndexByFields.js +0 -25
- package/dist/cjs/utils/findIndexByFields.js.map +0 -1
- package/dist/cjs/utils/resolveOwnerFields.js +0 -48
- package/dist/cjs/utils/resolveOwnerFields.js.map +0 -1
- package/dist/cjs/utils/resolvePKFields.js +0 -17
- package/dist/cjs/utils/resolvePKFields.js.map +0 -1
- package/dist/esm/internals/APIClient.d.ts +0 -126
- package/dist/esm/internals/APIClient.mjs +0 -721
- package/dist/esm/internals/APIClient.mjs.map +0 -1
- package/dist/esm/internals/clientUtils.d.ts +0 -6
- package/dist/esm/internals/clientUtils.mjs +0 -26
- package/dist/esm/internals/clientUtils.mjs.map +0 -1
- package/dist/esm/internals/generateCustomOperationsProperty.d.ts +0 -9
- package/dist/esm/internals/generateCustomOperationsProperty.mjs +0 -45
- package/dist/esm/internals/generateCustomOperationsProperty.mjs.map +0 -1
- package/dist/esm/internals/operations/custom.d.ts +0 -63
- package/dist/esm/internals/operations/custom.mjs +0 -354
- package/dist/esm/internals/operations/custom.mjs.map +0 -1
- package/dist/esm/internals/operations/get.d.ts +0 -5
- package/dist/esm/internals/operations/get.mjs +0 -60
- package/dist/esm/internals/operations/get.mjs.map +0 -1
- package/dist/esm/internals/operations/indexQuery.d.ts +0 -9
- package/dist/esm/internals/operations/indexQuery.mjs +0 -73
- package/dist/esm/internals/operations/indexQuery.mjs.map +0 -1
- package/dist/esm/internals/operations/list.d.ts +0 -4
- package/dist/esm/internals/operations/list.mjs +0 -72
- package/dist/esm/internals/operations/list.mjs.map +0 -1
- package/dist/esm/internals/operations/observeQuery.d.ts +0 -3
- package/dist/esm/internals/operations/observeQuery.mjs +0 -121
- package/dist/esm/internals/operations/observeQuery.mjs.map +0 -1
- package/dist/esm/internals/operations/subscription.d.ts +0 -3
- package/dist/esm/internals/operations/subscription.mjs +0 -29
- package/dist/esm/internals/operations/subscription.mjs.map +0 -1
- package/dist/esm/internals/server/generateModelsProperty.d.ts +0 -3
- package/dist/esm/internals/server/generateModelsProperty.mjs +0 -51
- package/dist/esm/internals/server/generateModelsProperty.mjs.map +0 -1
- package/dist/esm/internals/utils/clientProperties/generateEnumsProperty.d.ts +0 -3
- package/dist/esm/internals/utils/clientProperties/generateEnumsProperty.mjs +0 -18
- package/dist/esm/internals/utils/clientProperties/generateEnumsProperty.mjs.map +0 -1
- package/dist/esm/internals/utils/clientProperties/generateModelsProperty.d.ts +0 -4
- package/dist/esm/internals/utils/clientProperties/generateModelsProperty.mjs +0 -43
- package/dist/esm/internals/utils/clientProperties/generateModelsProperty.mjs.map +0 -1
- package/dist/esm/utils/findIndexByFields.d.ts +0 -9
- package/dist/esm/utils/findIndexByFields.mjs +0 -22
- package/dist/esm/utils/findIndexByFields.mjs.map +0 -1
- package/dist/esm/utils/resolveOwnerFields.d.ts +0 -12
- package/dist/esm/utils/resolveOwnerFields.mjs +0 -45
- package/dist/esm/utils/resolveOwnerFields.mjs.map +0 -1
- package/dist/esm/utils/resolvePKFields.d.ts +0 -13
- package/dist/esm/utils/resolvePKFields.mjs +0 -14
- package/dist/esm/utils/resolvePKFields.mjs.map +0 -1
- package/src/internals/APIClient.ts +0 -1076
- package/src/internals/clientUtils.ts +0 -37
- package/src/internals/generateCustomOperationsProperty.ts +0 -112
- package/src/internals/operations/custom.ts +0 -492
- package/src/internals/operations/get.ts +0 -140
- package/src/internals/operations/indexQuery.ts +0 -170
- package/src/internals/operations/list.ts +0 -135
- package/src/internals/operations/observeQuery.ts +0 -146
- package/src/internals/operations/subscription.ts +0 -75
- package/src/internals/server/generateModelsProperty.ts +0 -97
- package/src/internals/utils/clientProperties/generateEnumsProperty.ts +0 -34
- package/src/internals/utils/clientProperties/generateModelsProperty.ts +0 -81
- package/src/utils/findIndexByFields.ts +0 -31
- package/src/utils/resolveOwnerFields.ts +0 -77
- package/src/utils/resolvePKFields.ts +0 -23
|
@@ -1,140 +0,0 @@
|
|
|
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
|
-
ModelIntrospectionSchema,
|
|
6
|
-
SchemaModel,
|
|
7
|
-
} from '@aws-amplify/core/internals/utils';
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
ModelOperation,
|
|
11
|
-
authModeParams,
|
|
12
|
-
buildGraphQLVariables,
|
|
13
|
-
flattenItems,
|
|
14
|
-
generateGraphQLDocument,
|
|
15
|
-
getCustomHeaders,
|
|
16
|
-
initializeModel,
|
|
17
|
-
} from '../APIClient';
|
|
18
|
-
import {
|
|
19
|
-
AuthModeParams,
|
|
20
|
-
ClientWithModels,
|
|
21
|
-
GraphQLOptionsV6,
|
|
22
|
-
GraphQLResult,
|
|
23
|
-
ListArgs,
|
|
24
|
-
QueryArgs,
|
|
25
|
-
V6Client,
|
|
26
|
-
V6ClientSSRRequest,
|
|
27
|
-
} from '../../types';
|
|
28
|
-
|
|
29
|
-
export function getFactory(
|
|
30
|
-
client: ClientWithModels,
|
|
31
|
-
modelIntrospection: ModelIntrospectionSchema,
|
|
32
|
-
model: SchemaModel,
|
|
33
|
-
operation: ModelOperation,
|
|
34
|
-
useContext = false,
|
|
35
|
-
) {
|
|
36
|
-
const getWithContext = async (
|
|
37
|
-
contextSpec: AmplifyServer.ContextSpec & GraphQLOptionsV6<unknown, string>,
|
|
38
|
-
arg?: any,
|
|
39
|
-
options?: any,
|
|
40
|
-
) => {
|
|
41
|
-
return _get(
|
|
42
|
-
client,
|
|
43
|
-
modelIntrospection,
|
|
44
|
-
model,
|
|
45
|
-
arg,
|
|
46
|
-
options,
|
|
47
|
-
operation,
|
|
48
|
-
contextSpec,
|
|
49
|
-
);
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const get = async (arg?: any, options?: any) => {
|
|
53
|
-
return _get(client, modelIntrospection, model, arg, options, operation);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
return useContext ? getWithContext : get;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async function _get(
|
|
60
|
-
client: ClientWithModels,
|
|
61
|
-
modelIntrospection: ModelIntrospectionSchema,
|
|
62
|
-
model: SchemaModel,
|
|
63
|
-
arg: QueryArgs,
|
|
64
|
-
options: AuthModeParams & ListArgs,
|
|
65
|
-
operation: ModelOperation,
|
|
66
|
-
context?: AmplifyServer.ContextSpec,
|
|
67
|
-
) {
|
|
68
|
-
const { name } = model;
|
|
69
|
-
|
|
70
|
-
const query = generateGraphQLDocument(
|
|
71
|
-
modelIntrospection,
|
|
72
|
-
name,
|
|
73
|
-
operation,
|
|
74
|
-
options,
|
|
75
|
-
);
|
|
76
|
-
const variables = buildGraphQLVariables(
|
|
77
|
-
model,
|
|
78
|
-
operation,
|
|
79
|
-
arg,
|
|
80
|
-
modelIntrospection,
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
try {
|
|
84
|
-
const auth = authModeParams(client, options);
|
|
85
|
-
|
|
86
|
-
const headers = getCustomHeaders(client, options?.headers);
|
|
87
|
-
|
|
88
|
-
const { data, extensions } = context
|
|
89
|
-
? ((await (client as V6ClientSSRRequest<Record<string, any>>).graphql(
|
|
90
|
-
context,
|
|
91
|
-
{
|
|
92
|
-
...auth,
|
|
93
|
-
query,
|
|
94
|
-
variables,
|
|
95
|
-
},
|
|
96
|
-
headers,
|
|
97
|
-
)) as GraphQLResult<any>)
|
|
98
|
-
: ((await (client as V6Client<Record<string, any>>).graphql(
|
|
99
|
-
{
|
|
100
|
-
...auth,
|
|
101
|
-
query,
|
|
102
|
-
variables,
|
|
103
|
-
},
|
|
104
|
-
headers,
|
|
105
|
-
)) as GraphQLResult<any>);
|
|
106
|
-
|
|
107
|
-
// flatten response
|
|
108
|
-
if (data) {
|
|
109
|
-
const [key] = Object.keys(data);
|
|
110
|
-
const flattenedResult = flattenItems(data)[key];
|
|
111
|
-
|
|
112
|
-
if (options?.selectionSet) {
|
|
113
|
-
return { data: flattenedResult, extensions };
|
|
114
|
-
} else {
|
|
115
|
-
// TODO: refactor to avoid destructuring here
|
|
116
|
-
const [initialized] = initializeModel(
|
|
117
|
-
client,
|
|
118
|
-
name,
|
|
119
|
-
[flattenedResult],
|
|
120
|
-
modelIntrospection,
|
|
121
|
-
auth.authMode,
|
|
122
|
-
auth.authToken,
|
|
123
|
-
!!context,
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
return { data: initialized, extensions };
|
|
127
|
-
}
|
|
128
|
-
} else {
|
|
129
|
-
return { data: null, extensions };
|
|
130
|
-
}
|
|
131
|
-
} catch (error: any) {
|
|
132
|
-
if (error.errors) {
|
|
133
|
-
// graphql errors pass through
|
|
134
|
-
return error as any;
|
|
135
|
-
} else {
|
|
136
|
-
// non-graphql errors re re-thrown
|
|
137
|
-
throw error;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
@@ -1,170 +0,0 @@
|
|
|
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
|
-
ModelIntrospectionSchema,
|
|
6
|
-
SchemaModel,
|
|
7
|
-
} from '@aws-amplify/core/internals/utils';
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
authModeParams,
|
|
11
|
-
buildGraphQLVariables,
|
|
12
|
-
flattenItems,
|
|
13
|
-
generateGraphQLDocument,
|
|
14
|
-
getCustomHeaders,
|
|
15
|
-
initializeModel,
|
|
16
|
-
} from '../APIClient';
|
|
17
|
-
import {
|
|
18
|
-
AuthModeParams,
|
|
19
|
-
ClientWithModels,
|
|
20
|
-
GraphQLResult,
|
|
21
|
-
ListArgs,
|
|
22
|
-
QueryArgs,
|
|
23
|
-
V6ClientSSRRequest,
|
|
24
|
-
} from '../../types';
|
|
25
|
-
|
|
26
|
-
export interface IndexMeta {
|
|
27
|
-
queryField: string;
|
|
28
|
-
pk: string;
|
|
29
|
-
sk?: string[];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function indexQueryFactory(
|
|
33
|
-
client: ClientWithModels,
|
|
34
|
-
modelIntrospection: ModelIntrospectionSchema,
|
|
35
|
-
model: SchemaModel,
|
|
36
|
-
indexMeta: IndexMeta,
|
|
37
|
-
context = false,
|
|
38
|
-
) {
|
|
39
|
-
const indexQueryWithContext = async (
|
|
40
|
-
contextSpec: AmplifyServer.ContextSpec,
|
|
41
|
-
args: QueryArgs,
|
|
42
|
-
options?: ListArgs,
|
|
43
|
-
) => {
|
|
44
|
-
return _indexQuery(
|
|
45
|
-
client,
|
|
46
|
-
modelIntrospection,
|
|
47
|
-
model,
|
|
48
|
-
indexMeta,
|
|
49
|
-
{
|
|
50
|
-
...args,
|
|
51
|
-
...options,
|
|
52
|
-
},
|
|
53
|
-
contextSpec,
|
|
54
|
-
);
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const indexQuery = async (args: QueryArgs, options?: ListArgs) => {
|
|
58
|
-
return _indexQuery(client, modelIntrospection, model, indexMeta, {
|
|
59
|
-
...args,
|
|
60
|
-
...options,
|
|
61
|
-
});
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
return context ? indexQueryWithContext : indexQuery;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function processGraphQlResponse(
|
|
68
|
-
result: GraphQLResult<any>,
|
|
69
|
-
selectionSet: undefined | string[],
|
|
70
|
-
modelInitializer: (flattenedResult: any[]) => any[],
|
|
71
|
-
) {
|
|
72
|
-
const { data, extensions } = result;
|
|
73
|
-
|
|
74
|
-
const [key] = Object.keys(data);
|
|
75
|
-
|
|
76
|
-
if (data[key].items) {
|
|
77
|
-
const flattenedResult = flattenItems(data)[key];
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
data: selectionSet ? flattenedResult : modelInitializer(flattenedResult),
|
|
81
|
-
nextToken: data[key].nextToken,
|
|
82
|
-
extensions,
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return {
|
|
87
|
-
data: data[key],
|
|
88
|
-
nextToken: data[key].nextToken,
|
|
89
|
-
extensions,
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function handleGraphQlError(error: any) {
|
|
94
|
-
if (error.errors) {
|
|
95
|
-
// graphql errors pass through
|
|
96
|
-
return error as any;
|
|
97
|
-
} else {
|
|
98
|
-
// non-graphql errors re re-thrown
|
|
99
|
-
throw error;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async function _indexQuery(
|
|
104
|
-
client: ClientWithModels,
|
|
105
|
-
modelIntrospection: ModelIntrospectionSchema,
|
|
106
|
-
model: SchemaModel,
|
|
107
|
-
indexMeta: IndexMeta,
|
|
108
|
-
args?: ListArgs & AuthModeParams,
|
|
109
|
-
contextSpec?: AmplifyServer.ContextSpec,
|
|
110
|
-
) {
|
|
111
|
-
const { name } = model;
|
|
112
|
-
|
|
113
|
-
const query = generateGraphQLDocument(
|
|
114
|
-
modelIntrospection,
|
|
115
|
-
name,
|
|
116
|
-
'INDEX_QUERY',
|
|
117
|
-
args,
|
|
118
|
-
indexMeta,
|
|
119
|
-
);
|
|
120
|
-
const variables = buildGraphQLVariables(
|
|
121
|
-
model,
|
|
122
|
-
'INDEX_QUERY',
|
|
123
|
-
args,
|
|
124
|
-
modelIntrospection,
|
|
125
|
-
indexMeta,
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
const auth = authModeParams(client, args);
|
|
129
|
-
|
|
130
|
-
const modelInitializer = (flattenedResult: any[]) =>
|
|
131
|
-
initializeModel(
|
|
132
|
-
client,
|
|
133
|
-
name,
|
|
134
|
-
flattenedResult,
|
|
135
|
-
modelIntrospection,
|
|
136
|
-
auth.authMode,
|
|
137
|
-
auth.authToken,
|
|
138
|
-
!!contextSpec,
|
|
139
|
-
);
|
|
140
|
-
|
|
141
|
-
try {
|
|
142
|
-
const headers = getCustomHeaders(client, args?.headers);
|
|
143
|
-
|
|
144
|
-
const graphQlParams = {
|
|
145
|
-
...auth,
|
|
146
|
-
query,
|
|
147
|
-
variables,
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
const requestArgs: [any, any] = [graphQlParams, headers];
|
|
151
|
-
|
|
152
|
-
if (contextSpec !== undefined) {
|
|
153
|
-
requestArgs.unshift(contextSpec);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const response = (await (
|
|
157
|
-
client as V6ClientSSRRequest<Record<string, any>>
|
|
158
|
-
).graphql(...requestArgs)) as GraphQLResult<any>;
|
|
159
|
-
|
|
160
|
-
if (response.data !== undefined) {
|
|
161
|
-
return processGraphQlResponse(
|
|
162
|
-
response,
|
|
163
|
-
args?.selectionSet,
|
|
164
|
-
modelInitializer,
|
|
165
|
-
);
|
|
166
|
-
}
|
|
167
|
-
} catch (error: any) {
|
|
168
|
-
return handleGraphQlError(error);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
@@ -1,135 +0,0 @@
|
|
|
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
|
-
ModelIntrospectionSchema,
|
|
6
|
-
SchemaModel,
|
|
7
|
-
} from '@aws-amplify/core/internals/utils';
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
authModeParams,
|
|
11
|
-
buildGraphQLVariables,
|
|
12
|
-
flattenItems,
|
|
13
|
-
generateGraphQLDocument,
|
|
14
|
-
getCustomHeaders,
|
|
15
|
-
initializeModel,
|
|
16
|
-
} from '../APIClient';
|
|
17
|
-
import {
|
|
18
|
-
AuthModeParams,
|
|
19
|
-
ClientWithModels,
|
|
20
|
-
GraphQLResult,
|
|
21
|
-
ListArgs,
|
|
22
|
-
V6Client,
|
|
23
|
-
V6ClientSSRRequest,
|
|
24
|
-
} from '../../types';
|
|
25
|
-
|
|
26
|
-
export function listFactory(
|
|
27
|
-
client: ClientWithModels,
|
|
28
|
-
modelIntrospection: ModelIntrospectionSchema,
|
|
29
|
-
model: SchemaModel,
|
|
30
|
-
context = false,
|
|
31
|
-
) {
|
|
32
|
-
const listWithContext = async (
|
|
33
|
-
contextSpec: AmplifyServer.ContextSpec,
|
|
34
|
-
args?: ListArgs,
|
|
35
|
-
) => {
|
|
36
|
-
return _list(client, modelIntrospection, model, args, contextSpec);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const list = async (args?: any) => {
|
|
40
|
-
return _list(client, modelIntrospection, model, args);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return context ? listWithContext : list;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function _list(
|
|
47
|
-
client: ClientWithModels,
|
|
48
|
-
modelIntrospection: ModelIntrospectionSchema,
|
|
49
|
-
model: SchemaModel,
|
|
50
|
-
args?: ListArgs & AuthModeParams,
|
|
51
|
-
contextSpec?: AmplifyServer.ContextSpec,
|
|
52
|
-
) {
|
|
53
|
-
const { name } = model;
|
|
54
|
-
|
|
55
|
-
const query = generateGraphQLDocument(modelIntrospection, name, 'LIST', args);
|
|
56
|
-
const variables = buildGraphQLVariables(
|
|
57
|
-
model,
|
|
58
|
-
'LIST',
|
|
59
|
-
args,
|
|
60
|
-
modelIntrospection,
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
try {
|
|
64
|
-
const auth = authModeParams(client, args);
|
|
65
|
-
|
|
66
|
-
const headers = getCustomHeaders(client, args?.headers);
|
|
67
|
-
|
|
68
|
-
const { data, extensions } = contextSpec
|
|
69
|
-
? ((await (client as V6ClientSSRRequest<Record<string, any>>).graphql(
|
|
70
|
-
contextSpec,
|
|
71
|
-
{
|
|
72
|
-
...auth,
|
|
73
|
-
query,
|
|
74
|
-
variables,
|
|
75
|
-
},
|
|
76
|
-
headers,
|
|
77
|
-
)) as GraphQLResult<any>)
|
|
78
|
-
: ((await (client as V6Client<Record<string, any>>).graphql(
|
|
79
|
-
{
|
|
80
|
-
...auth,
|
|
81
|
-
query,
|
|
82
|
-
variables,
|
|
83
|
-
},
|
|
84
|
-
headers,
|
|
85
|
-
)) as GraphQLResult<any>);
|
|
86
|
-
|
|
87
|
-
// flatten response
|
|
88
|
-
if (data !== undefined) {
|
|
89
|
-
const [key] = Object.keys(data);
|
|
90
|
-
|
|
91
|
-
if (data[key].items) {
|
|
92
|
-
const flattenedResult = flattenItems(data)[key];
|
|
93
|
-
|
|
94
|
-
// don't init if custom selection set
|
|
95
|
-
if (args?.selectionSet) {
|
|
96
|
-
return {
|
|
97
|
-
data: flattenedResult,
|
|
98
|
-
nextToken: data[key].nextToken,
|
|
99
|
-
extensions,
|
|
100
|
-
};
|
|
101
|
-
} else {
|
|
102
|
-
const initialized = initializeModel(
|
|
103
|
-
client,
|
|
104
|
-
name,
|
|
105
|
-
flattenedResult,
|
|
106
|
-
modelIntrospection,
|
|
107
|
-
auth.authMode,
|
|
108
|
-
auth.authToken,
|
|
109
|
-
!!contextSpec,
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
return {
|
|
113
|
-
data: initialized,
|
|
114
|
-
nextToken: data[key].nextToken,
|
|
115
|
-
extensions,
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return {
|
|
121
|
-
data: data[key],
|
|
122
|
-
nextToken: data[key].nextToken,
|
|
123
|
-
extensions,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
} catch (error: any) {
|
|
127
|
-
if (error.errors) {
|
|
128
|
-
// graphql errors pass through
|
|
129
|
-
return error as any;
|
|
130
|
-
} else {
|
|
131
|
-
// non-graphql errors re re-thrown
|
|
132
|
-
throw error;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { Observable } from 'rxjs';
|
|
4
|
-
import { SchemaModel } from '@aws-amplify/core/internals/utils';
|
|
5
|
-
|
|
6
|
-
import { findIndexByFields, resolvePKFields } from '../../utils';
|
|
7
|
-
|
|
8
|
-
export function observeQueryFactory(models: any, model: SchemaModel) {
|
|
9
|
-
const { name } = model;
|
|
10
|
-
|
|
11
|
-
const observeQuery = (arg?: any) =>
|
|
12
|
-
new Observable(subscriber => {
|
|
13
|
-
// what we'll be sending to our subscribers
|
|
14
|
-
const items: object[] = [];
|
|
15
|
-
|
|
16
|
-
// To enqueue subscription messages while we collect our initial
|
|
17
|
-
// result set.
|
|
18
|
-
const messageQueue = [] as {
|
|
19
|
-
type: 'create' | 'update' | 'delete';
|
|
20
|
-
item: object;
|
|
21
|
-
}[];
|
|
22
|
-
|
|
23
|
-
// operation to take when message(s) arrive.
|
|
24
|
-
// this operation will be swapped out once initial "sync" is complete
|
|
25
|
-
// to immediately ingest messsages.
|
|
26
|
-
let receiveMessages = (...messages: typeof messageQueue) => {
|
|
27
|
-
return messageQueue.push(...messages);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
// start subscriptions
|
|
31
|
-
const onCreateSub = models[name].onCreate(arg).subscribe({
|
|
32
|
-
next(item: object) {
|
|
33
|
-
receiveMessages({ item, type: 'create' });
|
|
34
|
-
},
|
|
35
|
-
error(error: any) {
|
|
36
|
-
subscriber.error({ type: 'onCreate', error });
|
|
37
|
-
},
|
|
38
|
-
});
|
|
39
|
-
const onUpdateSub = models[name].onUpdate(arg).subscribe({
|
|
40
|
-
next(item: object) {
|
|
41
|
-
receiveMessages({ item, type: 'update' });
|
|
42
|
-
},
|
|
43
|
-
error(error: any) {
|
|
44
|
-
subscriber.error({ type: 'onUpdate', error });
|
|
45
|
-
},
|
|
46
|
-
});
|
|
47
|
-
const onDeleteSub = models[name].onDelete(arg).subscribe({
|
|
48
|
-
next(item: object) {
|
|
49
|
-
receiveMessages({ item, type: 'delete' });
|
|
50
|
-
},
|
|
51
|
-
error(error: any) {
|
|
52
|
-
subscriber.error({ type: 'onDelete', error });
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
// consumes a list of messages and sends a snapshot
|
|
57
|
-
function ingestMessages(messages: typeof messageQueue) {
|
|
58
|
-
for (const message of messages) {
|
|
59
|
-
const idx = findIndexByFields(message.item, items, pkFields as any);
|
|
60
|
-
switch (message.type) {
|
|
61
|
-
case 'create':
|
|
62
|
-
if (idx < 0) items.push(message.item);
|
|
63
|
-
break;
|
|
64
|
-
case 'update':
|
|
65
|
-
if (idx >= 0) items[idx] = message.item;
|
|
66
|
-
break;
|
|
67
|
-
case 'delete':
|
|
68
|
-
if (idx >= 0) items.splice(idx, 1);
|
|
69
|
-
break;
|
|
70
|
-
default:
|
|
71
|
-
console.error('Unrecognized message in observeQuery.', message);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
subscriber.next({
|
|
75
|
-
items,
|
|
76
|
-
isSynced: true,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const pkFields = resolvePKFields(model);
|
|
81
|
-
|
|
82
|
-
// initial results
|
|
83
|
-
(async () => {
|
|
84
|
-
let firstPage = true;
|
|
85
|
-
let nextToken: string | null = null;
|
|
86
|
-
while (!subscriber.closed && (firstPage || nextToken)) {
|
|
87
|
-
firstPage = false;
|
|
88
|
-
|
|
89
|
-
const {
|
|
90
|
-
data: page,
|
|
91
|
-
errors,
|
|
92
|
-
nextToken: _nextToken,
|
|
93
|
-
}: {
|
|
94
|
-
data: any;
|
|
95
|
-
errors: any;
|
|
96
|
-
nextToken: string | null;
|
|
97
|
-
} = await models[name].list({ ...arg, nextToken });
|
|
98
|
-
nextToken = _nextToken;
|
|
99
|
-
|
|
100
|
-
items.push(...page);
|
|
101
|
-
|
|
102
|
-
// if there are no more pages and no items we already know about
|
|
103
|
-
// that need to be merged in from sub, we're "synced"
|
|
104
|
-
const isSynced =
|
|
105
|
-
messageQueue.length === 0 &&
|
|
106
|
-
(nextToken === null || nextToken === undefined);
|
|
107
|
-
|
|
108
|
-
subscriber.next({
|
|
109
|
-
items,
|
|
110
|
-
isSynced,
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
if (Array.isArray(errors)) {
|
|
114
|
-
for (const error of errors) {
|
|
115
|
-
subscriber.error(error);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// play through the queue
|
|
121
|
-
if (messageQueue.length > 0) {
|
|
122
|
-
ingestMessages(messageQueue);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// switch the queue to write directly to the items collection
|
|
126
|
-
receiveMessages = (...messages: typeof messageQueue) => {
|
|
127
|
-
ingestMessages(messages);
|
|
128
|
-
|
|
129
|
-
return items.length;
|
|
130
|
-
};
|
|
131
|
-
})();
|
|
132
|
-
|
|
133
|
-
// when subscriber unsubscribes, tear down internal subs
|
|
134
|
-
return () => {
|
|
135
|
-
// 1. tear down internal subs
|
|
136
|
-
onCreateSub.unsubscribe();
|
|
137
|
-
onUpdateSub.unsubscribe();
|
|
138
|
-
onDeleteSub.unsubscribe();
|
|
139
|
-
|
|
140
|
-
// 2. there is no need to explicitly stop paging. instead, we
|
|
141
|
-
// just check `subscriber.closed` above before fetching each page.
|
|
142
|
-
};
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
return observeQuery;
|
|
146
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import { map } from 'rxjs';
|
|
5
|
-
import {
|
|
6
|
-
ModelIntrospectionSchema,
|
|
7
|
-
SchemaModel,
|
|
8
|
-
} from '@aws-amplify/core/internals/utils';
|
|
9
|
-
|
|
10
|
-
import { GraphqlSubscriptionResult, V6Client } from '../../types';
|
|
11
|
-
import {
|
|
12
|
-
ModelOperation,
|
|
13
|
-
authModeParams,
|
|
14
|
-
buildGraphQLVariables,
|
|
15
|
-
generateGraphQLDocument,
|
|
16
|
-
getCustomHeaders,
|
|
17
|
-
initializeModel,
|
|
18
|
-
} from '../APIClient';
|
|
19
|
-
|
|
20
|
-
export function subscriptionFactory(
|
|
21
|
-
client: any,
|
|
22
|
-
modelIntrospection: ModelIntrospectionSchema,
|
|
23
|
-
model: SchemaModel,
|
|
24
|
-
operation: ModelOperation,
|
|
25
|
-
) {
|
|
26
|
-
const { name } = model as any;
|
|
27
|
-
|
|
28
|
-
const subscription = (args?: any) => {
|
|
29
|
-
const query = generateGraphQLDocument(
|
|
30
|
-
modelIntrospection,
|
|
31
|
-
name,
|
|
32
|
-
operation,
|
|
33
|
-
args,
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
const variables = buildGraphQLVariables(
|
|
37
|
-
model,
|
|
38
|
-
operation,
|
|
39
|
-
args,
|
|
40
|
-
modelIntrospection,
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
const auth = authModeParams(client, args);
|
|
44
|
-
|
|
45
|
-
const headers = getCustomHeaders(client, args?.headers);
|
|
46
|
-
|
|
47
|
-
const observable = client.graphql(
|
|
48
|
-
{
|
|
49
|
-
...auth,
|
|
50
|
-
query,
|
|
51
|
-
variables,
|
|
52
|
-
},
|
|
53
|
-
headers,
|
|
54
|
-
) as GraphqlSubscriptionResult<object>;
|
|
55
|
-
|
|
56
|
-
return observable.pipe(
|
|
57
|
-
map(value => {
|
|
58
|
-
const [key] = Object.keys(value.data);
|
|
59
|
-
const data = (value.data as any)[key];
|
|
60
|
-
const [initialized] = initializeModel(
|
|
61
|
-
client as V6Client<Record<string, any>>,
|
|
62
|
-
name,
|
|
63
|
-
[data],
|
|
64
|
-
modelIntrospection,
|
|
65
|
-
auth.authMode,
|
|
66
|
-
auth.authToken,
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
return initialized;
|
|
70
|
-
}),
|
|
71
|
-
);
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
return subscription;
|
|
75
|
-
}
|