@aws-amplify/api-graphql 3.4.8 → 3.4.9-api-v6.d339f98.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.
@@ -1,11 +1,15 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { Source, DocumentNode, GraphQLError, OperationTypeNode } from 'graphql';
3
+ import { Source, DocumentNode, GraphQLError } from 'graphql';
4
4
  export { OperationTypeNode } from 'graphql';
5
5
  import { GRAPHQL_AUTH_MODE } from '@aws-amplify/auth';
6
6
  export { GRAPHQL_AUTH_MODE };
7
- import { CustomUserAgentDetails } from '@aws-amplify/core';
7
+ import { Observable } from 'zen-observable-ts';
8
+ import { AWSAppSyncRealTimeProvider } from '@aws-amplify/pubsub';
8
9
 
10
+ /**
11
+ * Loose/Unknown options for raw GraphQLAPICategory `graphql()`.
12
+ */
9
13
  export interface GraphQLOptions {
10
14
  query: string | DocumentNode;
11
15
  variables?: object;
@@ -25,6 +29,59 @@ export interface GraphQLResult<T = object> {
25
29
  };
26
30
  }
27
31
 
32
+ // Opaque type used for determining the graphql query type
33
+ declare const queryType: unique symbol;
34
+
35
+ export type GraphQLQuery<T> = T & { readonly [queryType]: 'query' };
36
+ export type GraphQLSubscription<T> = T & {
37
+ readonly [queryType]: 'subscription';
38
+ };
39
+
40
+ /**
41
+ * The return value from a `graphql({query})` call when `query` is a subscription.
42
+ *
43
+ * ```ts
44
+ * // |-- You are here
45
+ * // v
46
+ * const subResult: GraphqlSubscriptionResult<T> = client.graphql({
47
+ * query: onCreateWidget
48
+ * });
49
+ *
50
+ * const sub = subResult.subscribe({
51
+ * //
52
+ * // |-- You are here
53
+ * // v
54
+ * next(message: GraphqlSubscriptionMessage<OnCreateWidgetSubscription>) {
55
+ * handle(message.value); // <-- type OnCreateWidgetSubscription
56
+ * }
57
+ * })
58
+ * ```
59
+ */
60
+ export type GraphqlSubscriptionResult<T> = Observable<
61
+ GraphqlSubscriptionMessage<T>
62
+ >;
63
+
64
+ /**
65
+ * The shape of messages passed to `next()` from a graphql subscription. E.g.,
66
+ *
67
+ * ```ts
68
+ * const sub = client.graphql({
69
+ * query: onCreateWidget,
70
+ * }).subscribe({
71
+ * //
72
+ * // |-- You are here
73
+ * // v
74
+ * next(message: GraphqlSubscriptionMessage<OnCreateWidgetSubscription>) {
75
+ * handle(message.value); // <-- type OnCreateWidgetSubscription
76
+ * }
77
+ * })
78
+ * ```
79
+ */
80
+ export type GraphqlSubscriptionMessage<T> = {
81
+ provider: AWSAppSyncRealTimeProvider;
82
+ value: { data?: T };
83
+ };
84
+
28
85
  export enum GraphQLAuthError {
29
86
  NO_API_KEY = 'No api-key configured',
30
87
  NO_CURRENT_USER = 'No current user',
@@ -38,3 +95,156 @@ export enum GraphQLAuthError {
38
95
  * @see: https://graphql.org/graphql-js/language/#parse
39
96
  */
40
97
  export type GraphQLOperation = Source | string;
98
+
99
+ /**
100
+ * API V6 `graphql({options})` type that can leverage branded graphql `query`
101
+ * objects and fallback types.
102
+ */
103
+ export interface GraphQLOptionsV6<
104
+ FALLBACK_TYPES = unknown,
105
+ TYPED_GQL_STRING extends string = string
106
+ > {
107
+ query: TYPED_GQL_STRING | DocumentNode;
108
+ variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
109
+ authMode?: keyof typeof GRAPHQL_AUTH_MODE;
110
+ authToken?: string;
111
+ /**
112
+ * @deprecated This property should not be used
113
+ */
114
+ userAgentSuffix?: string; // TODO: remove in v6
115
+ }
116
+
117
+ /**
118
+ * Result type for `graphql()` operations that don't include any specific
119
+ * type information. The result could be either a `Promise` or `Subscription`.
120
+ *
121
+ * Invoking code should either cast the result or use `?` and `!` operators to
122
+ * navigate the result objects.
123
+ */
124
+ export type UnknownGraphQLResponse =
125
+ | Promise<GraphQLResult<any>>
126
+ | GraphqlSubscriptionResult<any>;
127
+
128
+ /**
129
+ * The expected type for `variables` in a V6 `graphql()` operation with
130
+ * respect to the given `FALLBACK_TYPES` and `TYPED_GQL_STRING`.
131
+ */
132
+ export type GraphQLVariablesV6<
133
+ FALLBACK_TYPES = unknown,
134
+ TYPED_GQL_STRING extends string = string
135
+ > = TYPED_GQL_STRING extends GeneratedQuery<infer IN, any>
136
+ ? IN
137
+ : TYPED_GQL_STRING extends GeneratedMutation<infer IN, any>
138
+ ? IN
139
+ : TYPED_GQL_STRING extends GeneratedSubscription<infer IN, any>
140
+ ? IN
141
+ : FALLBACK_TYPES extends GraphQLOperationType<infer IN, any>
142
+ ? IN
143
+ : any;
144
+
145
+ /**
146
+ * The expected return type with respect to the given `FALLBACK_TYPE`
147
+ * and `TYPED_GQL_STRING`.
148
+ */
149
+ export type GraphQLResponseV6<
150
+ FALLBACK_TYPE = unknown,
151
+ TYPED_GQL_STRING extends string = string
152
+ > = TYPED_GQL_STRING extends GeneratedQuery<infer IN, infer QUERY_OUT>
153
+ ? Promise<GraphQLResult<QUERY_OUT>>
154
+ : TYPED_GQL_STRING extends GeneratedMutation<infer IN, infer MUTATION_OUT>
155
+ ? Promise<GraphQLResult<MUTATION_OUT>>
156
+ : TYPED_GQL_STRING extends GeneratedSubscription<infer IN, infer SUB_OUT>
157
+ ? GraphqlSubscriptionResult<SUB_OUT>
158
+ : FALLBACK_TYPE extends GraphQLQuery<infer T>
159
+ ? Promise<GraphQLResult<FALLBACK_TYPE>>
160
+ : FALLBACK_TYPE extends GraphQLSubscription<infer T>
161
+ ? GraphqlSubscriptionResult<FALLBACK_TYPE>
162
+ : FALLBACK_TYPE extends GraphQLOperationType<infer IN, infer CUSTOM_OUT>
163
+ ? CUSTOM_OUT
164
+ : UnknownGraphQLResponse;
165
+
166
+ /**
167
+ * The shape customers can use to provide `T` to `graphql<T>()` to specify both
168
+ * `IN` and `OUT` types (the type of `variables` and the return type, respectively).
169
+ *
170
+ * I.E.,
171
+ *
172
+ * ```ts
173
+ * type MyVariablesType = { ... };
174
+ * type MyResultType = { ... };
175
+ * type MyOperationType = { variables: MyVariablesType, result: MyResultType };
176
+ *
177
+ * const result: MyResultType = graphql<MyOperationType>("graphql string", {
178
+ * variables: {
179
+ * // MyVariablesType
180
+ * }
181
+ * })
182
+ * ```
183
+ */
184
+ export type GraphQLOperationType<IN extends {}, OUT extends {}> = {
185
+ variables: IN;
186
+ result: OUT;
187
+ };
188
+
189
+ /**
190
+ * Nominal type for branding generated graphql query operation strings with
191
+ * input and output types.
192
+ *
193
+ * E.g.,
194
+ *
195
+ * ```ts
196
+ * export const getWidget = `...` as GeneratedQuery<
197
+ * GetWidgetQueryVariables,
198
+ * GetWidgetQuery
199
+ * >;
200
+ * ```
201
+ *
202
+ * This allows `graphql()` to extract `InputType` and `OutputType` to correctly
203
+ * assign types to the `variables` and result objects.
204
+ */
205
+ export type GeneratedQuery<InputType, OutputType> = string & {
206
+ __generatedQueryInput: InputType;
207
+ __generatedQueryOutput: OutputType;
208
+ };
209
+
210
+ /**
211
+ * Nominal type for branding generated graphql mutation operation strings with
212
+ * input and output types.
213
+ *
214
+ * E.g.,
215
+ *
216
+ * ```ts
217
+ * export const createWidget = `...` as GeneratedQuery<
218
+ * CreateWidgetMutationVariables,
219
+ * CreateWidgetMutation
220
+ * >;
221
+ * ```
222
+ *
223
+ * This allows `graphql()` to extract `InputType` and `OutputType` to correctly
224
+ * assign types to the `variables` and result objects.
225
+ */
226
+ export type GeneratedMutation<InputType, OutputType> = string & {
227
+ __generatedMutationInput: InputType;
228
+ __generatedMutationOutput: OutputType;
229
+ };
230
+
231
+ /**
232
+ * Nominal type for branding generated graphql mutation operation strings with
233
+ * input and output types.
234
+ *
235
+ * E.g.,
236
+ *
237
+ * ```ts
238
+ * export const createWidget = `...` as GeneratedMutation<
239
+ * CreateWidgetMutationVariables,
240
+ * CreateWidgetMutation
241
+ * >;
242
+ * ```
243
+ *
244
+ * This allows `graphql()` to extract `InputType` and `OutputType` to correctly
245
+ * assign types to the `variables` and result objects.
246
+ */
247
+ export type GeneratedSubscription<InputType, OutputType> = string & {
248
+ __generatedSubscriptionInput: InputType;
249
+ __generatedSubscriptionOutput: OutputType;
250
+ };