@aws-amplify/api-graphql 4.0.1-api-v6-models.c1977f8.0 → 4.0.1-api-v6-models.891fe0d.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/internals/server/package.json +8 -0
- package/lib/GraphQLAPI.d.ts +1 -1
- package/lib/GraphQLAPI.js +23 -28
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +467 -582
- package/lib/index.js +1 -1
- package/lib/internals/APIClient.d.ts +16 -1
- package/lib/internals/APIClient.js +232 -206
- package/lib/internals/InternalGraphQLAPI.d.ts +1 -1
- package/lib/internals/InternalGraphQLAPI.js +205 -247
- package/lib/internals/generateClient.js +14 -11
- package/lib/internals/generateModelsProperty.js +19 -302
- package/lib/internals/operations/get.d.ts +2 -0
- package/lib/internals/operations/get.js +59 -0
- package/lib/internals/operations/list.d.ts +2 -0
- package/lib/internals/operations/list.js +71 -0
- package/lib/internals/operations/observeQuery.d.ts +2 -0
- package/lib/internals/operations/observeQuery.js +119 -0
- package/lib/internals/operations/subscription.d.ts +1 -0
- package/lib/internals/operations/subscription.js +27 -0
- package/lib/internals/server/generateClient.d.ts +11 -0
- package/lib/internals/server/generateClient.js +30 -0
- package/lib/internals/server/generateModelsProperty.d.ts +3 -0
- package/lib/internals/server/generateModelsProperty.js +45 -0
- package/lib/internals/server/index.d.ts +1 -0
- package/lib/internals/server/index.js +7 -0
- package/lib/internals/types.d.ts +8 -0
- package/lib/internals/v6.js +3 -3
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/index.d.ts +41 -4
- package/lib/types/index.js +3 -1
- package/lib/utils/ConnectionStateMonitor.js +51 -64
- package/lib/utils/ReachabilityMonitor/index.js +2 -2
- package/lib/utils/ReachabilityMonitor/index.native.js +4 -6
- package/lib/utils/ReconnectionMonitor.js +20 -24
- package/lib/utils/errors/APIError.js +7 -11
- package/lib/utils/errors/assertValidationError.js +4 -4
- package/lib/utils/errors/validation.js +5 -6
- package/lib/utils/findIndexByFields.js +4 -9
- package/lib/utils/resolveConfig.js +9 -10
- package/lib/utils/resolveLibraryOptions.js +4 -5
- package/lib/utils/resolveOwnerFields.js +10 -33
- package/lib/utils/resolvePKFields.js +2 -3
- package/lib-esm/GraphQLAPI.d.ts +1 -1
- package/lib-esm/GraphQLAPI.js +23 -29
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +459 -575
- package/lib-esm/Providers/constants.js +10 -10
- package/lib-esm/internals/APIClient.d.ts +16 -1
- package/lib-esm/internals/APIClient.js +230 -205
- package/lib-esm/internals/InternalGraphQLAPI.d.ts +1 -1
- package/lib-esm/internals/InternalGraphQLAPI.js +201 -244
- package/lib-esm/internals/generateClient.js +12 -9
- package/lib-esm/internals/generateModelsProperty.js +19 -302
- package/lib-esm/internals/operations/get.d.ts +2 -0
- package/lib-esm/internals/operations/get.js +55 -0
- package/lib-esm/internals/operations/list.d.ts +2 -0
- package/lib-esm/internals/operations/list.js +67 -0
- package/lib-esm/internals/operations/observeQuery.d.ts +2 -0
- package/lib-esm/internals/operations/observeQuery.js +115 -0
- package/lib-esm/internals/operations/subscription.d.ts +1 -0
- package/lib-esm/internals/operations/subscription.js +23 -0
- package/lib-esm/internals/server/generateClient.d.ts +11 -0
- package/lib-esm/internals/server/generateClient.js +26 -0
- package/lib-esm/internals/server/generateModelsProperty.d.ts +3 -0
- package/lib-esm/internals/server/generateModelsProperty.js +41 -0
- package/lib-esm/internals/server/index.d.ts +1 -0
- package/lib-esm/internals/server/index.js +3 -0
- package/lib-esm/internals/types.d.ts +8 -0
- package/lib-esm/internals/v6.js +1 -1
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/index.d.ts +41 -4
- package/lib-esm/types/index.js +3 -1
- package/lib-esm/utils/ConnectionStateMonitor.js +49 -63
- package/lib-esm/utils/ReachabilityMonitor/index.js +1 -1
- package/lib-esm/utils/ReachabilityMonitor/index.native.js +1 -3
- package/lib-esm/utils/ReconnectionMonitor.js +19 -24
- package/lib-esm/utils/errors/APIError.js +6 -11
- package/lib-esm/utils/errors/assertValidationError.js +2 -2
- package/lib-esm/utils/errors/validation.js +5 -6
- package/lib-esm/utils/findIndexByFields.js +4 -9
- package/lib-esm/utils/resolveConfig.js +8 -9
- package/lib-esm/utils/resolveLibraryOptions.js +4 -5
- package/lib-esm/utils/resolveOwnerFields.js +10 -33
- package/lib-esm/utils/resolvePKFields.js +2 -3
- package/package.json +8 -7
- package/src/GraphQLAPI.ts +6 -2
- package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +6 -5
- package/src/internals/APIClient.ts +123 -36
- package/src/internals/InternalGraphQLAPI.ts +35 -10
- package/src/internals/generateClient.ts +3 -2
- package/src/internals/generateModelsProperty.ts +28 -314
- package/src/internals/operations/get.ts +121 -0
- package/src/internals/operations/list.ts +111 -0
- package/src/internals/operations/observeQuery.ts +140 -0
- package/src/internals/operations/subscription.ts +59 -0
- package/src/internals/server/generateClient.ts +45 -0
- package/src/internals/server/generateModelsProperty.ts +70 -0
- package/src/internals/server/index.ts +4 -0
- package/src/internals/types.ts +9 -0
- package/src/types/index.ts +71 -9
|
@@ -0,0 +1,121 @@
|
|
|
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
|
+
} from '../APIClient';
|
|
11
|
+
|
|
12
|
+
export function getFactory(
|
|
13
|
+
client,
|
|
14
|
+
modelIntrospection,
|
|
15
|
+
model,
|
|
16
|
+
operation,
|
|
17
|
+
context = false
|
|
18
|
+
) {
|
|
19
|
+
const getWithContext = async (
|
|
20
|
+
contextSpec: AmplifyServer.ContextSpec,
|
|
21
|
+
arg?: any,
|
|
22
|
+
options?: any
|
|
23
|
+
) => {
|
|
24
|
+
return _get(
|
|
25
|
+
client,
|
|
26
|
+
modelIntrospection,
|
|
27
|
+
model,
|
|
28
|
+
arg,
|
|
29
|
+
options,
|
|
30
|
+
operation,
|
|
31
|
+
contextSpec
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const get = async (arg?: any, options?: any) => {
|
|
36
|
+
return _get(
|
|
37
|
+
client,
|
|
38
|
+
modelIntrospection,
|
|
39
|
+
model,
|
|
40
|
+
arg,
|
|
41
|
+
options,
|
|
42
|
+
operation,
|
|
43
|
+
context
|
|
44
|
+
);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
return context ? getWithContext : get;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function _get(
|
|
51
|
+
client,
|
|
52
|
+
modelIntrospection,
|
|
53
|
+
model,
|
|
54
|
+
arg,
|
|
55
|
+
options,
|
|
56
|
+
operation,
|
|
57
|
+
context
|
|
58
|
+
) {
|
|
59
|
+
const { name } = model as any;
|
|
60
|
+
|
|
61
|
+
const query = generateGraphQLDocument(
|
|
62
|
+
modelIntrospection.models,
|
|
63
|
+
name,
|
|
64
|
+
operation,
|
|
65
|
+
options
|
|
66
|
+
);
|
|
67
|
+
const variables = buildGraphQLVariables(
|
|
68
|
+
model,
|
|
69
|
+
operation,
|
|
70
|
+
arg,
|
|
71
|
+
modelIntrospection
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
const auth = authModeParams(client, options);
|
|
76
|
+
const { data, extensions } = context
|
|
77
|
+
? ((await client.graphql(context, {
|
|
78
|
+
...auth,
|
|
79
|
+
query,
|
|
80
|
+
variables,
|
|
81
|
+
})) as any)
|
|
82
|
+
: ((await client.graphql({
|
|
83
|
+
...auth,
|
|
84
|
+
query,
|
|
85
|
+
variables,
|
|
86
|
+
})) as any);
|
|
87
|
+
|
|
88
|
+
// flatten response
|
|
89
|
+
if (data !== undefined) {
|
|
90
|
+
const [key] = Object.keys(data);
|
|
91
|
+
const flattenedResult = flattenItems(data)[key];
|
|
92
|
+
|
|
93
|
+
if (options?.selectionSet) {
|
|
94
|
+
return { data: flattenedResult, extensions };
|
|
95
|
+
} else {
|
|
96
|
+
// TODO: refactor to avoid destructuring here
|
|
97
|
+
const [initialized] = initializeModel(
|
|
98
|
+
client,
|
|
99
|
+
name,
|
|
100
|
+
[flattenedResult],
|
|
101
|
+
modelIntrospection,
|
|
102
|
+
auth.authMode,
|
|
103
|
+
auth.authToken,
|
|
104
|
+
context
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
return { data: initialized, extensions };
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
return { data: null, extensions };
|
|
111
|
+
}
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if (error.errors) {
|
|
114
|
+
// graphql errors pass through
|
|
115
|
+
return error as any;
|
|
116
|
+
} else {
|
|
117
|
+
// non-graphql errors re re-thrown
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
} from '../APIClient';
|
|
11
|
+
|
|
12
|
+
export function listFactory(
|
|
13
|
+
client,
|
|
14
|
+
modelIntrospection,
|
|
15
|
+
model,
|
|
16
|
+
context = false
|
|
17
|
+
) {
|
|
18
|
+
const listWithContext = async (
|
|
19
|
+
contextSpec: AmplifyServer.ContextSpec,
|
|
20
|
+
args?: any
|
|
21
|
+
) => {
|
|
22
|
+
return _list(client, modelIntrospection, model, args, contextSpec);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const list = async (args?: any) => {
|
|
26
|
+
return _list(client, modelIntrospection, model, args, context);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return context ? listWithContext : list;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function _list(client, modelIntrospection, model, args, context) {
|
|
33
|
+
const { name } = model as any;
|
|
34
|
+
|
|
35
|
+
const query = generateGraphQLDocument(
|
|
36
|
+
modelIntrospection.models,
|
|
37
|
+
name,
|
|
38
|
+
'LIST',
|
|
39
|
+
args
|
|
40
|
+
);
|
|
41
|
+
const variables = buildGraphQLVariables(
|
|
42
|
+
model,
|
|
43
|
+
'LIST',
|
|
44
|
+
args,
|
|
45
|
+
modelIntrospection
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const auth = authModeParams(client, args);
|
|
50
|
+
|
|
51
|
+
const { data, extensions } = context
|
|
52
|
+
? ((await client.graphql(context, {
|
|
53
|
+
...auth,
|
|
54
|
+
query,
|
|
55
|
+
variables,
|
|
56
|
+
})) as any)
|
|
57
|
+
: ((await client.graphql({
|
|
58
|
+
...auth,
|
|
59
|
+
query,
|
|
60
|
+
variables,
|
|
61
|
+
})) as any);
|
|
62
|
+
|
|
63
|
+
// flatten response
|
|
64
|
+
if (data !== undefined) {
|
|
65
|
+
const [key] = Object.keys(data);
|
|
66
|
+
|
|
67
|
+
if (data[key].items) {
|
|
68
|
+
const flattenedResult = flattenItems(data)[key];
|
|
69
|
+
|
|
70
|
+
// don't init if custom selection set
|
|
71
|
+
if (args?.selectionSet) {
|
|
72
|
+
return {
|
|
73
|
+
data: flattenedResult,
|
|
74
|
+
nextToken: data[key].nextToken,
|
|
75
|
+
extensions,
|
|
76
|
+
};
|
|
77
|
+
} else {
|
|
78
|
+
const initialized = initializeModel(
|
|
79
|
+
client,
|
|
80
|
+
name,
|
|
81
|
+
flattenedResult,
|
|
82
|
+
modelIntrospection,
|
|
83
|
+
auth.authMode,
|
|
84
|
+
auth.authToken,
|
|
85
|
+
context
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
data: initialized,
|
|
90
|
+
nextToken: data[key].nextToken,
|
|
91
|
+
extensions,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
data: data[key],
|
|
98
|
+
nextToken: data[key].nextToken,
|
|
99
|
+
extensions,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (error.errors) {
|
|
104
|
+
// graphql errors pass through
|
|
105
|
+
return error as any;
|
|
106
|
+
} else {
|
|
107
|
+
// non-graphql errors re re-thrown
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { Observable, map } from 'rxjs';
|
|
5
|
+
import { findIndexByFields, resolvePKFields } from '../../utils';
|
|
6
|
+
|
|
7
|
+
export function observeQueryFactory(models, model) {
|
|
8
|
+
const { name } = model as any;
|
|
9
|
+
|
|
10
|
+
const observeQuery = (arg?: any) =>
|
|
11
|
+
new Observable(subscriber => {
|
|
12
|
+
// what we'll be sending to our subscribers
|
|
13
|
+
const items: object[] = [];
|
|
14
|
+
|
|
15
|
+
// To enqueue subscription messages while we collect our initial
|
|
16
|
+
// result set.
|
|
17
|
+
const messageQueue = [] as {
|
|
18
|
+
type: 'create' | 'update' | 'delete';
|
|
19
|
+
item: object;
|
|
20
|
+
}[];
|
|
21
|
+
|
|
22
|
+
// operation to take when message(s) arrive.
|
|
23
|
+
// this operation will be swapped out once initial "sync" is complete
|
|
24
|
+
// to immediately ingest messsages.
|
|
25
|
+
let receiveMessages = (...messages: typeof messageQueue) => {
|
|
26
|
+
return messageQueue.push(...messages);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// start subscriptions
|
|
30
|
+
const onCreateSub = models[name].onCreate(arg).subscribe({
|
|
31
|
+
next(item) {
|
|
32
|
+
receiveMessages({ item, type: 'create' });
|
|
33
|
+
},
|
|
34
|
+
error(error) {
|
|
35
|
+
subscriber.error({ type: 'onCreate', error });
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
const onUpdateSub = models[name].onUpdate(arg).subscribe({
|
|
39
|
+
next(item) {
|
|
40
|
+
receiveMessages({ item, type: 'update' });
|
|
41
|
+
},
|
|
42
|
+
error(error) {
|
|
43
|
+
subscriber.error({ type: 'onUpdate', error });
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
const onDeleteSub = models[name].onDelete(arg).subscribe({
|
|
47
|
+
next(item) {
|
|
48
|
+
receiveMessages({ item, type: 'delete' });
|
|
49
|
+
},
|
|
50
|
+
error(error) {
|
|
51
|
+
subscriber.error({ type: 'onDelete', error });
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// consumes a list of messages and sends a snapshot
|
|
56
|
+
function ingestMessages(messages: typeof messageQueue) {
|
|
57
|
+
for (const message of messages) {
|
|
58
|
+
const idx = findIndexByFields(message.item, items, pkFields as any);
|
|
59
|
+
switch (message.type) {
|
|
60
|
+
case 'create':
|
|
61
|
+
if (idx < 0) items.push(message.item);
|
|
62
|
+
break;
|
|
63
|
+
case 'update':
|
|
64
|
+
if (idx >= 0) items[idx] = message.item;
|
|
65
|
+
break;
|
|
66
|
+
case 'delete':
|
|
67
|
+
if (idx >= 0) items.splice(idx, 1);
|
|
68
|
+
break;
|
|
69
|
+
default:
|
|
70
|
+
console.error('Unrecognized message in observeQuery.', message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
subscriber.next({
|
|
74
|
+
items,
|
|
75
|
+
isSynced: true,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const pkFields = resolvePKFields(model);
|
|
80
|
+
|
|
81
|
+
// initial results
|
|
82
|
+
(async () => {
|
|
83
|
+
let firstPage = true;
|
|
84
|
+
let nextToken: string | null = null;
|
|
85
|
+
while (!subscriber.closed && (firstPage || nextToken)) {
|
|
86
|
+
firstPage = false;
|
|
87
|
+
|
|
88
|
+
const {
|
|
89
|
+
data: page,
|
|
90
|
+
errors,
|
|
91
|
+
nextToken: _nextToken,
|
|
92
|
+
} = await models[name].list({ ...arg, nextToken });
|
|
93
|
+
nextToken = _nextToken;
|
|
94
|
+
|
|
95
|
+
items.push(...page);
|
|
96
|
+
|
|
97
|
+
// if there are no more pages and no items we already know about
|
|
98
|
+
// that need to be merged in from sub, we're "synced"
|
|
99
|
+
const isSynced =
|
|
100
|
+
messageQueue.length === 0 &&
|
|
101
|
+
(nextToken === null || nextToken === undefined);
|
|
102
|
+
|
|
103
|
+
subscriber.next({
|
|
104
|
+
items,
|
|
105
|
+
isSynced,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
if (Array.isArray(errors)) {
|
|
109
|
+
for (const error of errors) {
|
|
110
|
+
subscriber.error(error);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// play through the queue
|
|
116
|
+
if (messageQueue.length > 0) {
|
|
117
|
+
ingestMessages(messageQueue);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// switch the queue to write directly to the items collection
|
|
121
|
+
receiveMessages = (...messages: typeof messageQueue) => {
|
|
122
|
+
ingestMessages(messages);
|
|
123
|
+
return items.length;
|
|
124
|
+
};
|
|
125
|
+
})();
|
|
126
|
+
|
|
127
|
+
// when subscriber unsubscribes, tear down internal subs
|
|
128
|
+
return () => {
|
|
129
|
+
// 1. tear down internal subs
|
|
130
|
+
onCreateSub.unsubscribe();
|
|
131
|
+
onUpdateSub.unsubscribe();
|
|
132
|
+
onDeleteSub.unsubscribe();
|
|
133
|
+
|
|
134
|
+
// 2. there is no need to explicitly stop paging. instead, we
|
|
135
|
+
// just check `subscriber.closed` above before fetching each page.
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
return observeQuery;
|
|
140
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
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 { GraphqlSubscriptionResult } from '../../types';
|
|
6
|
+
import {
|
|
7
|
+
initializeModel,
|
|
8
|
+
generateGraphQLDocument,
|
|
9
|
+
buildGraphQLVariables,
|
|
10
|
+
authModeParams,
|
|
11
|
+
} from '../APIClient';
|
|
12
|
+
|
|
13
|
+
export function subscriptionFactory(
|
|
14
|
+
client,
|
|
15
|
+
modelIntrospection,
|
|
16
|
+
model,
|
|
17
|
+
operation
|
|
18
|
+
) {
|
|
19
|
+
const { name } = model as any;
|
|
20
|
+
|
|
21
|
+
const subscription = (args?: any) => {
|
|
22
|
+
const query = generateGraphQLDocument(
|
|
23
|
+
modelIntrospection.models,
|
|
24
|
+
name,
|
|
25
|
+
operation
|
|
26
|
+
);
|
|
27
|
+
const variables = buildGraphQLVariables(
|
|
28
|
+
model,
|
|
29
|
+
operation,
|
|
30
|
+
args,
|
|
31
|
+
modelIntrospection
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const auth = authModeParams(client, args);
|
|
35
|
+
|
|
36
|
+
const observable = client.graphql({
|
|
37
|
+
...auth,
|
|
38
|
+
query,
|
|
39
|
+
variables,
|
|
40
|
+
}) as GraphqlSubscriptionResult<object>;
|
|
41
|
+
|
|
42
|
+
return observable.pipe(
|
|
43
|
+
map(value => {
|
|
44
|
+
const [key] = Object.keys(value.data);
|
|
45
|
+
const [initialized] = initializeModel(
|
|
46
|
+
client,
|
|
47
|
+
name,
|
|
48
|
+
[value.data[key]],
|
|
49
|
+
modelIntrospection,
|
|
50
|
+
auth.authMode,
|
|
51
|
+
auth.authToken
|
|
52
|
+
);
|
|
53
|
+
return initialized;
|
|
54
|
+
})
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return subscription;
|
|
59
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { graphql, cancel, isCancelError } from '../';
|
|
5
|
+
import { generateModelsProperty } from './generateModelsProperty';
|
|
6
|
+
import {
|
|
7
|
+
__amplify,
|
|
8
|
+
__authMode,
|
|
9
|
+
__authToken,
|
|
10
|
+
V6ClientSSRRequest,
|
|
11
|
+
V6ClientSSRCookies,
|
|
12
|
+
ServerClientGenerationParams,
|
|
13
|
+
CommonPublicClientOptions,
|
|
14
|
+
} from '../../types';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @private
|
|
18
|
+
*
|
|
19
|
+
* Creates a client that can be used to make GraphQL requests, using a provided `AmplifyClassV6`
|
|
20
|
+
* compatible context object for config and auth fetching.
|
|
21
|
+
*
|
|
22
|
+
* @param params
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
export function generateClient<
|
|
26
|
+
T extends Record<any, any> = never,
|
|
27
|
+
ClientType extends
|
|
28
|
+
| V6ClientSSRRequest<T>
|
|
29
|
+
| V6ClientSSRCookies<T> = V6ClientSSRCookies<T>
|
|
30
|
+
>(
|
|
31
|
+
params: ServerClientGenerationParams & CommonPublicClientOptions
|
|
32
|
+
): ClientType {
|
|
33
|
+
const client = {
|
|
34
|
+
[__amplify]: params.amplify,
|
|
35
|
+
[__authMode]: params.authMode,
|
|
36
|
+
[__authToken]: params.authToken,
|
|
37
|
+
graphql,
|
|
38
|
+
cancel,
|
|
39
|
+
isCancelError,
|
|
40
|
+
} as any;
|
|
41
|
+
|
|
42
|
+
client.models = generateModelsProperty<T, ClientType>(client, params);
|
|
43
|
+
|
|
44
|
+
return client as ClientType;
|
|
45
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { graphQLOperationsInfo, ModelOperation } from '../APIClient';
|
|
5
|
+
import { ServerClientGenerationParams } from '../../types/';
|
|
6
|
+
import { V6ClientSSRRequest, V6ClientSSRCookies } from '../../types';
|
|
7
|
+
|
|
8
|
+
import { listFactory } from '../operations/list';
|
|
9
|
+
import { getFactory } from '../operations/get';
|
|
10
|
+
|
|
11
|
+
export function generateModelsProperty<
|
|
12
|
+
T extends Record<any, any> = never,
|
|
13
|
+
ClientType extends
|
|
14
|
+
| V6ClientSSRRequest<T>
|
|
15
|
+
| V6ClientSSRCookies<T> = V6ClientSSRCookies<T>
|
|
16
|
+
>(client: ClientType, params: ServerClientGenerationParams): ClientType {
|
|
17
|
+
const models = {} as any;
|
|
18
|
+
const config = params.config;
|
|
19
|
+
const useContext = client === null;
|
|
20
|
+
|
|
21
|
+
if (!config) {
|
|
22
|
+
// TODO: improve
|
|
23
|
+
throw new Error('generateModelsProperty cannot retrieve Amplify config');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const modelIntrospection = config.API?.GraphQL?.modelIntrospection;
|
|
27
|
+
if (!modelIntrospection) {
|
|
28
|
+
return {} as any;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const SSR_UNSUPORTED_OPS = [
|
|
32
|
+
'ONCREATE',
|
|
33
|
+
'ONUPDATE',
|
|
34
|
+
'ONDELETE',
|
|
35
|
+
'OBSERVE_QUERY',
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
for (const model of Object.values(modelIntrospection.models)) {
|
|
39
|
+
const { name } = model as any;
|
|
40
|
+
models[name] = {} as any;
|
|
41
|
+
|
|
42
|
+
Object.entries(graphQLOperationsInfo).forEach(
|
|
43
|
+
([key, { operationPrefix }]) => {
|
|
44
|
+
const operation = key as ModelOperation;
|
|
45
|
+
|
|
46
|
+
// subscriptions are not supported in SSR
|
|
47
|
+
if (SSR_UNSUPORTED_OPS.includes(operation)) return;
|
|
48
|
+
|
|
49
|
+
if (operation === 'LIST') {
|
|
50
|
+
models[name][operationPrefix] = listFactory(
|
|
51
|
+
client,
|
|
52
|
+
modelIntrospection,
|
|
53
|
+
model,
|
|
54
|
+
useContext
|
|
55
|
+
);
|
|
56
|
+
} else {
|
|
57
|
+
models[name][operationPrefix] = getFactory(
|
|
58
|
+
client,
|
|
59
|
+
modelIntrospection,
|
|
60
|
+
model,
|
|
61
|
+
operation,
|
|
62
|
+
useContext
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return models;
|
|
70
|
+
}
|
package/src/internals/types.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
4
|
+
import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* @private
|
|
@@ -9,4 +10,12 @@ import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
|
9
10
|
*/
|
|
10
11
|
export type ClientGenerationParams = {
|
|
11
12
|
amplify: AmplifyClassV6;
|
|
13
|
+
} & CommonPublicClientOptions;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Common options that can be used on public `generateClient()` interfaces.
|
|
17
|
+
*/
|
|
18
|
+
export type CommonPublicClientOptions = {
|
|
19
|
+
authMode?: GraphQLAuthMode;
|
|
20
|
+
authToken?: string;
|
|
12
21
|
};
|
package/src/types/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { AmplifyClassV6 } from '@aws-amplify/core';
|
|
3
|
+
import { AmplifyClassV6, ResourcesConfig } from '@aws-amplify/core';
|
|
4
4
|
import { ModelTypes } from '@aws-amplify/amplify-api-next-types-alpha';
|
|
5
5
|
import { Source, DocumentNode, GraphQLError } from 'graphql';
|
|
6
6
|
export { OperationTypeNode } from 'graphql';
|
|
@@ -10,10 +10,14 @@ import {
|
|
|
10
10
|
GraphQLAuthMode,
|
|
11
11
|
DocumentType,
|
|
12
12
|
} from '@aws-amplify/core/internals/utils';
|
|
13
|
+
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
|
|
14
|
+
|
|
13
15
|
export { CONTROL_MSG, ConnectionState } from './PubSub';
|
|
14
16
|
|
|
15
17
|
export { SelectionSet } from '@aws-amplify/amplify-api-next-types-alpha';
|
|
16
18
|
|
|
19
|
+
export { CommonPublicClientOptions } from '../internals/types';
|
|
20
|
+
|
|
17
21
|
/**
|
|
18
22
|
* Loose/Unknown options for raw GraphQLAPICategory `graphql()`.
|
|
19
23
|
*/
|
|
@@ -357,18 +361,76 @@ type ExcludeNeverFields<O> = {
|
|
|
357
361
|
};
|
|
358
362
|
|
|
359
363
|
export const __amplify = Symbol('amplify');
|
|
364
|
+
export const __authMode = Symbol('authMode');
|
|
365
|
+
export const __authToken = Symbol('authToken');
|
|
360
366
|
|
|
361
367
|
export type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
|
|
362
368
|
[__amplify]: AmplifyClassV6;
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
| {
|
|
367
|
-
[key: string]: string;
|
|
368
|
-
}
|
|
369
|
-
| undefined
|
|
370
|
-
) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
369
|
+
[__authMode]?: GraphQLAuthMode;
|
|
370
|
+
[__authToken]?: string;
|
|
371
|
+
graphql: GraphQLMethod;
|
|
371
372
|
cancel: (promise: Promise<any>, message?: string) => boolean;
|
|
372
373
|
isCancelError: (error: any) => boolean;
|
|
373
374
|
models: ModelTypes<T>;
|
|
374
375
|
}>;
|
|
376
|
+
|
|
377
|
+
export type V6ClientSSRRequest<T extends Record<any, any> = never> =
|
|
378
|
+
ExcludeNeverFields<{
|
|
379
|
+
[__amplify]: AmplifyClassV6;
|
|
380
|
+
[__authMode]?: GraphQLAuthMode;
|
|
381
|
+
[__authToken]?: string;
|
|
382
|
+
graphql: GraphQLMethodSSR;
|
|
383
|
+
cancel: (promise: Promise<any>, message?: string) => boolean;
|
|
384
|
+
isCancelError: (error: any) => boolean;
|
|
385
|
+
models: ModelTypes<T, 'REQUEST'>;
|
|
386
|
+
}>;
|
|
387
|
+
|
|
388
|
+
export type V6ClientSSRCookies<T extends Record<any, any> = never> =
|
|
389
|
+
ExcludeNeverFields<{
|
|
390
|
+
[__amplify]: AmplifyClassV6;
|
|
391
|
+
[__authMode]?: GraphQLAuthMode;
|
|
392
|
+
[__authToken]?: string;
|
|
393
|
+
graphql: GraphQLMethod;
|
|
394
|
+
cancel: (promise: Promise<any>, message?: string) => boolean;
|
|
395
|
+
isCancelError: (error: any) => boolean;
|
|
396
|
+
models: ModelTypes<T, 'COOKIES'>;
|
|
397
|
+
}>;
|
|
398
|
+
|
|
399
|
+
export type GraphQLMethod = <
|
|
400
|
+
FALLBACK_TYPES = unknown,
|
|
401
|
+
TYPED_GQL_STRING extends string = string
|
|
402
|
+
>(
|
|
403
|
+
options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
|
|
404
|
+
additionalHeaders?:
|
|
405
|
+
| {
|
|
406
|
+
[key: string]: string;
|
|
407
|
+
}
|
|
408
|
+
| undefined
|
|
409
|
+
) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
410
|
+
|
|
411
|
+
export type GraphQLMethodSSR = <
|
|
412
|
+
FALLBACK_TYPES = unknown,
|
|
413
|
+
TYPED_GQL_STRING extends string = string
|
|
414
|
+
>(
|
|
415
|
+
contextSpec: AmplifyServer.ContextSpec,
|
|
416
|
+
options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
|
|
417
|
+
additionalHeaders?:
|
|
418
|
+
| {
|
|
419
|
+
[key: string]: string;
|
|
420
|
+
}
|
|
421
|
+
| undefined
|
|
422
|
+
) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* @private
|
|
426
|
+
*
|
|
427
|
+
* The knobs available for configuring `server/generateClient` internally.
|
|
428
|
+
*/
|
|
429
|
+
export type ServerClientGenerationParams = {
|
|
430
|
+
amplify:
|
|
431
|
+
| null // null expected when used with `generateServerClient`
|
|
432
|
+
// closure expected with `generateServerClientUsingCookies`
|
|
433
|
+
| ((fn: (amplify: AmplifyClassV6) => Promise<any>) => Promise<any>);
|
|
434
|
+
// global env-sourced config use for retrieving modelIntro
|
|
435
|
+
config: ResourcesConfig;
|
|
436
|
+
};
|