@aws-amplify/api-graphql 3.4.10 → 3.4.11-api-v6.26fa5d0.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/lib/internals/index.d.ts +1 -0
- package/lib/internals/index.js +2 -0
- package/lib/internals/index.js.map +1 -1
- package/lib/internals/v6.d.ts +89 -0
- package/lib/internals/v6.js +100 -0
- package/lib/internals/v6.js.map +1 -0
- package/lib/types/index.d.ts +169 -0
- package/lib/types/index.js.map +1 -1
- package/lib-esm/internals/index.d.ts +1 -0
- package/lib-esm/internals/index.js +1 -0
- package/lib-esm/internals/index.js.map +1 -1
- package/lib-esm/internals/v6.d.ts +89 -0
- package/lib-esm/internals/v6.js +97 -0
- package/lib-esm/internals/v6.js.map +1 -0
- package/lib-esm/types/index.d.ts +169 -0
- package/lib-esm/types/index.js.map +1 -1
- package/package.json +13 -9
- package/src/internals/index.ts +2 -0
- package/src/internals/v6.ts +106 -0
- package/src/types/index.ts +212 -2
package/lib/internals/index.d.ts
CHANGED
package/lib/internals/index.js
CHANGED
|
@@ -5,4 +5,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
5
5
|
var InternalGraphQLAPI_1 = require("./InternalGraphQLAPI");
|
|
6
6
|
exports.InternalGraphQLAPI = InternalGraphQLAPI_1.InternalGraphQLAPI;
|
|
7
7
|
exports.InternalGraphQLAPIClass = InternalGraphQLAPI_1.InternalGraphQLAPIClass;
|
|
8
|
+
var v6_1 = require("./v6");
|
|
9
|
+
exports.graphql = v6_1.graphql;
|
|
8
10
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/internals/index.ts"],"names":[],"mappings":";;AAAA,qEAAqE;AACrE,sCAAsC;AACtC,2DAG8B;AAF7B,kDAAA,kBAAkB,CAAA;AAClB,uDAAA,uBAAuB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/internals/index.ts"],"names":[],"mappings":";;AAAA,qEAAqE;AACrE,sCAAsC;AACtC,2DAG8B;AAF7B,kDAAA,kBAAkB,CAAA;AAClB,uDAAA,uBAAuB,CAAA;AAGxB,2BAA+B;AAAtB,uBAAA,OAAO,CAAA"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { GraphQLOptionsV6, GraphQLResponseV6 } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Invokes graphql operations against a graphql service, providing correct input and
|
|
4
|
+
* output types if Amplify-generated graphql from a recent version of the CLI/codegen
|
|
5
|
+
* are used *or* correct typing is provided via the type argument.
|
|
6
|
+
*
|
|
7
|
+
* Amplify-generated "branded" graphql queries will look similar to this:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* //
|
|
11
|
+
* // |-- branding
|
|
12
|
+
* // v
|
|
13
|
+
* export const getModel = `...` as GeneratedQuery<
|
|
14
|
+
* GetModelQueryVariables,
|
|
15
|
+
* GetModelQuery
|
|
16
|
+
* >;
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* If this branding is not in your generated graphql, update to a newer version of
|
|
20
|
+
* CLI/codegen and regenerate your graphql using `amplify codegen`.
|
|
21
|
+
*
|
|
22
|
+
* ## Using Amplify-generated graphql
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* import * as queries from './graphql/queries';
|
|
26
|
+
*
|
|
27
|
+
* //
|
|
28
|
+
* // |-- correctly typed graphql response containing a Widget
|
|
29
|
+
* // v
|
|
30
|
+
* const queryResult = await graphql({
|
|
31
|
+
* query: queries.getWidget,
|
|
32
|
+
* variables: {
|
|
33
|
+
* id: "abc", // <-- type hinted/enforced
|
|
34
|
+
* },
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* //
|
|
38
|
+
* // |-- a correctly typed Widget
|
|
39
|
+
* // v
|
|
40
|
+
* const fetchedWidget = queryResult.data?.getWidget!;
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* ## Custom input + result types
|
|
44
|
+
*
|
|
45
|
+
* To provide input types (`variables`) and result types:
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* type GetById_NameOnly = {
|
|
49
|
+
* variables: {
|
|
50
|
+
* id: string
|
|
51
|
+
* },
|
|
52
|
+
* result: Promise<{
|
|
53
|
+
* data: { getWidget: { name: string } }
|
|
54
|
+
* }>
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* //
|
|
58
|
+
* // |-- type is GetById_NameOnly["result"]
|
|
59
|
+
* // v
|
|
60
|
+
* const result = graphql<GetById_NameOnly>({
|
|
61
|
+
* query: "...",
|
|
62
|
+
* variables: { id: "abc" } // <-- type of GetById_NameOnly["variables"]
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* ## Custom result type only
|
|
67
|
+
*
|
|
68
|
+
* To specify result types only, use a type that is *not* in the `{variables, result}` shape:
|
|
69
|
+
*
|
|
70
|
+
* ```ts
|
|
71
|
+
* type MyResultType = Promise<{
|
|
72
|
+
* data: {
|
|
73
|
+
* getWidget: { name: string }
|
|
74
|
+
* }
|
|
75
|
+
* }>
|
|
76
|
+
*
|
|
77
|
+
* //
|
|
78
|
+
* // |-- type is MyResultType
|
|
79
|
+
* // v
|
|
80
|
+
* const result = graphql<MyResultType>({query: "..."});
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @param options
|
|
84
|
+
* @param additionalHeaders
|
|
85
|
+
*/
|
|
86
|
+
export declare function graphql<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: {
|
|
87
|
+
[key: string]: string;
|
|
88
|
+
}): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
89
|
+
export { GraphQLOptionsV6, GraphQLResponseV6 };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
var GraphQLAPI_1 = require("../GraphQLAPI");
|
|
6
|
+
/**
|
|
7
|
+
* Invokes graphql operations against a graphql service, providing correct input and
|
|
8
|
+
* output types if Amplify-generated graphql from a recent version of the CLI/codegen
|
|
9
|
+
* are used *or* correct typing is provided via the type argument.
|
|
10
|
+
*
|
|
11
|
+
* Amplify-generated "branded" graphql queries will look similar to this:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* //
|
|
15
|
+
* // |-- branding
|
|
16
|
+
* // v
|
|
17
|
+
* export const getModel = `...` as GeneratedQuery<
|
|
18
|
+
* GetModelQueryVariables,
|
|
19
|
+
* GetModelQuery
|
|
20
|
+
* >;
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* If this branding is not in your generated graphql, update to a newer version of
|
|
24
|
+
* CLI/codegen and regenerate your graphql using `amplify codegen`.
|
|
25
|
+
*
|
|
26
|
+
* ## Using Amplify-generated graphql
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import * as queries from './graphql/queries';
|
|
30
|
+
*
|
|
31
|
+
* //
|
|
32
|
+
* // |-- correctly typed graphql response containing a Widget
|
|
33
|
+
* // v
|
|
34
|
+
* const queryResult = await graphql({
|
|
35
|
+
* query: queries.getWidget,
|
|
36
|
+
* variables: {
|
|
37
|
+
* id: "abc", // <-- type hinted/enforced
|
|
38
|
+
* },
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* //
|
|
42
|
+
* // |-- a correctly typed Widget
|
|
43
|
+
* // v
|
|
44
|
+
* const fetchedWidget = queryResult.data?.getWidget!;
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* ## Custom input + result types
|
|
48
|
+
*
|
|
49
|
+
* To provide input types (`variables`) and result types:
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* type GetById_NameOnly = {
|
|
53
|
+
* variables: {
|
|
54
|
+
* id: string
|
|
55
|
+
* },
|
|
56
|
+
* result: Promise<{
|
|
57
|
+
* data: { getWidget: { name: string } }
|
|
58
|
+
* }>
|
|
59
|
+
* }
|
|
60
|
+
*
|
|
61
|
+
* //
|
|
62
|
+
* // |-- type is GetById_NameOnly["result"]
|
|
63
|
+
* // v
|
|
64
|
+
* const result = graphql<GetById_NameOnly>({
|
|
65
|
+
* query: "...",
|
|
66
|
+
* variables: { id: "abc" } // <-- type of GetById_NameOnly["variables"]
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* ## Custom result type only
|
|
71
|
+
*
|
|
72
|
+
* To specify result types only, use a type that is *not* in the `{variables, result}` shape:
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* type MyResultType = Promise<{
|
|
76
|
+
* data: {
|
|
77
|
+
* getWidget: { name: string }
|
|
78
|
+
* }
|
|
79
|
+
* }>
|
|
80
|
+
*
|
|
81
|
+
* //
|
|
82
|
+
* // |-- type is MyResultType
|
|
83
|
+
* // v
|
|
84
|
+
* const result = graphql<MyResultType>({query: "..."});
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @param options
|
|
88
|
+
* @param additionalHeaders
|
|
89
|
+
*/
|
|
90
|
+
function graphql(options, additionalHeaders) {
|
|
91
|
+
/**
|
|
92
|
+
* The correctness of these typings depends on correct string branding or overrides.
|
|
93
|
+
* Neither of these can actually be validated at runtime. Hence, we don't perform
|
|
94
|
+
* any validation or type-guarding here.
|
|
95
|
+
*/
|
|
96
|
+
var result = GraphQLAPI_1.GraphQLAPI.graphql(options, additionalHeaders);
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
exports.graphql = graphql;
|
|
100
|
+
//# sourceMappingURL=v6.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"v6.js","sourceRoot":"","sources":["../../src/internals/v6.ts"],"names":[],"mappings":";;AAAA,qEAAqE;AACrE,sCAAsC;AACtC,4CAA2C;AAG3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmFG;AACH,SAAgB,OAAO,CAItB,OAA2D,EAC3D,iBAA6C;IAE7C;;;;OAIG;IACH,IAAM,MAAM,GAAG,uBAAU,CAAC,OAAO,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;IAC9D,OAAO,MAAa,CAAC;AACtB,CAAC;AAdD,0BAcC"}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -2,6 +2,11 @@ import { Source, DocumentNode, GraphQLError } from 'graphql';
|
|
|
2
2
|
export { OperationTypeNode } from 'graphql';
|
|
3
3
|
import { GRAPHQL_AUTH_MODE } from '@aws-amplify/auth';
|
|
4
4
|
export { GRAPHQL_AUTH_MODE };
|
|
5
|
+
import { Observable } from 'zen-observable-ts';
|
|
6
|
+
import { AWSAppSyncRealTimeProvider } from '@aws-amplify/pubsub';
|
|
7
|
+
/**
|
|
8
|
+
* Loose/Unknown options for raw GraphQLAPICategory `graphql()`.
|
|
9
|
+
*/
|
|
5
10
|
export interface GraphQLOptions {
|
|
6
11
|
query: string | DocumentNode;
|
|
7
12
|
variables?: object;
|
|
@@ -19,6 +24,56 @@ export interface GraphQLResult<T = object> {
|
|
|
19
24
|
[key: string]: any;
|
|
20
25
|
};
|
|
21
26
|
}
|
|
27
|
+
declare const queryType: unique symbol;
|
|
28
|
+
export declare type GraphQLQuery<T> = T & {
|
|
29
|
+
readonly [queryType]: 'query';
|
|
30
|
+
};
|
|
31
|
+
export declare type GraphQLSubscription<T> = T & {
|
|
32
|
+
readonly [queryType]: 'subscription';
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* The return value from a `graphql({query})` call when `query` is a subscription.
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* // |-- You are here
|
|
39
|
+
* // v
|
|
40
|
+
* const subResult: GraphqlSubscriptionResult<T> = client.graphql({
|
|
41
|
+
* query: onCreateWidget
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* const sub = subResult.subscribe({
|
|
45
|
+
* //
|
|
46
|
+
* // |-- You are here
|
|
47
|
+
* // v
|
|
48
|
+
* next(message: GraphqlSubscriptionMessage<OnCreateWidgetSubscription>) {
|
|
49
|
+
* handle(message.value); // <-- type OnCreateWidgetSubscription
|
|
50
|
+
* }
|
|
51
|
+
* })
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare type GraphqlSubscriptionResult<T> = Observable<GraphqlSubscriptionMessage<T>>;
|
|
55
|
+
/**
|
|
56
|
+
* The shape of messages passed to `next()` from a graphql subscription. E.g.,
|
|
57
|
+
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* const sub = client.graphql({
|
|
60
|
+
* query: onCreateWidget,
|
|
61
|
+
* }).subscribe({
|
|
62
|
+
* //
|
|
63
|
+
* // |-- You are here
|
|
64
|
+
* // v
|
|
65
|
+
* next(message: GraphqlSubscriptionMessage<OnCreateWidgetSubscription>) {
|
|
66
|
+
* handle(message.value); // <-- type OnCreateWidgetSubscription
|
|
67
|
+
* }
|
|
68
|
+
* })
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare type GraphqlSubscriptionMessage<T> = {
|
|
72
|
+
provider: AWSAppSyncRealTimeProvider;
|
|
73
|
+
value: {
|
|
74
|
+
data?: T;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
22
77
|
export declare enum GraphQLAuthError {
|
|
23
78
|
NO_API_KEY = "No api-key configured",
|
|
24
79
|
NO_CURRENT_USER = "No current user",
|
|
@@ -31,3 +86,117 @@ export declare enum GraphQLAuthError {
|
|
|
31
86
|
* @see: https://graphql.org/graphql-js/language/#parse
|
|
32
87
|
*/
|
|
33
88
|
export declare type GraphQLOperation = Source | string;
|
|
89
|
+
/**
|
|
90
|
+
* API V6 `graphql({options})` type that can leverage branded graphql `query`
|
|
91
|
+
* objects and fallback types.
|
|
92
|
+
*/
|
|
93
|
+
export interface GraphQLOptionsV6<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string> {
|
|
94
|
+
query: TYPED_GQL_STRING | DocumentNode;
|
|
95
|
+
variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
96
|
+
authMode?: keyof typeof GRAPHQL_AUTH_MODE;
|
|
97
|
+
authToken?: string;
|
|
98
|
+
/**
|
|
99
|
+
* @deprecated This property should not be used
|
|
100
|
+
*/
|
|
101
|
+
userAgentSuffix?: string;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Result type for `graphql()` operations that don't include any specific
|
|
105
|
+
* type information. The result could be either a `Promise` or `Subscription`.
|
|
106
|
+
*
|
|
107
|
+
* Invoking code should either cast the result or use `?` and `!` operators to
|
|
108
|
+
* navigate the result objects.
|
|
109
|
+
*/
|
|
110
|
+
export declare type UnknownGraphQLResponse = Promise<GraphQLResult<any>> | GraphqlSubscriptionResult<any>;
|
|
111
|
+
/**
|
|
112
|
+
* The expected type for `variables` in a V6 `graphql()` operation with
|
|
113
|
+
* respect to the given `FALLBACK_TYPES` and `TYPED_GQL_STRING`.
|
|
114
|
+
*/
|
|
115
|
+
export declare type GraphQLVariablesV6<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string> = TYPED_GQL_STRING extends GeneratedQuery<infer IN, any> ? IN : TYPED_GQL_STRING extends GeneratedMutation<infer IN, any> ? IN : TYPED_GQL_STRING extends GeneratedSubscription<infer IN, any> ? IN : FALLBACK_TYPES extends GraphQLOperationType<infer IN, any> ? IN : any;
|
|
116
|
+
/**
|
|
117
|
+
* The expected return type with respect to the given `FALLBACK_TYPE`
|
|
118
|
+
* and `TYPED_GQL_STRING`.
|
|
119
|
+
*/
|
|
120
|
+
export declare type GraphQLResponseV6<FALLBACK_TYPE = unknown, TYPED_GQL_STRING extends string = string> = TYPED_GQL_STRING extends GeneratedQuery<infer IN, infer QUERY_OUT> ? Promise<GraphQLResult<QUERY_OUT>> : TYPED_GQL_STRING extends GeneratedMutation<infer IN, infer MUTATION_OUT> ? Promise<GraphQLResult<MUTATION_OUT>> : TYPED_GQL_STRING extends GeneratedSubscription<infer IN, infer SUB_OUT> ? GraphqlSubscriptionResult<SUB_OUT> : FALLBACK_TYPE extends GraphQLQuery<infer T> ? Promise<GraphQLResult<FALLBACK_TYPE>> : FALLBACK_TYPE extends GraphQLSubscription<infer T> ? GraphqlSubscriptionResult<FALLBACK_TYPE> : FALLBACK_TYPE extends GraphQLOperationType<infer IN, infer CUSTOM_OUT> ? CUSTOM_OUT : UnknownGraphQLResponse;
|
|
121
|
+
/**
|
|
122
|
+
* The shape customers can use to provide `T` to `graphql<T>()` to specify both
|
|
123
|
+
* `IN` and `OUT` types (the type of `variables` and the return type, respectively).
|
|
124
|
+
*
|
|
125
|
+
* I.E.,
|
|
126
|
+
*
|
|
127
|
+
* ```ts
|
|
128
|
+
* type MyVariablesType = { ... };
|
|
129
|
+
* type MyResultType = { ... };
|
|
130
|
+
* type MyOperationType = { variables: MyVariablesType, result: MyResultType };
|
|
131
|
+
*
|
|
132
|
+
* const result: MyResultType = graphql<MyOperationType>("graphql string", {
|
|
133
|
+
* variables: {
|
|
134
|
+
* // MyVariablesType
|
|
135
|
+
* }
|
|
136
|
+
* })
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export declare type GraphQLOperationType<IN extends {}, OUT extends {}> = {
|
|
140
|
+
variables: IN;
|
|
141
|
+
result: OUT;
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Nominal type for branding generated graphql query operation strings with
|
|
145
|
+
* input and output types.
|
|
146
|
+
*
|
|
147
|
+
* E.g.,
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* export const getWidget = `...` as GeneratedQuery<
|
|
151
|
+
* GetWidgetQueryVariables,
|
|
152
|
+
* GetWidgetQuery
|
|
153
|
+
* >;
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* This allows `graphql()` to extract `InputType` and `OutputType` to correctly
|
|
157
|
+
* assign types to the `variables` and result objects.
|
|
158
|
+
*/
|
|
159
|
+
export declare type GeneratedQuery<InputType, OutputType> = string & {
|
|
160
|
+
__generatedQueryInput: InputType;
|
|
161
|
+
__generatedQueryOutput: OutputType;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Nominal type for branding generated graphql mutation operation strings with
|
|
165
|
+
* input and output types.
|
|
166
|
+
*
|
|
167
|
+
* E.g.,
|
|
168
|
+
*
|
|
169
|
+
* ```ts
|
|
170
|
+
* export const createWidget = `...` as GeneratedQuery<
|
|
171
|
+
* CreateWidgetMutationVariables,
|
|
172
|
+
* CreateWidgetMutation
|
|
173
|
+
* >;
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* This allows `graphql()` to extract `InputType` and `OutputType` to correctly
|
|
177
|
+
* assign types to the `variables` and result objects.
|
|
178
|
+
*/
|
|
179
|
+
export declare type GeneratedMutation<InputType, OutputType> = string & {
|
|
180
|
+
__generatedMutationInput: InputType;
|
|
181
|
+
__generatedMutationOutput: OutputType;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Nominal type for branding generated graphql mutation operation strings with
|
|
185
|
+
* input and output types.
|
|
186
|
+
*
|
|
187
|
+
* E.g.,
|
|
188
|
+
*
|
|
189
|
+
* ```ts
|
|
190
|
+
* export const createWidget = `...` as GeneratedMutation<
|
|
191
|
+
* CreateWidgetMutationVariables,
|
|
192
|
+
* CreateWidgetMutation
|
|
193
|
+
* >;
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* This allows `graphql()` to extract `InputType` and `OutputType` to correctly
|
|
197
|
+
* assign types to the `variables` and result objects.
|
|
198
|
+
*/
|
|
199
|
+
export declare type GeneratedSubscription<InputType, OutputType> = string & {
|
|
200
|
+
__generatedSubscriptionInput: InputType;
|
|
201
|
+
__generatedSubscriptionOutput: OutputType;
|
|
202
|
+
};
|
package/lib/types/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;AAIA,0CAAsD;AAC7C,4BADA,wBAAiB,CACA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;AAIA,0CAAsD;AAC7C,4BADA,wBAAiB,CACA;AA+E1B,IAAY,gBAMX;AAND,WAAY,gBAAgB;IAC3B,wDAAoC,CAAA;IACpC,uDAAmC,CAAA;IACnC,qDAAiC,CAAA;IACjC,yDAAqC,CAAA;IACrC,6DAAyC,CAAA;AAC1C,CAAC,EANW,gBAAgB,GAAhB,wBAAgB,KAAhB,wBAAgB,QAM3B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/internals/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AACtC,OAAO,EACN,kBAAkB,EAClB,uBAAuB,GACvB,MAAM,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/internals/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AACtC,OAAO,EACN,kBAAkB,EAClB,uBAAuB,GACvB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { GraphQLOptionsV6, GraphQLResponseV6 } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Invokes graphql operations against a graphql service, providing correct input and
|
|
4
|
+
* output types if Amplify-generated graphql from a recent version of the CLI/codegen
|
|
5
|
+
* are used *or* correct typing is provided via the type argument.
|
|
6
|
+
*
|
|
7
|
+
* Amplify-generated "branded" graphql queries will look similar to this:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* //
|
|
11
|
+
* // |-- branding
|
|
12
|
+
* // v
|
|
13
|
+
* export const getModel = `...` as GeneratedQuery<
|
|
14
|
+
* GetModelQueryVariables,
|
|
15
|
+
* GetModelQuery
|
|
16
|
+
* >;
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* If this branding is not in your generated graphql, update to a newer version of
|
|
20
|
+
* CLI/codegen and regenerate your graphql using `amplify codegen`.
|
|
21
|
+
*
|
|
22
|
+
* ## Using Amplify-generated graphql
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* import * as queries from './graphql/queries';
|
|
26
|
+
*
|
|
27
|
+
* //
|
|
28
|
+
* // |-- correctly typed graphql response containing a Widget
|
|
29
|
+
* // v
|
|
30
|
+
* const queryResult = await graphql({
|
|
31
|
+
* query: queries.getWidget,
|
|
32
|
+
* variables: {
|
|
33
|
+
* id: "abc", // <-- type hinted/enforced
|
|
34
|
+
* },
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* //
|
|
38
|
+
* // |-- a correctly typed Widget
|
|
39
|
+
* // v
|
|
40
|
+
* const fetchedWidget = queryResult.data?.getWidget!;
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* ## Custom input + result types
|
|
44
|
+
*
|
|
45
|
+
* To provide input types (`variables`) and result types:
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* type GetById_NameOnly = {
|
|
49
|
+
* variables: {
|
|
50
|
+
* id: string
|
|
51
|
+
* },
|
|
52
|
+
* result: Promise<{
|
|
53
|
+
* data: { getWidget: { name: string } }
|
|
54
|
+
* }>
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* //
|
|
58
|
+
* // |-- type is GetById_NameOnly["result"]
|
|
59
|
+
* // v
|
|
60
|
+
* const result = graphql<GetById_NameOnly>({
|
|
61
|
+
* query: "...",
|
|
62
|
+
* variables: { id: "abc" } // <-- type of GetById_NameOnly["variables"]
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* ## Custom result type only
|
|
67
|
+
*
|
|
68
|
+
* To specify result types only, use a type that is *not* in the `{variables, result}` shape:
|
|
69
|
+
*
|
|
70
|
+
* ```ts
|
|
71
|
+
* type MyResultType = Promise<{
|
|
72
|
+
* data: {
|
|
73
|
+
* getWidget: { name: string }
|
|
74
|
+
* }
|
|
75
|
+
* }>
|
|
76
|
+
*
|
|
77
|
+
* //
|
|
78
|
+
* // |-- type is MyResultType
|
|
79
|
+
* // v
|
|
80
|
+
* const result = graphql<MyResultType>({query: "..."});
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @param options
|
|
84
|
+
* @param additionalHeaders
|
|
85
|
+
*/
|
|
86
|
+
export declare function graphql<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: {
|
|
87
|
+
[key: string]: string;
|
|
88
|
+
}): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
89
|
+
export { GraphQLOptionsV6, GraphQLResponseV6 };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { GraphQLAPI } from '../GraphQLAPI';
|
|
4
|
+
/**
|
|
5
|
+
* Invokes graphql operations against a graphql service, providing correct input and
|
|
6
|
+
* output types if Amplify-generated graphql from a recent version of the CLI/codegen
|
|
7
|
+
* are used *or* correct typing is provided via the type argument.
|
|
8
|
+
*
|
|
9
|
+
* Amplify-generated "branded" graphql queries will look similar to this:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* //
|
|
13
|
+
* // |-- branding
|
|
14
|
+
* // v
|
|
15
|
+
* export const getModel = `...` as GeneratedQuery<
|
|
16
|
+
* GetModelQueryVariables,
|
|
17
|
+
* GetModelQuery
|
|
18
|
+
* >;
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* If this branding is not in your generated graphql, update to a newer version of
|
|
22
|
+
* CLI/codegen and regenerate your graphql using `amplify codegen`.
|
|
23
|
+
*
|
|
24
|
+
* ## Using Amplify-generated graphql
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* import * as queries from './graphql/queries';
|
|
28
|
+
*
|
|
29
|
+
* //
|
|
30
|
+
* // |-- correctly typed graphql response containing a Widget
|
|
31
|
+
* // v
|
|
32
|
+
* const queryResult = await graphql({
|
|
33
|
+
* query: queries.getWidget,
|
|
34
|
+
* variables: {
|
|
35
|
+
* id: "abc", // <-- type hinted/enforced
|
|
36
|
+
* },
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* //
|
|
40
|
+
* // |-- a correctly typed Widget
|
|
41
|
+
* // v
|
|
42
|
+
* const fetchedWidget = queryResult.data?.getWidget!;
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* ## Custom input + result types
|
|
46
|
+
*
|
|
47
|
+
* To provide input types (`variables`) and result types:
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* type GetById_NameOnly = {
|
|
51
|
+
* variables: {
|
|
52
|
+
* id: string
|
|
53
|
+
* },
|
|
54
|
+
* result: Promise<{
|
|
55
|
+
* data: { getWidget: { name: string } }
|
|
56
|
+
* }>
|
|
57
|
+
* }
|
|
58
|
+
*
|
|
59
|
+
* //
|
|
60
|
+
* // |-- type is GetById_NameOnly["result"]
|
|
61
|
+
* // v
|
|
62
|
+
* const result = graphql<GetById_NameOnly>({
|
|
63
|
+
* query: "...",
|
|
64
|
+
* variables: { id: "abc" } // <-- type of GetById_NameOnly["variables"]
|
|
65
|
+
* });
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* ## Custom result type only
|
|
69
|
+
*
|
|
70
|
+
* To specify result types only, use a type that is *not* in the `{variables, result}` shape:
|
|
71
|
+
*
|
|
72
|
+
* ```ts
|
|
73
|
+
* type MyResultType = Promise<{
|
|
74
|
+
* data: {
|
|
75
|
+
* getWidget: { name: string }
|
|
76
|
+
* }
|
|
77
|
+
* }>
|
|
78
|
+
*
|
|
79
|
+
* //
|
|
80
|
+
* // |-- type is MyResultType
|
|
81
|
+
* // v
|
|
82
|
+
* const result = graphql<MyResultType>({query: "..."});
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @param options
|
|
86
|
+
* @param additionalHeaders
|
|
87
|
+
*/
|
|
88
|
+
export function graphql(options, additionalHeaders) {
|
|
89
|
+
/**
|
|
90
|
+
* The correctness of these typings depends on correct string branding or overrides.
|
|
91
|
+
* Neither of these can actually be validated at runtime. Hence, we don't perform
|
|
92
|
+
* any validation or type-guarding here.
|
|
93
|
+
*/
|
|
94
|
+
var result = GraphQLAPI.graphql(options, additionalHeaders);
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=v6.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"v6.js","sourceRoot":"","sources":["../../src/internals/v6.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAG3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmFG;AACH,MAAM,UAAU,OAAO,CAItB,OAA2D,EAC3D,iBAA6C;IAE7C;;;;OAIG;IACH,IAAM,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;IAC9D,OAAO,MAAa,CAAC;AACtB,CAAC"}
|
package/lib-esm/types/index.d.ts
CHANGED
|
@@ -2,6 +2,11 @@ import { Source, DocumentNode, GraphQLError } from 'graphql';
|
|
|
2
2
|
export { OperationTypeNode } from 'graphql';
|
|
3
3
|
import { GRAPHQL_AUTH_MODE } from '@aws-amplify/auth';
|
|
4
4
|
export { GRAPHQL_AUTH_MODE };
|
|
5
|
+
import { Observable } from 'zen-observable-ts';
|
|
6
|
+
import { AWSAppSyncRealTimeProvider } from '@aws-amplify/pubsub';
|
|
7
|
+
/**
|
|
8
|
+
* Loose/Unknown options for raw GraphQLAPICategory `graphql()`.
|
|
9
|
+
*/
|
|
5
10
|
export interface GraphQLOptions {
|
|
6
11
|
query: string | DocumentNode;
|
|
7
12
|
variables?: object;
|
|
@@ -19,6 +24,56 @@ export interface GraphQLResult<T = object> {
|
|
|
19
24
|
[key: string]: any;
|
|
20
25
|
};
|
|
21
26
|
}
|
|
27
|
+
declare const queryType: unique symbol;
|
|
28
|
+
export declare type GraphQLQuery<T> = T & {
|
|
29
|
+
readonly [queryType]: 'query';
|
|
30
|
+
};
|
|
31
|
+
export declare type GraphQLSubscription<T> = T & {
|
|
32
|
+
readonly [queryType]: 'subscription';
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* The return value from a `graphql({query})` call when `query` is a subscription.
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* // |-- You are here
|
|
39
|
+
* // v
|
|
40
|
+
* const subResult: GraphqlSubscriptionResult<T> = client.graphql({
|
|
41
|
+
* query: onCreateWidget
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* const sub = subResult.subscribe({
|
|
45
|
+
* //
|
|
46
|
+
* // |-- You are here
|
|
47
|
+
* // v
|
|
48
|
+
* next(message: GraphqlSubscriptionMessage<OnCreateWidgetSubscription>) {
|
|
49
|
+
* handle(message.value); // <-- type OnCreateWidgetSubscription
|
|
50
|
+
* }
|
|
51
|
+
* })
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare type GraphqlSubscriptionResult<T> = Observable<GraphqlSubscriptionMessage<T>>;
|
|
55
|
+
/**
|
|
56
|
+
* The shape of messages passed to `next()` from a graphql subscription. E.g.,
|
|
57
|
+
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* const sub = client.graphql({
|
|
60
|
+
* query: onCreateWidget,
|
|
61
|
+
* }).subscribe({
|
|
62
|
+
* //
|
|
63
|
+
* // |-- You are here
|
|
64
|
+
* // v
|
|
65
|
+
* next(message: GraphqlSubscriptionMessage<OnCreateWidgetSubscription>) {
|
|
66
|
+
* handle(message.value); // <-- type OnCreateWidgetSubscription
|
|
67
|
+
* }
|
|
68
|
+
* })
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare type GraphqlSubscriptionMessage<T> = {
|
|
72
|
+
provider: AWSAppSyncRealTimeProvider;
|
|
73
|
+
value: {
|
|
74
|
+
data?: T;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
22
77
|
export declare enum GraphQLAuthError {
|
|
23
78
|
NO_API_KEY = "No api-key configured",
|
|
24
79
|
NO_CURRENT_USER = "No current user",
|
|
@@ -31,3 +86,117 @@ export declare enum GraphQLAuthError {
|
|
|
31
86
|
* @see: https://graphql.org/graphql-js/language/#parse
|
|
32
87
|
*/
|
|
33
88
|
export declare type GraphQLOperation = Source | string;
|
|
89
|
+
/**
|
|
90
|
+
* API V6 `graphql({options})` type that can leverage branded graphql `query`
|
|
91
|
+
* objects and fallback types.
|
|
92
|
+
*/
|
|
93
|
+
export interface GraphQLOptionsV6<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string> {
|
|
94
|
+
query: TYPED_GQL_STRING | DocumentNode;
|
|
95
|
+
variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
96
|
+
authMode?: keyof typeof GRAPHQL_AUTH_MODE;
|
|
97
|
+
authToken?: string;
|
|
98
|
+
/**
|
|
99
|
+
* @deprecated This property should not be used
|
|
100
|
+
*/
|
|
101
|
+
userAgentSuffix?: string;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Result type for `graphql()` operations that don't include any specific
|
|
105
|
+
* type information. The result could be either a `Promise` or `Subscription`.
|
|
106
|
+
*
|
|
107
|
+
* Invoking code should either cast the result or use `?` and `!` operators to
|
|
108
|
+
* navigate the result objects.
|
|
109
|
+
*/
|
|
110
|
+
export declare type UnknownGraphQLResponse = Promise<GraphQLResult<any>> | GraphqlSubscriptionResult<any>;
|
|
111
|
+
/**
|
|
112
|
+
* The expected type for `variables` in a V6 `graphql()` operation with
|
|
113
|
+
* respect to the given `FALLBACK_TYPES` and `TYPED_GQL_STRING`.
|
|
114
|
+
*/
|
|
115
|
+
export declare type GraphQLVariablesV6<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string> = TYPED_GQL_STRING extends GeneratedQuery<infer IN, any> ? IN : TYPED_GQL_STRING extends GeneratedMutation<infer IN, any> ? IN : TYPED_GQL_STRING extends GeneratedSubscription<infer IN, any> ? IN : FALLBACK_TYPES extends GraphQLOperationType<infer IN, any> ? IN : any;
|
|
116
|
+
/**
|
|
117
|
+
* The expected return type with respect to the given `FALLBACK_TYPE`
|
|
118
|
+
* and `TYPED_GQL_STRING`.
|
|
119
|
+
*/
|
|
120
|
+
export declare type GraphQLResponseV6<FALLBACK_TYPE = unknown, TYPED_GQL_STRING extends string = string> = TYPED_GQL_STRING extends GeneratedQuery<infer IN, infer QUERY_OUT> ? Promise<GraphQLResult<QUERY_OUT>> : TYPED_GQL_STRING extends GeneratedMutation<infer IN, infer MUTATION_OUT> ? Promise<GraphQLResult<MUTATION_OUT>> : TYPED_GQL_STRING extends GeneratedSubscription<infer IN, infer SUB_OUT> ? GraphqlSubscriptionResult<SUB_OUT> : FALLBACK_TYPE extends GraphQLQuery<infer T> ? Promise<GraphQLResult<FALLBACK_TYPE>> : FALLBACK_TYPE extends GraphQLSubscription<infer T> ? GraphqlSubscriptionResult<FALLBACK_TYPE> : FALLBACK_TYPE extends GraphQLOperationType<infer IN, infer CUSTOM_OUT> ? CUSTOM_OUT : UnknownGraphQLResponse;
|
|
121
|
+
/**
|
|
122
|
+
* The shape customers can use to provide `T` to `graphql<T>()` to specify both
|
|
123
|
+
* `IN` and `OUT` types (the type of `variables` and the return type, respectively).
|
|
124
|
+
*
|
|
125
|
+
* I.E.,
|
|
126
|
+
*
|
|
127
|
+
* ```ts
|
|
128
|
+
* type MyVariablesType = { ... };
|
|
129
|
+
* type MyResultType = { ... };
|
|
130
|
+
* type MyOperationType = { variables: MyVariablesType, result: MyResultType };
|
|
131
|
+
*
|
|
132
|
+
* const result: MyResultType = graphql<MyOperationType>("graphql string", {
|
|
133
|
+
* variables: {
|
|
134
|
+
* // MyVariablesType
|
|
135
|
+
* }
|
|
136
|
+
* })
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export declare type GraphQLOperationType<IN extends {}, OUT extends {}> = {
|
|
140
|
+
variables: IN;
|
|
141
|
+
result: OUT;
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Nominal type for branding generated graphql query operation strings with
|
|
145
|
+
* input and output types.
|
|
146
|
+
*
|
|
147
|
+
* E.g.,
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* export const getWidget = `...` as GeneratedQuery<
|
|
151
|
+
* GetWidgetQueryVariables,
|
|
152
|
+
* GetWidgetQuery
|
|
153
|
+
* >;
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* This allows `graphql()` to extract `InputType` and `OutputType` to correctly
|
|
157
|
+
* assign types to the `variables` and result objects.
|
|
158
|
+
*/
|
|
159
|
+
export declare type GeneratedQuery<InputType, OutputType> = string & {
|
|
160
|
+
__generatedQueryInput: InputType;
|
|
161
|
+
__generatedQueryOutput: OutputType;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Nominal type for branding generated graphql mutation operation strings with
|
|
165
|
+
* input and output types.
|
|
166
|
+
*
|
|
167
|
+
* E.g.,
|
|
168
|
+
*
|
|
169
|
+
* ```ts
|
|
170
|
+
* export const createWidget = `...` as GeneratedQuery<
|
|
171
|
+
* CreateWidgetMutationVariables,
|
|
172
|
+
* CreateWidgetMutation
|
|
173
|
+
* >;
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* This allows `graphql()` to extract `InputType` and `OutputType` to correctly
|
|
177
|
+
* assign types to the `variables` and result objects.
|
|
178
|
+
*/
|
|
179
|
+
export declare type GeneratedMutation<InputType, OutputType> = string & {
|
|
180
|
+
__generatedMutationInput: InputType;
|
|
181
|
+
__generatedMutationOutput: OutputType;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Nominal type for branding generated graphql mutation operation strings with
|
|
185
|
+
* input and output types.
|
|
186
|
+
*
|
|
187
|
+
* E.g.,
|
|
188
|
+
*
|
|
189
|
+
* ```ts
|
|
190
|
+
* export const createWidget = `...` as GeneratedMutation<
|
|
191
|
+
* CreateWidgetMutationVariables,
|
|
192
|
+
* CreateWidgetMutation
|
|
193
|
+
* >;
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* This allows `graphql()` to extract `InputType` and `OutputType` to correctly
|
|
197
|
+
* assign types to the `variables` and result objects.
|
|
198
|
+
*/
|
|
199
|
+
export declare type GeneratedSubscription<InputType, OutputType> = string & {
|
|
200
|
+
__generatedSubscriptionInput: InputType;
|
|
201
|
+
__generatedSubscriptionOutput: OutputType;
|
|
202
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,CAAC;AA+E7B,MAAM,CAAN,IAAY,gBAMX;AAND,WAAY,gBAAgB;IAC3B,wDAAoC,CAAA;IACpC,uDAAmC,CAAA;IACnC,qDAAiC,CAAA;IACjC,yDAAqC,CAAA;IACrC,6DAAyC,CAAA;AAC1C,CAAC,EANW,gBAAgB,KAAhB,gBAAgB,QAM3B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/api-graphql",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.11-api-v6.26fa5d0.0+26fa5d0",
|
|
4
4
|
"description": "Api-graphql category of aws-amplify",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib-esm/index.js",
|
|
@@ -50,11 +50,11 @@
|
|
|
50
50
|
"internals"
|
|
51
51
|
],
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@aws-amplify/api-rest": "3.5.
|
|
54
|
-
"@aws-amplify/auth": "5.6.
|
|
55
|
-
"@aws-amplify/cache": "5.1.
|
|
56
|
-
"@aws-amplify/core": "5.8.
|
|
57
|
-
"@aws-amplify/pubsub": "5.5.
|
|
53
|
+
"@aws-amplify/api-rest": "3.5.5-api-v6.26fa5d0.0+26fa5d0",
|
|
54
|
+
"@aws-amplify/auth": "5.6.5-api-v6.26fa5d0.0+26fa5d0",
|
|
55
|
+
"@aws-amplify/cache": "5.1.11-api-v6.26fa5d0.0+26fa5d0",
|
|
56
|
+
"@aws-amplify/core": "5.8.5-api-v6.26fa5d0.0+26fa5d0",
|
|
57
|
+
"@aws-amplify/pubsub": "5.5.5-api-v6.26fa5d0.0+26fa5d0",
|
|
58
58
|
"graphql": "15.8.0",
|
|
59
59
|
"tslib": "^1.8.0",
|
|
60
60
|
"uuid": "^3.2.1",
|
|
@@ -65,13 +65,13 @@
|
|
|
65
65
|
"name": "API (GraphQL client)",
|
|
66
66
|
"path": "./lib-esm/index.js",
|
|
67
67
|
"import": "{ Amplify, GraphQLAPI }",
|
|
68
|
-
"limit": "
|
|
68
|
+
"limit": "91.7 kB"
|
|
69
69
|
}
|
|
70
70
|
],
|
|
71
71
|
"jest": {
|
|
72
72
|
"globals": {
|
|
73
73
|
"ts-jest": {
|
|
74
|
-
"diagnostics":
|
|
74
|
+
"diagnostics": true,
|
|
75
75
|
"tsConfig": {
|
|
76
76
|
"lib": [
|
|
77
77
|
"es5",
|
|
@@ -88,6 +88,10 @@
|
|
|
88
88
|
"^.+\\.(js|jsx|ts|tsx)$": "ts-jest"
|
|
89
89
|
},
|
|
90
90
|
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
|
|
91
|
+
"testPathIgnorePatterns": [
|
|
92
|
+
"__tests__/fixtures",
|
|
93
|
+
"__tests__/utils"
|
|
94
|
+
],
|
|
91
95
|
"moduleFileExtensions": [
|
|
92
96
|
"ts",
|
|
93
97
|
"tsx",
|
|
@@ -112,5 +116,5 @@
|
|
|
112
116
|
"lib-esm"
|
|
113
117
|
]
|
|
114
118
|
},
|
|
115
|
-
"gitHead": "
|
|
119
|
+
"gitHead": "26fa5d0a69db52eda29ec36eb31762b86cd30392"
|
|
116
120
|
}
|
package/src/internals/index.ts
CHANGED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { GraphQLAPI } from '../GraphQLAPI';
|
|
4
|
+
import { GraphQLOptionsV6, GraphQLResponseV6 } from '../types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Invokes graphql operations against a graphql service, providing correct input and
|
|
8
|
+
* output types if Amplify-generated graphql from a recent version of the CLI/codegen
|
|
9
|
+
* are used *or* correct typing is provided via the type argument.
|
|
10
|
+
*
|
|
11
|
+
* Amplify-generated "branded" graphql queries will look similar to this:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* //
|
|
15
|
+
* // |-- branding
|
|
16
|
+
* // v
|
|
17
|
+
* export const getModel = `...` as GeneratedQuery<
|
|
18
|
+
* GetModelQueryVariables,
|
|
19
|
+
* GetModelQuery
|
|
20
|
+
* >;
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* If this branding is not in your generated graphql, update to a newer version of
|
|
24
|
+
* CLI/codegen and regenerate your graphql using `amplify codegen`.
|
|
25
|
+
*
|
|
26
|
+
* ## Using Amplify-generated graphql
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import * as queries from './graphql/queries';
|
|
30
|
+
*
|
|
31
|
+
* //
|
|
32
|
+
* // |-- correctly typed graphql response containing a Widget
|
|
33
|
+
* // v
|
|
34
|
+
* const queryResult = await graphql({
|
|
35
|
+
* query: queries.getWidget,
|
|
36
|
+
* variables: {
|
|
37
|
+
* id: "abc", // <-- type hinted/enforced
|
|
38
|
+
* },
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* //
|
|
42
|
+
* // |-- a correctly typed Widget
|
|
43
|
+
* // v
|
|
44
|
+
* const fetchedWidget = queryResult.data?.getWidget!;
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* ## Custom input + result types
|
|
48
|
+
*
|
|
49
|
+
* To provide input types (`variables`) and result types:
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* type GetById_NameOnly = {
|
|
53
|
+
* variables: {
|
|
54
|
+
* id: string
|
|
55
|
+
* },
|
|
56
|
+
* result: Promise<{
|
|
57
|
+
* data: { getWidget: { name: string } }
|
|
58
|
+
* }>
|
|
59
|
+
* }
|
|
60
|
+
*
|
|
61
|
+
* //
|
|
62
|
+
* // |-- type is GetById_NameOnly["result"]
|
|
63
|
+
* // v
|
|
64
|
+
* const result = graphql<GetById_NameOnly>({
|
|
65
|
+
* query: "...",
|
|
66
|
+
* variables: { id: "abc" } // <-- type of GetById_NameOnly["variables"]
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* ## Custom result type only
|
|
71
|
+
*
|
|
72
|
+
* To specify result types only, use a type that is *not* in the `{variables, result}` shape:
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* type MyResultType = Promise<{
|
|
76
|
+
* data: {
|
|
77
|
+
* getWidget: { name: string }
|
|
78
|
+
* }
|
|
79
|
+
* }>
|
|
80
|
+
*
|
|
81
|
+
* //
|
|
82
|
+
* // |-- type is MyResultType
|
|
83
|
+
* // v
|
|
84
|
+
* const result = graphql<MyResultType>({query: "..."});
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @param options
|
|
88
|
+
* @param additionalHeaders
|
|
89
|
+
*/
|
|
90
|
+
export function graphql<
|
|
91
|
+
FALLBACK_TYPES = unknown,
|
|
92
|
+
TYPED_GQL_STRING extends string = string
|
|
93
|
+
>(
|
|
94
|
+
options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
|
|
95
|
+
additionalHeaders?: { [key: string]: string }
|
|
96
|
+
): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING> {
|
|
97
|
+
/**
|
|
98
|
+
* The correctness of these typings depends on correct string branding or overrides.
|
|
99
|
+
* Neither of these can actually be validated at runtime. Hence, we don't perform
|
|
100
|
+
* any validation or type-guarding here.
|
|
101
|
+
*/
|
|
102
|
+
const result = GraphQLAPI.graphql(options, additionalHeaders);
|
|
103
|
+
return result as any;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export { GraphQLOptionsV6, GraphQLResponseV6 };
|
package/src/types/index.ts
CHANGED
|
@@ -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
|
|
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 {
|
|
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
|
+
};
|