@aws-amplify/api-graphql 0.0.0-unstable-1ca644b-20260128091132

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.
Files changed (257) hide show
  1. package/README.md +3 -0
  2. package/dist/cjs/GraphQLAPI.js +66 -0
  3. package/dist/cjs/GraphQLAPI.js.map +1 -0
  4. package/dist/cjs/Providers/AWSAppSyncEventsProvider/index.js +117 -0
  5. package/dist/cjs/Providers/AWSAppSyncEventsProvider/index.js.map +1 -0
  6. package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js +96 -0
  7. package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -0
  8. package/dist/cjs/Providers/AWSWebSocketProvider/appsyncUrl.js +113 -0
  9. package/dist/cjs/Providers/AWSWebSocketProvider/appsyncUrl.js.map +1 -0
  10. package/dist/cjs/Providers/AWSWebSocketProvider/authHeaders.js +99 -0
  11. package/dist/cjs/Providers/AWSWebSocketProvider/authHeaders.js.map +1 -0
  12. package/dist/cjs/Providers/AWSWebSocketProvider/index.js +693 -0
  13. package/dist/cjs/Providers/AWSWebSocketProvider/index.js.map +1 -0
  14. package/dist/cjs/Providers/constants.js +149 -0
  15. package/dist/cjs/Providers/constants.js.map +1 -0
  16. package/dist/cjs/index.js +18 -0
  17. package/dist/cjs/index.js.map +1 -0
  18. package/dist/cjs/internals/InternalGraphQLAPI.js +269 -0
  19. package/dist/cjs/internals/InternalGraphQLAPI.js.map +1 -0
  20. package/dist/cjs/internals/events/appsyncRequest.js +96 -0
  21. package/dist/cjs/internals/events/appsyncRequest.js.map +1 -0
  22. package/dist/cjs/internals/events/index.js +142 -0
  23. package/dist/cjs/internals/events/index.js.map +1 -0
  24. package/dist/cjs/internals/events/types.js +6 -0
  25. package/dist/cjs/internals/events/types.js.map +1 -0
  26. package/dist/cjs/internals/events/utils.js +57 -0
  27. package/dist/cjs/internals/events/utils.js.map +1 -0
  28. package/dist/cjs/internals/generateClient.js +86 -0
  29. package/dist/cjs/internals/generateClient.js.map +1 -0
  30. package/dist/cjs/internals/graphqlAuth.js +63 -0
  31. package/dist/cjs/internals/graphqlAuth.js.map +1 -0
  32. package/dist/cjs/internals/graphqlRequest.js +19 -0
  33. package/dist/cjs/internals/graphqlRequest.js.map +1 -0
  34. package/dist/cjs/internals/index.js +16 -0
  35. package/dist/cjs/internals/index.js.map +1 -0
  36. package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js +48 -0
  37. package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js.map +1 -0
  38. package/dist/cjs/internals/server/index.js +9 -0
  39. package/dist/cjs/internals/server/index.js.map +1 -0
  40. package/dist/cjs/internals/types.js +4 -0
  41. package/dist/cjs/internals/types.js.map +1 -0
  42. package/dist/cjs/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.js +10 -0
  43. package/dist/cjs/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.js.map +1 -0
  44. package/dist/cjs/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.js +10 -0
  45. package/dist/cjs/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.js.map +1 -0
  46. package/dist/cjs/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.js +14 -0
  47. package/dist/cjs/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.js.map +1 -0
  48. package/dist/cjs/internals/v6.js +155 -0
  49. package/dist/cjs/internals/v6.js.map +1 -0
  50. package/dist/cjs/server/generateClient.js +42 -0
  51. package/dist/cjs/server/generateClient.js.map +1 -0
  52. package/dist/cjs/server/index.js +9 -0
  53. package/dist/cjs/server/index.js.map +1 -0
  54. package/dist/cjs/types/PubSub.js +49 -0
  55. package/dist/cjs/types/PubSub.js.map +1 -0
  56. package/dist/cjs/types/index.js +34 -0
  57. package/dist/cjs/types/index.js.map +1 -0
  58. package/dist/cjs/utils/ConnectionStateMonitor.js +134 -0
  59. package/dist/cjs/utils/ConnectionStateMonitor.js.map +1 -0
  60. package/dist/cjs/utils/ReachabilityMonitor/index.js +10 -0
  61. package/dist/cjs/utils/ReachabilityMonitor/index.js.map +1 -0
  62. package/dist/cjs/utils/ReachabilityMonitor/index.native.js +12 -0
  63. package/dist/cjs/utils/ReachabilityMonitor/index.native.js.map +1 -0
  64. package/dist/cjs/utils/ReconnectionMonitor.js +71 -0
  65. package/dist/cjs/utils/ReconnectionMonitor.js.map +1 -0
  66. package/dist/cjs/utils/errors/GraphQLApiError.js +21 -0
  67. package/dist/cjs/utils/errors/GraphQLApiError.js.map +1 -0
  68. package/dist/cjs/utils/errors/assertValidationError.js +18 -0
  69. package/dist/cjs/utils/errors/assertValidationError.js.map +1 -0
  70. package/dist/cjs/utils/errors/constants.js +47 -0
  71. package/dist/cjs/utils/errors/constants.js.map +1 -0
  72. package/dist/cjs/utils/errors/createGraphQLResultWithError.js +15 -0
  73. package/dist/cjs/utils/errors/createGraphQLResultWithError.js.map +1 -0
  74. package/dist/cjs/utils/errors/index.js +16 -0
  75. package/dist/cjs/utils/errors/index.js.map +1 -0
  76. package/dist/cjs/utils/errors/repackageAuthError.js +38 -0
  77. package/dist/cjs/utils/errors/repackageAuthError.js.map +1 -0
  78. package/dist/cjs/utils/errors/validation.js +28 -0
  79. package/dist/cjs/utils/errors/validation.js.map +1 -0
  80. package/dist/cjs/utils/index.js +11 -0
  81. package/dist/cjs/utils/index.js.map +1 -0
  82. package/dist/cjs/utils/resolveConfig.js +32 -0
  83. package/dist/cjs/utils/resolveConfig.js.map +1 -0
  84. package/dist/cjs/utils/resolveLibraryOptions.js +16 -0
  85. package/dist/cjs/utils/resolveLibraryOptions.js.map +1 -0
  86. package/dist/esm/GraphQLAPI.d.ts +37 -0
  87. package/dist/esm/GraphQLAPI.mjs +63 -0
  88. package/dist/esm/GraphQLAPI.mjs.map +1 -0
  89. package/dist/esm/Providers/AWSAppSyncEventsProvider/index.d.ts +49 -0
  90. package/dist/esm/Providers/AWSAppSyncEventsProvider/index.mjs +115 -0
  91. package/dist/esm/Providers/AWSAppSyncEventsProvider/index.mjs.map +1 -0
  92. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +47 -0
  93. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs +94 -0
  94. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs.map +1 -0
  95. package/dist/esm/Providers/AWSWebSocketProvider/appsyncUrl.d.ts +21 -0
  96. package/dist/esm/Providers/AWSWebSocketProvider/appsyncUrl.mjs +107 -0
  97. package/dist/esm/Providers/AWSWebSocketProvider/appsyncUrl.mjs.map +1 -0
  98. package/dist/esm/Providers/AWSWebSocketProvider/authHeaders.d.ts +8 -0
  99. package/dist/esm/Providers/AWSWebSocketProvider/authHeaders.mjs +97 -0
  100. package/dist/esm/Providers/AWSWebSocketProvider/authHeaders.mjs.map +1 -0
  101. package/dist/esm/Providers/AWSWebSocketProvider/index.d.ts +99 -0
  102. package/dist/esm/Providers/AWSWebSocketProvider/index.mjs +691 -0
  103. package/dist/esm/Providers/AWSWebSocketProvider/index.mjs.map +1 -0
  104. package/dist/esm/Providers/constants.d.ts +135 -0
  105. package/dist/esm/Providers/constants.mjs +147 -0
  106. package/dist/esm/Providers/constants.mjs.map +1 -0
  107. package/dist/esm/index.d.ts +6 -0
  108. package/dist/esm/index.mjs +7 -0
  109. package/dist/esm/index.mjs.map +1 -0
  110. package/dist/esm/internals/InternalGraphQLAPI.d.ts +45 -0
  111. package/dist/esm/internals/InternalGraphQLAPI.mjs +270 -0
  112. package/dist/esm/internals/InternalGraphQLAPI.mjs.map +1 -0
  113. package/dist/esm/internals/events/appsyncRequest.d.ts +14 -0
  114. package/dist/esm/internals/events/appsyncRequest.mjs +96 -0
  115. package/dist/esm/internals/events/appsyncRequest.mjs.map +1 -0
  116. package/dist/esm/internals/events/index.d.ts +58 -0
  117. package/dist/esm/internals/events/index.mjs +139 -0
  118. package/dist/esm/internals/events/index.mjs.map +1 -0
  119. package/dist/esm/internals/events/types.d.ts +93 -0
  120. package/dist/esm/internals/events/types.mjs +2 -0
  121. package/dist/esm/internals/events/types.mjs.map +1 -0
  122. package/dist/esm/internals/events/utils.d.ts +16 -0
  123. package/dist/esm/internals/events/utils.mjs +53 -0
  124. package/dist/esm/internals/events/utils.mjs.map +1 -0
  125. package/dist/esm/internals/generateClient.d.ts +12 -0
  126. package/dist/esm/internals/generateClient.mjs +85 -0
  127. package/dist/esm/internals/generateClient.mjs.map +1 -0
  128. package/dist/esm/internals/graphqlAuth.d.ts +3 -0
  129. package/dist/esm/internals/graphqlAuth.mjs +64 -0
  130. package/dist/esm/internals/graphqlAuth.mjs.map +1 -0
  131. package/dist/esm/internals/graphqlRequest.d.ts +3 -0
  132. package/dist/esm/internals/graphqlRequest.mjs +18 -0
  133. package/dist/esm/internals/graphqlRequest.mjs.map +1 -0
  134. package/dist/esm/internals/index.d.ts +4 -0
  135. package/dist/esm/internals/index.mjs +4 -0
  136. package/dist/esm/internals/index.mjs.map +1 -0
  137. package/dist/esm/internals/server/generateClientWithAmplifyInstance.d.ts +13 -0
  138. package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs +61 -0
  139. package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs.map +1 -0
  140. package/dist/esm/internals/server/index.d.ts +1 -0
  141. package/dist/esm/internals/server/index.mjs +2 -0
  142. package/dist/esm/internals/server/index.mjs.map +1 -0
  143. package/dist/esm/internals/types.d.ts +34 -0
  144. package/dist/esm/internals/types.mjs +2 -0
  145. package/dist/esm/internals/types.mjs.map +1 -0
  146. package/dist/esm/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.d.ts +2 -0
  147. package/dist/esm/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.mjs +8 -0
  148. package/dist/esm/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.mjs.map +1 -0
  149. package/dist/esm/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.d.ts +8 -0
  150. package/dist/esm/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.mjs +8 -0
  151. package/dist/esm/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.mjs.map +1 -0
  152. package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.d.ts +2 -0
  153. package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.mjs +12 -0
  154. package/dist/esm/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.mjs.map +1 -0
  155. package/dist/esm/internals/v6.d.ts +100 -0
  156. package/dist/esm/internals/v6.mjs +152 -0
  157. package/dist/esm/internals/v6.mjs.map +1 -0
  158. package/dist/esm/server/generateClient.d.ts +21 -0
  159. package/dist/esm/server/generateClient.mjs +41 -0
  160. package/dist/esm/server/generateClient.mjs.map +1 -0
  161. package/dist/esm/server/index.d.ts +1 -0
  162. package/dist/esm/server/index.mjs +2 -0
  163. package/dist/esm/server/index.mjs.map +1 -0
  164. package/dist/esm/types/PubSub.d.ts +27 -0
  165. package/dist/esm/types/PubSub.mjs +47 -0
  166. package/dist/esm/types/PubSub.mjs.map +1 -0
  167. package/dist/esm/types/index.d.ts +369 -0
  168. package/dist/esm/types/index.mjs +30 -0
  169. package/dist/esm/types/index.mjs.map +1 -0
  170. package/dist/esm/utils/ConnectionStateMonitor.d.ts +69 -0
  171. package/dist/esm/utils/ConnectionStateMonitor.mjs +132 -0
  172. package/dist/esm/utils/ConnectionStateMonitor.mjs.map +1 -0
  173. package/dist/esm/utils/ReachabilityMonitor/index.d.ts +1 -0
  174. package/dist/esm/utils/ReachabilityMonitor/index.mjs +8 -0
  175. package/dist/esm/utils/ReachabilityMonitor/index.mjs.map +1 -0
  176. package/dist/esm/utils/ReachabilityMonitor/index.native.d.ts +1 -0
  177. package/dist/esm/utils/ReachabilityMonitor/index.native.mjs +9 -0
  178. package/dist/esm/utils/ReachabilityMonitor/index.native.mjs.map +1 -0
  179. package/dist/esm/utils/ReconnectionMonitor.d.ts +29 -0
  180. package/dist/esm/utils/ReconnectionMonitor.mjs +69 -0
  181. package/dist/esm/utils/ReconnectionMonitor.mjs.map +1 -0
  182. package/dist/esm/utils/errors/GraphQLApiError.d.ts +7 -0
  183. package/dist/esm/utils/errors/GraphQLApiError.mjs +19 -0
  184. package/dist/esm/utils/errors/GraphQLApiError.mjs.map +1 -0
  185. package/dist/esm/utils/errors/assertValidationError.d.ts +5 -0
  186. package/dist/esm/utils/errors/assertValidationError.mjs +17 -0
  187. package/dist/esm/utils/errors/assertValidationError.mjs.map +1 -0
  188. package/dist/esm/utils/errors/constants.d.ts +7 -0
  189. package/dist/esm/utils/errors/constants.mjs +46 -0
  190. package/dist/esm/utils/errors/constants.mjs.map +1 -0
  191. package/dist/esm/utils/errors/createGraphQLResultWithError.d.ts +2 -0
  192. package/dist/esm/utils/errors/createGraphQLResultWithError.mjs +13 -0
  193. package/dist/esm/utils/errors/createGraphQLResultWithError.mjs.map +1 -0
  194. package/dist/esm/utils/errors/index.d.ts +4 -0
  195. package/dist/esm/utils/errors/index.mjs +5 -0
  196. package/dist/esm/utils/errors/index.mjs.map +1 -0
  197. package/dist/esm/utils/errors/repackageAuthError.d.ts +7 -0
  198. package/dist/esm/utils/errors/repackageAuthError.mjs +36 -0
  199. package/dist/esm/utils/errors/repackageAuthError.mjs.map +1 -0
  200. package/dist/esm/utils/errors/validation.d.ts +7 -0
  201. package/dist/esm/utils/errors/validation.mjs +26 -0
  202. package/dist/esm/utils/errors/validation.mjs.map +1 -0
  203. package/dist/esm/utils/index.d.ts +2 -0
  204. package/dist/esm/utils/index.mjs +3 -0
  205. package/dist/esm/utils/index.mjs.map +1 -0
  206. package/dist/esm/utils/resolveConfig.d.ts +12 -0
  207. package/dist/esm/utils/resolveConfig.mjs +33 -0
  208. package/dist/esm/utils/resolveConfig.mjs.map +1 -0
  209. package/dist/esm/utils/resolveLibraryOptions.d.ts +11 -0
  210. package/dist/esm/utils/resolveLibraryOptions.mjs +13 -0
  211. package/dist/esm/utils/resolveLibraryOptions.mjs.map +1 -0
  212. package/internals/package.json +8 -0
  213. package/internals/server/package.json +8 -0
  214. package/package.json +99 -0
  215. package/server/package.json +7 -0
  216. package/src/GraphQLAPI.ts +97 -0
  217. package/src/Providers/AWSAppSyncEventsProvider/index.ts +218 -0
  218. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +185 -0
  219. package/src/Providers/AWSWebSocketProvider/appsyncUrl.ts +152 -0
  220. package/src/Providers/AWSWebSocketProvider/authHeaders.ts +146 -0
  221. package/src/Providers/AWSWebSocketProvider/index.ts +1028 -0
  222. package/src/Providers/constants.ts +154 -0
  223. package/src/index.ts +12 -0
  224. package/src/internals/InternalGraphQLAPI.ts +424 -0
  225. package/src/internals/events/appsyncRequest.ts +169 -0
  226. package/src/internals/events/index.ts +199 -0
  227. package/src/internals/events/types.ts +109 -0
  228. package/src/internals/events/utils.ts +77 -0
  229. package/src/internals/generateClient.ts +120 -0
  230. package/src/internals/graphqlAuth.ts +87 -0
  231. package/src/internals/graphqlRequest.ts +30 -0
  232. package/src/internals/index.ts +10 -0
  233. package/src/internals/server/generateClientWithAmplifyInstance.ts +73 -0
  234. package/src/internals/server/index.ts +4 -0
  235. package/src/internals/types.ts +42 -0
  236. package/src/internals/utils/runtimeTypeGuards/isApiGraphQLProviderConfig.ts +10 -0
  237. package/src/internals/utils/runtimeTypeGuards/isConfigureEventWithResourceConfig.ts +13 -0
  238. package/src/internals/utils/runtimeTypeGuards/isGraphQLResponseWithErrors.ts +15 -0
  239. package/src/internals/v6.ts +189 -0
  240. package/src/server/generateClient.ts +68 -0
  241. package/src/server/index.ts +4 -0
  242. package/src/types/PubSub.ts +57 -0
  243. package/src/types/index.ts +524 -0
  244. package/src/utils/ConnectionStateMonitor.ts +195 -0
  245. package/src/utils/ReachabilityMonitor/index.native.ts +7 -0
  246. package/src/utils/ReachabilityMonitor/index.ts +5 -0
  247. package/src/utils/ReconnectionMonitor.ts +77 -0
  248. package/src/utils/errors/GraphQLApiError.ts +21 -0
  249. package/src/utils/errors/assertValidationError.ts +19 -0
  250. package/src/utils/errors/constants.ts +56 -0
  251. package/src/utils/errors/createGraphQLResultWithError.ts +15 -0
  252. package/src/utils/errors/index.ts +7 -0
  253. package/src/utils/errors/repackageAuthError.ts +43 -0
  254. package/src/utils/errors/validation.ts +26 -0
  255. package/src/utils/index.ts +5 -0
  256. package/src/utils/resolveConfig.ts +46 -0
  257. package/src/utils/resolveLibraryOptions.ts +14 -0
@@ -0,0 +1,87 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
5
+ import { AmplifyClassV6 } from '@aws-amplify/core';
6
+
7
+ import { GraphQLApiError } from '../utils/errors';
8
+ import {
9
+ NO_API_KEY,
10
+ NO_AUTH_TOKEN_HEADER,
11
+ NO_SIGNED_IN_USER,
12
+ NO_VALID_AUTH_TOKEN,
13
+ NO_VALID_CREDENTIALS,
14
+ } from '../utils/errors/constants';
15
+
16
+ export async function headerBasedAuth(
17
+ amplify: AmplifyClassV6,
18
+ authMode: GraphQLAuthMode,
19
+ apiKey: string | undefined,
20
+ additionalHeaders: Record<string, string> = {},
21
+ ) {
22
+ let headers = {};
23
+
24
+ switch (authMode) {
25
+ case 'apiKey':
26
+ if (!apiKey) {
27
+ throw new GraphQLApiError(NO_API_KEY);
28
+ }
29
+ headers = {
30
+ 'X-Api-Key': apiKey,
31
+ };
32
+ break;
33
+ case 'iam': {
34
+ const session = await amplify.Auth.fetchAuthSession();
35
+ if (session.credentials === undefined) {
36
+ throw new GraphQLApiError(NO_VALID_CREDENTIALS);
37
+ }
38
+ break;
39
+ }
40
+ case 'oidc':
41
+ case 'userPool': {
42
+ let token: string | undefined;
43
+
44
+ try {
45
+ token = (
46
+ await amplify.Auth.fetchAuthSession()
47
+ ).tokens?.accessToken.toString();
48
+ } catch (e) {
49
+ // fetchAuthSession failed
50
+ throw new GraphQLApiError({
51
+ ...NO_SIGNED_IN_USER,
52
+ underlyingError: e,
53
+ });
54
+ }
55
+
56
+ // `fetchAuthSession()` succeeded but didn't return `tokens`.
57
+ // This may happen when unauthenticated access is enabled and there is
58
+ // no user signed in.
59
+ if (!token) {
60
+ throw new GraphQLApiError(NO_VALID_AUTH_TOKEN);
61
+ }
62
+
63
+ headers = {
64
+ Authorization: token,
65
+ };
66
+ break;
67
+ }
68
+ case 'lambda':
69
+ if (
70
+ typeof additionalHeaders === 'object' &&
71
+ !additionalHeaders.Authorization
72
+ ) {
73
+ throw new GraphQLApiError(NO_AUTH_TOKEN_HEADER);
74
+ }
75
+
76
+ headers = {
77
+ Authorization: additionalHeaders.Authorization,
78
+ };
79
+ break;
80
+ case 'none':
81
+ break;
82
+ default:
83
+ break;
84
+ }
85
+
86
+ return headers;
87
+ }
@@ -0,0 +1,30 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { AmplifyUrl } from '@aws-amplify/core/internals/utils';
5
+ import { AmplifyClassV6 } from '@aws-amplify/core';
6
+ import {
7
+ // cancel as cancelREST,
8
+ post,
9
+ // updateRequestToBeCancellable,
10
+ } from '@aws-amplify/api-rest/internals';
11
+
12
+ export async function graphqlRequest(
13
+ amplify: AmplifyClassV6,
14
+ url: string,
15
+ options: any,
16
+ abortController: AbortController,
17
+ _post?: typeof post,
18
+ ) {
19
+ const p = _post ?? post;
20
+
21
+ const { body: responseBody } = await p(amplify, {
22
+ url: new AmplifyUrl(url),
23
+ options,
24
+ abortController,
25
+ });
26
+
27
+ const response = await responseBody.json();
28
+
29
+ return response;
30
+ }
@@ -0,0 +1,10 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export {
4
+ InternalGraphQLAPI,
5
+ InternalGraphQLAPIClass,
6
+ } from './InternalGraphQLAPI';
7
+
8
+ export { graphql, cancel, isCancelError } from './v6';
9
+ export { generateClient } from './generateClient';
10
+ export { CommonPublicClientOptions, DefaultCommonClientOptions } from './types';
@@ -0,0 +1,73 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { addSchemaToClientWithInstance } from '@aws-amplify/data-schema/runtime';
5
+
6
+ import {
7
+ CommonPublicClientOptions,
8
+ ServerClientGenerationParams,
9
+ V6ClientSSRCookies,
10
+ V6ClientSSRRequest,
11
+ __amplify,
12
+ __apiKey,
13
+ __authMode,
14
+ __authToken,
15
+ __endpoint,
16
+ __headers,
17
+ getInternals,
18
+ } from '../../types';
19
+ import { isApiGraphQLConfig } from '../utils/runtimeTypeGuards/isApiGraphQLProviderConfig';
20
+ import { cancel, graphql, isCancelError } from '..';
21
+
22
+ /**
23
+ * @private
24
+ *
25
+ * Used internally by `adapter-nextjs` package.
26
+ *
27
+ * Creates a client that can be used to make GraphQL requests, using a provided `AmplifyClassV6`
28
+ * compatible context object for config and auth fetching.
29
+ *
30
+ * @param params
31
+ * @returns
32
+ */
33
+ export function generateClientWithAmplifyInstance<
34
+ T extends Record<any, any> = never,
35
+ ClientType extends
36
+ | V6ClientSSRRequest<T>
37
+ | V6ClientSSRCookies<T> = V6ClientSSRCookies<T>,
38
+ >(
39
+ params: ServerClientGenerationParams & CommonPublicClientOptions,
40
+ ): ClientType {
41
+ const client = {
42
+ [__amplify]: params.amplify,
43
+ [__authMode]: params.authMode,
44
+ [__authToken]: params.authToken,
45
+ [__apiKey]: 'apiKey' in params ? params.apiKey : undefined,
46
+ [__endpoint]: 'endpoint' in params ? params.endpoint : undefined,
47
+ [__headers]: params.headers,
48
+ graphql,
49
+ cancel,
50
+ isCancelError,
51
+ } as any;
52
+
53
+ const apiGraphqlConfig = params.config?.API?.GraphQL;
54
+
55
+ if (client[__endpoint]) {
56
+ if (!client[__authMode]) {
57
+ throw new Error(
58
+ 'generateClient() requires an explicit `authMode` when `endpoint` is provided.',
59
+ );
60
+ }
61
+ if (client[__authMode] === 'apiKey' && !client[__apiKey]) {
62
+ throw new Error(
63
+ "generateClient() requires an explicit `apiKey` when `endpoint` is provided and `authMode = 'apiKey'`.",
64
+ );
65
+ }
66
+ }
67
+
68
+ if (!client[__endpoint] && isApiGraphQLConfig(apiGraphqlConfig)) {
69
+ addSchemaToClientWithInstance<T>(client, params, getInternals);
70
+ }
71
+
72
+ return client as any;
73
+ }
@@ -0,0 +1,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { generateClientWithAmplifyInstance } from './generateClientWithAmplifyInstance';
@@ -0,0 +1,42 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { AmplifyClassV6 } from '@aws-amplify/core';
4
+ import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
5
+ import { CustomHeaders } from '@aws-amplify/data-schema/runtime';
6
+
7
+ /**
8
+ * @private
9
+ *
10
+ * The knobs available for configuring `generateClient` internally.
11
+ */
12
+ export type ClientGenerationParams = {
13
+ amplify: AmplifyClassV6;
14
+ } & CommonPublicClientOptions;
15
+
16
+ export interface DefaultCommonClientOptions {
17
+ endpoint?: never;
18
+ authMode?: GraphQLAuthMode;
19
+ apiKey?: string;
20
+ authToken?: string;
21
+ headers?: CustomHeaders;
22
+ }
23
+
24
+ /**
25
+ * Common options that can be used on public `generateClient()` interfaces.
26
+ */
27
+ export type CommonPublicClientOptions =
28
+ | DefaultCommonClientOptions
29
+ | {
30
+ endpoint: string;
31
+ authMode: 'apiKey';
32
+ apiKey: string;
33
+ authToken?: string;
34
+ headers?: CustomHeaders;
35
+ }
36
+ | {
37
+ endpoint: string;
38
+ authMode: Exclude<GraphQLAuthMode, 'apiKey'>;
39
+ apiKey?: string;
40
+ authToken?: string;
41
+ headers?: CustomHeaders;
42
+ };
@@ -0,0 +1,10 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { GraphQLProviderConfig } from '@aws-amplify/core/internals/utils';
5
+
6
+ export function isApiGraphQLConfig(
7
+ apiGraphQLConfig: GraphQLProviderConfig['GraphQL'] | undefined,
8
+ ): apiGraphQLConfig is GraphQLProviderConfig['GraphQL'] {
9
+ return apiGraphQLConfig !== undefined;
10
+ }
@@ -0,0 +1,13 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { HubCapsule, ResourcesConfig } from '@aws-amplify/core';
5
+
6
+ export function isConfigureEventWithResourceConfig(
7
+ payload: HubCapsule<'core', { event: string; data?: unknown }>['payload'],
8
+ ): payload is {
9
+ event: 'configure';
10
+ data: ResourcesConfig;
11
+ } {
12
+ return payload.event === 'configure';
13
+ }
@@ -0,0 +1,15 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { GraphQLResult } from '../../../types';
5
+
6
+ export function isGraphQLResponseWithErrors(
7
+ response: unknown,
8
+ ): response is GraphQLResult {
9
+ if (!response) {
10
+ return false;
11
+ }
12
+ const r = response as GraphQLResult;
13
+
14
+ return Array.isArray(r.errors) && r.errors.length > 0;
15
+ }
@@ -0,0 +1,189 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { CustomHeaders } from '@aws-amplify/data-schema/runtime';
4
+
5
+ import { GraphQLAPI } from '../GraphQLAPI';
6
+ import {
7
+ CommonPublicClientOptions,
8
+ GraphQLOptions,
9
+ GraphQLOptionsV6,
10
+ GraphQLResponseV6,
11
+ V6Client,
12
+ getInternals,
13
+ } from '../types';
14
+
15
+ /**
16
+ * Invokes graphql operations against a graphql service, providing correct input and
17
+ * output types if Amplify-generated graphql from a recent version of the CLI/codegen
18
+ * are used *or* correct typing is provided via the type argument.
19
+ *
20
+ * Amplify-generated "branded" graphql queries will look similar to this:
21
+ *
22
+ * ```ts
23
+ * //
24
+ * // |-- branding
25
+ * // v
26
+ * export const getModel = `...` as GeneratedQuery<
27
+ * GetModelQueryVariables,
28
+ * GetModelQuery
29
+ * >;
30
+ * ```
31
+ *
32
+ * If this branding is not in your generated graphql, update to a newer version of
33
+ * CLI/codegen and regenerate your graphql using `amplify codegen`.
34
+ *
35
+ * ## Using Amplify-generated graphql
36
+ *
37
+ * ```ts
38
+ * import * as queries from './graphql/queries';
39
+ *
40
+ * //
41
+ * // |-- correctly typed graphql response containing a Widget
42
+ * // v
43
+ * const queryResult = await graphql({
44
+ * query: queries.getWidget,
45
+ * variables: {
46
+ * id: "abc", // <-- type hinted/enforced
47
+ * },
48
+ * });
49
+ *
50
+ * //
51
+ * // |-- a correctly typed Widget
52
+ * // v
53
+ * const fetchedWidget = queryResult.data?.getWidget!;
54
+ * ```
55
+ *
56
+ * ## Custom input + result types
57
+ *
58
+ * To provide input types (`variables`) and result types:
59
+ *
60
+ * ```ts
61
+ * type GetById_NameOnly = {
62
+ * variables: {
63
+ * id: string
64
+ * },
65
+ * result: Promise<{
66
+ * data: { getWidget: { name: string } }
67
+ * }>
68
+ * }
69
+ *
70
+ * //
71
+ * // |-- type is GetById_NameOnly["result"]
72
+ * // v
73
+ * const result = graphql<GetById_NameOnly>({
74
+ * query: "...",
75
+ * variables: { id: "abc" } // <-- type of GetById_NameOnly["variables"]
76
+ * });
77
+ * ```
78
+ *
79
+ * ## Custom result type only
80
+ *
81
+ * To specify result types only, use a type that is *not* in the `{variables, result}` shape:
82
+ *
83
+ * ```ts
84
+ * type MyResultType = Promise<{
85
+ * data: {
86
+ * getWidget: { name: string }
87
+ * }
88
+ * }>
89
+ *
90
+ * //
91
+ * // |-- type is MyResultType
92
+ * // v
93
+ * const result = graphql<MyResultType>({query: "..."});
94
+ * ```
95
+ *
96
+ * @param options
97
+ * @param additionalHeaders
98
+ */
99
+ export function graphql<
100
+ FALLBACK_TYPES = unknown,
101
+ TYPED_GQL_STRING extends string = string,
102
+ Options extends CommonPublicClientOptions = object,
103
+ >(
104
+ this: V6Client,
105
+ options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING, Options>,
106
+ additionalHeaders?: CustomHeaders,
107
+ ): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING> {
108
+ // inject client-level auth
109
+ const internals = getInternals(this);
110
+
111
+ /**
112
+ * The custom `endpoint` specific to the client
113
+ */
114
+ const clientEndpoint = internals.endpoint;
115
+
116
+ /**
117
+ * The `authMode` specific to the client.
118
+ */
119
+ const clientAuthMode = internals.authMode;
120
+
121
+ /**
122
+ * The `apiKey` specific to the client.
123
+ */
124
+ const clientApiKey = internals.apiKey;
125
+
126
+ /**
127
+ * The most specific `authMode` wins. Setting an `endpoint` value without also
128
+ * setting an `authMode` value is invalid. This helps to prevent customers apps
129
+ * from unexpectedly sending auth details to endpoints the auth details do not
130
+ * belong to.
131
+ *
132
+ * This is especially pronounced for `apiKey`. When both an `endpoint` *and*
133
+ * `authMode: 'apiKey'` are provided, an explicit `apiKey` override is required
134
+ * to prevent inadvertent sending of an API's `apiKey` to an endpoint is does
135
+ * not belong to.
136
+ */
137
+ options.authMode = options.authMode || clientAuthMode;
138
+ options.apiKey = options.apiKey ?? clientApiKey;
139
+ options.authToken = options.authToken || internals.authToken;
140
+
141
+ if (clientEndpoint && options.authMode === 'apiKey' && !options.apiKey) {
142
+ throw new Error(
143
+ "graphql() requires an explicit `apiKey` for a custom `endpoint` when `authMode = 'apiKey'`.",
144
+ );
145
+ }
146
+
147
+ const headers = additionalHeaders || internals.headers;
148
+
149
+ /**
150
+ * The correctness of these typings depends on correct string branding or overrides.
151
+ * Neither of these can actually be validated at runtime. Hence, we don't perform
152
+ * any validation or type-guarding here.
153
+ */
154
+ const result = GraphQLAPI.graphql(
155
+ // TODO: move V6Client back into this package?
156
+ internals.amplify as any,
157
+ {
158
+ ...options,
159
+ endpoint: clientEndpoint,
160
+ } as GraphQLOptions,
161
+ headers,
162
+ );
163
+
164
+ return result as any;
165
+ }
166
+
167
+ /**
168
+ * Cancels an inflight request. Only applicable for graphql queries and mutations
169
+ * @param {any} request - request to cancel
170
+ * @returns - A boolean indicating if the request was cancelled
171
+ */
172
+ export function cancel(
173
+ this: V6Client,
174
+ promise: Promise<any>,
175
+ message?: string,
176
+ ): boolean {
177
+ return GraphQLAPI.cancel(promise, message);
178
+ }
179
+
180
+ /**
181
+ * Checks to see if an error thrown is from an api request cancellation
182
+ * @param {any} error - Any error
183
+ * @returns - A boolean indicating if the error was from an api request cancellation
184
+ */
185
+ export function isCancelError(this: V6Client, error: any): boolean {
186
+ return GraphQLAPI.isCancelError(error);
187
+ }
188
+
189
+ export { GraphQLOptionsV6, GraphQLResponseV6 };
@@ -0,0 +1,68 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import {
5
+ AmplifyServer,
6
+ getAmplifyServerContext,
7
+ } from '@aws-amplify/core/internals/adapter-core';
8
+ import { ResourcesConfig } from '@aws-amplify/core';
9
+ import { CustomHeaders } from '@aws-amplify/data-schema/runtime';
10
+
11
+ import { generateClientWithAmplifyInstance } from '../internals/server';
12
+ import {
13
+ GenerateServerClientParams,
14
+ GraphQLMethod,
15
+ GraphQLMethodSSR,
16
+ GraphQLOptionsV6,
17
+ V6ClientSSRRequest,
18
+ __amplify,
19
+ } from '../types';
20
+
21
+ /**
22
+ * Generates an GraphQL API client that works with Amplify server context.
23
+ *
24
+ * @example
25
+ * import config from './amplifyconfiguration.json';
26
+ * import { listPosts } from './graphql/queries';
27
+ *
28
+ * const client = generateServerClient({ config });
29
+ *
30
+ * const result = await runWithAmplifyServerContext({
31
+ * nextServerContext: { request, response },
32
+ * operation: (contextSpec) => client.graphql(contextSpec, {
33
+ * query: listPosts,
34
+ * }),
35
+ * });
36
+ */
37
+ export function generateClient<
38
+ T extends Record<any, any> = never,
39
+ Options extends GenerateServerClientParams = { config: ResourcesConfig },
40
+ >(options: Options): V6ClientSSRRequest<T, Options> {
41
+ // passing `null` instance because each (future model) method must retrieve a valid instance
42
+ // from server context
43
+ const client = generateClientWithAmplifyInstance<T, V6ClientSSRRequest<T>>({
44
+ amplify: null,
45
+ ...options,
46
+ });
47
+
48
+ // TODO: improve this and the next type
49
+ const prevGraphql = client.graphql as unknown as GraphQLMethod<Options>;
50
+
51
+ const wrappedGraphql = (
52
+ contextSpec: AmplifyServer.ContextSpec,
53
+ innerOptions: GraphQLOptionsV6<unknown, string, Options>,
54
+ additionalHeaders?: CustomHeaders,
55
+ ) => {
56
+ const amplifyInstance = getAmplifyServerContext(contextSpec).amplify;
57
+
58
+ return prevGraphql.call(
59
+ { [__amplify]: amplifyInstance },
60
+ innerOptions,
61
+ additionalHeaders,
62
+ );
63
+ };
64
+
65
+ client.graphql = wrappedGraphql as unknown as GraphQLMethodSSR<Options>;
66
+
67
+ return client;
68
+ }
@@ -0,0 +1,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { generateClient } from './generateClient';
@@ -0,0 +1,57 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { Observer } from 'rxjs';
4
+
5
+ export interface SubscriptionObserver<T> {
6
+ closed: boolean;
7
+ next(value: T): void;
8
+ error(errorValue: any): void;
9
+ complete(): void;
10
+ }
11
+
12
+ export enum CONTROL_MSG {
13
+ CONNECTION_CLOSED = 'Connection closed',
14
+ CONNECTION_FAILED = 'Connection failed',
15
+ REALTIME_SUBSCRIPTION_INIT_ERROR = 'AppSync Realtime subscription init error',
16
+ SUBSCRIPTION_ACK = 'Subscription ack',
17
+ TIMEOUT_DISCONNECT = 'Timeout disconnect',
18
+ }
19
+
20
+ /** @enum {string} */
21
+ export enum ConnectionState {
22
+ /*
23
+ * The connection is alive and healthy
24
+ */
25
+ Connected = 'Connected',
26
+ /*
27
+ * The connection is alive, but the connection is offline
28
+ */
29
+ ConnectedPendingNetwork = 'ConnectedPendingNetwork',
30
+ /*
31
+ * The connection has been disconnected while in use
32
+ */
33
+ ConnectionDisrupted = 'ConnectionDisrupted',
34
+ /*
35
+ * The connection has been disconnected and the network is offline
36
+ */
37
+ ConnectionDisruptedPendingNetwork = 'ConnectionDisruptedPendingNetwork',
38
+ /*
39
+ * The connection is in the process of connecting
40
+ */
41
+ Connecting = 'Connecting',
42
+ /*
43
+ * The connection is not in use and is being disconnected
44
+ */
45
+ ConnectedPendingDisconnect = 'ConnectedPendingDisconnect',
46
+ /*
47
+ * The connection is not in use and has been disconnected
48
+ */
49
+ Disconnected = 'Disconnected',
50
+ /*
51
+ * The connection is alive, but a keep alive message has been missed
52
+ */
53
+ ConnectedPendingKeepAlive = 'ConnectedPendingKeepAlive',
54
+ }
55
+
56
+ export type PubSubContent = Record<string, unknown>;
57
+ export type PubSubContentObserver = Observer<PubSubContent>;