@aws-amplify/api-graphql 4.0.18-unstable.7d95fa1.0 → 4.0.18-unstable.7e4eff3.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/dist/cjs/internals/generateClient.js +7 -0
- package/dist/cjs/internals/generateClient.js.map +1 -1
- package/dist/cjs/internals/generateCustomOperationsProperty.js +45 -0
- package/dist/cjs/internals/generateCustomOperationsProperty.js.map +1 -0
- package/dist/cjs/internals/operations/custom.js +289 -0
- package/dist/cjs/internals/operations/custom.js.map +1 -0
- package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js +11 -1
- package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js.map +1 -1
- package/dist/esm/internals/generateClient.mjs +7 -0
- package/dist/esm/internals/generateClient.mjs.map +1 -1
- package/dist/esm/internals/generateCustomOperationsProperty.d.ts +8 -0
- package/dist/esm/internals/generateCustomOperationsProperty.mjs +41 -0
- package/dist/esm/internals/generateCustomOperationsProperty.mjs.map +1 -0
- package/dist/esm/internals/operations/custom.d.ts +60 -0
- package/dist/esm/internals/operations/custom.mjs +287 -0
- package/dist/esm/internals/operations/custom.mjs.map +1 -0
- package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs +12 -2
- package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs.map +1 -1
- package/dist/esm/types/index.d.ts +9 -1
- package/package.json +6 -6
- package/src/internals/generateClient.ts +25 -1
- package/src/internals/generateCustomOperationsProperty.ts +89 -0
- package/src/internals/operations/custom.ts +382 -0
- package/src/internals/server/generateClientWithAmplifyInstance.ts +20 -1
- package/src/types/index.ts +11 -1
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
|
|
2
|
+
import { AuthModeParams, ClientWithModels, GraphQLOptionsV6, ListArgs, QueryArgs } from '../../types';
|
|
3
|
+
import { ModelIntrospectionSchema, CustomOperation } from '@aws-amplify/core/internals/utils';
|
|
4
|
+
/**
|
|
5
|
+
* Builds an operation function, embedded with all client and context data, that
|
|
6
|
+
* can be attached to a client as a custom query or mutation.
|
|
7
|
+
*
|
|
8
|
+
* If we have this source schema:
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* a.schema({
|
|
12
|
+
* echo: a.query()
|
|
13
|
+
* .arguments({input: a.string().required()})
|
|
14
|
+
* .returns(a.string())
|
|
15
|
+
* })
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Our model intro schema will contain an entry like this:
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* {
|
|
22
|
+
* queries: {
|
|
23
|
+
* echo: {
|
|
24
|
+
* name: "echo",
|
|
25
|
+
* isArray: false,
|
|
26
|
+
* type: 'String',
|
|
27
|
+
* isRequired: false,
|
|
28
|
+
* arguments: {
|
|
29
|
+
* input: {
|
|
30
|
+
* name: 'input',
|
|
31
|
+
* isArray: false,
|
|
32
|
+
* type: String,
|
|
33
|
+
* isRequired: true
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* The `echo` object is used to build the `echo' method that goes here:
|
|
42
|
+
*
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const client = generateClent()
|
|
45
|
+
* const { data } = await client.queries.echo({input: 'a string'});
|
|
46
|
+
* // ^
|
|
47
|
+
* // |
|
|
48
|
+
* // +-- This one right here.
|
|
49
|
+
* //
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
*
|
|
53
|
+
* @param client The client to run graphql queries through.
|
|
54
|
+
* @param modelIntrospection The model introspection schema the op comes from.
|
|
55
|
+
* @param operationType The broad category of graphql operation.
|
|
56
|
+
* @param operation The operation definition from the introspection schema.
|
|
57
|
+
* @param useContext Whether the function needs to accept an SSR context.
|
|
58
|
+
* @returns The operation function to attach to query, mutations, etc.
|
|
59
|
+
*/
|
|
60
|
+
export declare function customOpFactory(client: ClientWithModels, modelIntrospection: ModelIntrospectionSchema, operationType: 'query' | 'mutation', operation: CustomOperation, useContext: boolean): ((contextSpec: AmplifyServer.ContextSpec & GraphQLOptionsV6<unknown, string>, arg?: QueryArgs, options?: AuthModeParams & ListArgs) => Promise<any>) | ((arg?: QueryArgs, options?: AuthModeParams & ListArgs) => Promise<any>);
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { authModeParams, getCustomHeaders, flattenItems, initializeModel } from '../APIClient.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Builds an operation function, embedded with all client and context data, that
|
|
5
|
+
* can be attached to a client as a custom query or mutation.
|
|
6
|
+
*
|
|
7
|
+
* If we have this source schema:
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* a.schema({
|
|
11
|
+
* echo: a.query()
|
|
12
|
+
* .arguments({input: a.string().required()})
|
|
13
|
+
* .returns(a.string())
|
|
14
|
+
* })
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Our model intro schema will contain an entry like this:
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* {
|
|
21
|
+
* queries: {
|
|
22
|
+
* echo: {
|
|
23
|
+
* name: "echo",
|
|
24
|
+
* isArray: false,
|
|
25
|
+
* type: 'String',
|
|
26
|
+
* isRequired: false,
|
|
27
|
+
* arguments: {
|
|
28
|
+
* input: {
|
|
29
|
+
* name: 'input',
|
|
30
|
+
* isArray: false,
|
|
31
|
+
* type: String,
|
|
32
|
+
* isRequired: true
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* The `echo` object is used to build the `echo' method that goes here:
|
|
41
|
+
*
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const client = generateClent()
|
|
44
|
+
* const { data } = await client.queries.echo({input: 'a string'});
|
|
45
|
+
* // ^
|
|
46
|
+
* // |
|
|
47
|
+
* // +-- This one right here.
|
|
48
|
+
* //
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
*
|
|
52
|
+
* @param client The client to run graphql queries through.
|
|
53
|
+
* @param modelIntrospection The model introspection schema the op comes from.
|
|
54
|
+
* @param operationType The broad category of graphql operation.
|
|
55
|
+
* @param operation The operation definition from the introspection schema.
|
|
56
|
+
* @param useContext Whether the function needs to accept an SSR context.
|
|
57
|
+
* @returns The operation function to attach to query, mutations, etc.
|
|
58
|
+
*/
|
|
59
|
+
function customOpFactory(client, modelIntrospection, operationType, operation, useContext) {
|
|
60
|
+
const opWithContext = async (contextSpec, arg, options) => {
|
|
61
|
+
return _op(client, modelIntrospection, operationType, operation, arg, options, contextSpec);
|
|
62
|
+
};
|
|
63
|
+
const op = async (arg, options) => {
|
|
64
|
+
return _op(client, modelIntrospection, operationType, operation, arg, options);
|
|
65
|
+
};
|
|
66
|
+
return useContext ? opWithContext : op;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Runtime test and type guard to check whether `o[field]` is a `String`.
|
|
70
|
+
*
|
|
71
|
+
* ```typescript
|
|
72
|
+
* if (hasStringField(o, 'prop')) {
|
|
73
|
+
* const s = o.prop;
|
|
74
|
+
* // ^? const s: string
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @param o Object to inspect
|
|
79
|
+
* @param field Field to look for
|
|
80
|
+
* @returns Boolean: `true` if the `o[field]` is a `string`
|
|
81
|
+
*/
|
|
82
|
+
function hasStringField(o, field) {
|
|
83
|
+
return typeof o[field] === 'string';
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Generates "outer" arguments string for a custom operation. For example,
|
|
87
|
+
* in this operation:
|
|
88
|
+
*
|
|
89
|
+
* ```graphql
|
|
90
|
+
* query MyQuery(InputString: String!) {
|
|
91
|
+
* echoString(InputString: $InputString)
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* This function returns the top/outer level arguments as a string:
|
|
96
|
+
*
|
|
97
|
+
* ```json
|
|
98
|
+
* "InputString: String!"
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* @param operation Operation object from model introspection schema.
|
|
102
|
+
* @returns "outer" arguments string
|
|
103
|
+
*/
|
|
104
|
+
function outerArguments(operation) {
|
|
105
|
+
const args = Object.entries(operation.arguments)
|
|
106
|
+
.map(([k, v]) => {
|
|
107
|
+
const baseType = v.type + (v.isRequired ? '!' : '');
|
|
108
|
+
const finalType = v.isArray
|
|
109
|
+
? `[${baseType}]${v.isArrayNullable ? '' : '!'}`
|
|
110
|
+
: baseType;
|
|
111
|
+
return `$${k}: ${finalType}`;
|
|
112
|
+
})
|
|
113
|
+
.join(', ');
|
|
114
|
+
return args.length > 0 ? `(${args})` : '';
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Generates "inner" arguments string for a custom operation. For example,
|
|
118
|
+
* in this operation:
|
|
119
|
+
*
|
|
120
|
+
* ```graphql
|
|
121
|
+
* query MyQuery(InputString: String!) {
|
|
122
|
+
* echoString(InputString: $InputString)
|
|
123
|
+
* }
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
* This function returns the inner arguments as a string:
|
|
127
|
+
*
|
|
128
|
+
* ```json
|
|
129
|
+
* "InputString: $InputString"
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* @param operation Operation object from model introspection schema.
|
|
133
|
+
* @returns "outer" arguments string
|
|
134
|
+
*/
|
|
135
|
+
function innerArguments(operation) {
|
|
136
|
+
const args = Object.entries(operation.arguments)
|
|
137
|
+
.map(([k, v]) => `${k}: $${k}`)
|
|
138
|
+
.join(', ');
|
|
139
|
+
return args.length > 0 ? `(${args})` : '';
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Accepts a Model or NonModel from generated introspection schema and
|
|
143
|
+
* generates the selection set string.
|
|
144
|
+
*
|
|
145
|
+
* @param nonModel Model or nonModel object from introspection schema.
|
|
146
|
+
* @returns String selection set.
|
|
147
|
+
*/
|
|
148
|
+
function modelishTypeSelectionSet(nonModel) {
|
|
149
|
+
return Object.values(nonModel.fields)
|
|
150
|
+
.filter(field => hasStringField(field, 'type') ||
|
|
151
|
+
hasStringField(field.type, 'nonModel') ||
|
|
152
|
+
hasStringField(field.type, 'enum'))
|
|
153
|
+
.map(field => field.name)
|
|
154
|
+
.join(' ');
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Generates the selection set string for a custom operation. This is slightly
|
|
158
|
+
* different than the selection set generation for models. If the custom op returns
|
|
159
|
+
* a primitive or enum types, it doen't require a selection set at all.
|
|
160
|
+
*
|
|
161
|
+
* E.g., the graphql might look like this:
|
|
162
|
+
*
|
|
163
|
+
* ```graphql
|
|
164
|
+
* query MyQuery {
|
|
165
|
+
* echoString(inputString: "whatever")
|
|
166
|
+
* }
|
|
167
|
+
* # ^
|
|
168
|
+
* # |
|
|
169
|
+
* # +-- no selection set
|
|
170
|
+
* ```
|
|
171
|
+
*
|
|
172
|
+
* Non-primitive return type selection set generation will be similar to other
|
|
173
|
+
* model operations.
|
|
174
|
+
*
|
|
175
|
+
* @param modelIntrospection The full code-generated introspection schema.
|
|
176
|
+
* @param operation The operation object from the schema.
|
|
177
|
+
* @returns The selection set as a string.
|
|
178
|
+
*/
|
|
179
|
+
function operationSelectionSet(modelIntrospection, operation) {
|
|
180
|
+
if (hasStringField(operation, 'type') ||
|
|
181
|
+
hasStringField(operation.type, 'enum')) {
|
|
182
|
+
return '';
|
|
183
|
+
}
|
|
184
|
+
else if (hasStringField(operation.type, 'nonModel')) {
|
|
185
|
+
const model = modelIntrospection.nonModels[operation.type.nonModel];
|
|
186
|
+
return `{${modelishTypeSelectionSet(model)}}`;
|
|
187
|
+
}
|
|
188
|
+
else if (hasStringField(operation.type, 'model')) {
|
|
189
|
+
const nonModel = modelIntrospection.models[operation.type.model];
|
|
190
|
+
return `{${modelishTypeSelectionSet(nonModel)}}`;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
return '';
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Maps an arguments objec to graphql variables, removing superfluous args and
|
|
198
|
+
* screaming loudly when required args are missing.
|
|
199
|
+
*
|
|
200
|
+
* @param operation The operation to construct graphql request variables for.
|
|
201
|
+
* @param args The arguments to map variables from.
|
|
202
|
+
* @returns The graphql variables object.
|
|
203
|
+
*/
|
|
204
|
+
function operationVariables(operation, args = {}) {
|
|
205
|
+
const variables = {};
|
|
206
|
+
for (const argDef of Object.values(operation.arguments)) {
|
|
207
|
+
if (typeof args[argDef.name] !== 'undefined') {
|
|
208
|
+
variables[argDef.name] = args[argDef.name];
|
|
209
|
+
}
|
|
210
|
+
else if (argDef.isRequired) {
|
|
211
|
+
// At this point, the variable is both required and missing: We don't need
|
|
212
|
+
// to continue. The operation is expected to fail.
|
|
213
|
+
throw new Error(`${operation.name} requires arguments '${argDef.name}'`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return variables;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Executes an operation from the given model intro schema against a client, returning
|
|
220
|
+
* a fully instantiated model when relevant.
|
|
221
|
+
*
|
|
222
|
+
* @param client The client to operate `graphql()` calls through.
|
|
223
|
+
* @param modelIntrospection The model intro schema to construct requests from.
|
|
224
|
+
* @param operationType The high level graphql operation type.
|
|
225
|
+
* @param operation The specific operation name, args, return type details.
|
|
226
|
+
* @param args The arguments to provide to the operation as variables.
|
|
227
|
+
* @param options Request options like headers, etc.
|
|
228
|
+
* @param context SSR context if relevant.
|
|
229
|
+
* @returns Result from the graphql request, model-instantiated when relevant.
|
|
230
|
+
*/
|
|
231
|
+
async function _op(client, modelIntrospection, operationType, operation, args, options, context) {
|
|
232
|
+
const { name: operationName } = operation;
|
|
233
|
+
const auth = authModeParams(client, options);
|
|
234
|
+
const headers = getCustomHeaders(client, options?.headers);
|
|
235
|
+
const outerArgsString = outerArguments(operation);
|
|
236
|
+
const innerArgsString = innerArguments(operation);
|
|
237
|
+
const selectionSet = operationSelectionSet(modelIntrospection, operation);
|
|
238
|
+
const returnTypeModelName = hasStringField(operation.type, 'model')
|
|
239
|
+
? operation.type.model
|
|
240
|
+
: undefined;
|
|
241
|
+
const query = `
|
|
242
|
+
${operationType.toLocaleLowerCase()}${outerArgsString} {
|
|
243
|
+
${operationName}${innerArgsString} ${selectionSet}
|
|
244
|
+
}
|
|
245
|
+
`;
|
|
246
|
+
const variables = operationVariables(operation, args);
|
|
247
|
+
try {
|
|
248
|
+
const { data, extensions } = context
|
|
249
|
+
? (await client.graphql(context, {
|
|
250
|
+
...auth,
|
|
251
|
+
query,
|
|
252
|
+
variables,
|
|
253
|
+
}, headers))
|
|
254
|
+
: (await client.graphql({
|
|
255
|
+
...auth,
|
|
256
|
+
query,
|
|
257
|
+
variables,
|
|
258
|
+
}, headers));
|
|
259
|
+
// flatten response
|
|
260
|
+
if (data) {
|
|
261
|
+
const [key] = Object.keys(data);
|
|
262
|
+
const flattenedResult = flattenItems(data)[key];
|
|
263
|
+
// TODO: custom selection set. current selection set is default selection set only
|
|
264
|
+
// custom selection set requires data-schema-type + runtime updates above.
|
|
265
|
+
const [initialized] = returnTypeModelName
|
|
266
|
+
? initializeModel(client, returnTypeModelName, [flattenedResult], modelIntrospection, auth.authMode, auth.authToken, !!context)
|
|
267
|
+
: [flattenedResult];
|
|
268
|
+
return { data: initialized, extensions };
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
return { data: null, extensions };
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
if (error.errors) {
|
|
276
|
+
// graphql errors pass through
|
|
277
|
+
return error;
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
// non-graphql errors re re-thrown
|
|
281
|
+
throw error;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export { customOpFactory };
|
|
287
|
+
//# sourceMappingURL=custom.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"custom.mjs","sources":["../../../../src/internals/operations/custom.ts"],"sourcesContent":["import { initializeModel, flattenItems, authModeParams, getCustomHeaders, } from '../APIClient';\n/**\n * Builds an operation function, embedded with all client and context data, that\n * can be attached to a client as a custom query or mutation.\n *\n * If we have this source schema:\n *\n * ```typescript\n * a.schema({\n * echo: a.query()\n * .arguments({input: a.string().required()})\n * .returns(a.string())\n * })\n * ```\n *\n * Our model intro schema will contain an entry like this:\n *\n * ```ts\n * {\n * queries: {\n * echo: {\n * name: \"echo\",\n * isArray: false,\n * type: 'String',\n * isRequired: false,\n * arguments: {\n * input: {\n * name: 'input',\n * isArray: false,\n * type: String,\n * isRequired: true\n * }\n * }\n * }\n * }\n * }\n * ```\n *\n * The `echo` object is used to build the `echo' method that goes here:\n *\n * ```typescript\n * const client = generateClent()\n * const { data } = await client.queries.echo({input: 'a string'});\n * // ^\n * // |\n * // +-- This one right here.\n * //\n * ```\n *\n *\n * @param client The client to run graphql queries through.\n * @param modelIntrospection The model introspection schema the op comes from.\n * @param operationType The broad category of graphql operation.\n * @param operation The operation definition from the introspection schema.\n * @param useContext Whether the function needs to accept an SSR context.\n * @returns The operation function to attach to query, mutations, etc.\n */\nexport function customOpFactory(client, modelIntrospection, operationType, operation, useContext) {\n const opWithContext = async (contextSpec, arg, options) => {\n return _op(client, modelIntrospection, operationType, operation, arg, options, contextSpec);\n };\n const op = async (arg, options) => {\n return _op(client, modelIntrospection, operationType, operation, arg, options);\n };\n return useContext ? opWithContext : op;\n}\n/**\n * Runtime test and type guard to check whether `o[field]` is a `String`.\n *\n * ```typescript\n * if (hasStringField(o, 'prop')) {\n * const s = o.prop;\n * // ^? const s: string\n * }\n * ```\n *\n * @param o Object to inspect\n * @param field Field to look for\n * @returns Boolean: `true` if the `o[field]` is a `string`\n */\nfunction hasStringField(o, field) {\n return typeof o[field] === 'string';\n}\n/**\n * Generates \"outer\" arguments string for a custom operation. For example,\n * in this operation:\n *\n * ```graphql\n * query MyQuery(InputString: String!) {\n * echoString(InputString: $InputString)\n * }\n * ```\n *\n * This function returns the top/outer level arguments as a string:\n *\n * ```json\n * \"InputString: String!\"\n * ```\n *\n * @param operation Operation object from model introspection schema.\n * @returns \"outer\" arguments string\n */\nfunction outerArguments(operation) {\n const args = Object.entries(operation.arguments)\n .map(([k, v]) => {\n const baseType = v.type + (v.isRequired ? '!' : '');\n const finalType = v.isArray\n ? `[${baseType}]${v.isArrayNullable ? '' : '!'}`\n : baseType;\n return `$${k}: ${finalType}`;\n })\n .join(', ');\n return args.length > 0 ? `(${args})` : '';\n}\n/**\n * Generates \"inner\" arguments string for a custom operation. For example,\n * in this operation:\n *\n * ```graphql\n * query MyQuery(InputString: String!) {\n * echoString(InputString: $InputString)\n * }\n * ```\n *\n * This function returns the inner arguments as a string:\n *\n * ```json\n * \"InputString: $InputString\"\n * ```\n *\n * @param operation Operation object from model introspection schema.\n * @returns \"outer\" arguments string\n */\nfunction innerArguments(operation) {\n const args = Object.entries(operation.arguments)\n .map(([k, v]) => `${k}: $${k}`)\n .join(', ');\n return args.length > 0 ? `(${args})` : '';\n}\n/**\n * Accepts a Model or NonModel from generated introspection schema and\n * generates the selection set string.\n *\n * @param nonModel Model or nonModel object from introspection schema.\n * @returns String selection set.\n */\nfunction modelishTypeSelectionSet(nonModel) {\n return Object.values(nonModel.fields)\n .filter(field => hasStringField(field, 'type') ||\n hasStringField(field.type, 'nonModel') ||\n hasStringField(field.type, 'enum'))\n .map(field => field.name)\n .join(' ');\n}\n/**\n * Generates the selection set string for a custom operation. This is slightly\n * different than the selection set generation for models. If the custom op returns\n * a primitive or enum types, it doen't require a selection set at all.\n *\n * E.g., the graphql might look like this:\n *\n * ```graphql\n * query MyQuery {\n * echoString(inputString: \"whatever\")\n * }\n * # ^\n * # |\n * # +-- no selection set\n * ```\n *\n * Non-primitive return type selection set generation will be similar to other\n * model operations.\n *\n * @param modelIntrospection The full code-generated introspection schema.\n * @param operation The operation object from the schema.\n * @returns The selection set as a string.\n */\nfunction operationSelectionSet(modelIntrospection, operation) {\n if (hasStringField(operation, 'type') ||\n hasStringField(operation.type, 'enum')) {\n return '';\n }\n else if (hasStringField(operation.type, 'nonModel')) {\n const model = modelIntrospection.nonModels[operation.type.nonModel];\n return `{${modelishTypeSelectionSet(model)}}`;\n }\n else if (hasStringField(operation.type, 'model')) {\n const nonModel = modelIntrospection.models[operation.type.model];\n return `{${modelishTypeSelectionSet(nonModel)}}`;\n }\n else {\n return '';\n }\n}\n/**\n * Maps an arguments objec to graphql variables, removing superfluous args and\n * screaming loudly when required args are missing.\n *\n * @param operation The operation to construct graphql request variables for.\n * @param args The arguments to map variables from.\n * @returns The graphql variables object.\n */\nfunction operationVariables(operation, args = {}) {\n const variables = {};\n for (const argDef of Object.values(operation.arguments)) {\n if (typeof args[argDef.name] !== 'undefined') {\n variables[argDef.name] = args[argDef.name];\n }\n else if (argDef.isRequired) {\n // At this point, the variable is both required and missing: We don't need\n // to continue. The operation is expected to fail.\n throw new Error(`${operation.name} requires arguments '${argDef.name}'`);\n }\n }\n return variables;\n}\n/**\n * Executes an operation from the given model intro schema against a client, returning\n * a fully instantiated model when relevant.\n *\n * @param client The client to operate `graphql()` calls through.\n * @param modelIntrospection The model intro schema to construct requests from.\n * @param operationType The high level graphql operation type.\n * @param operation The specific operation name, args, return type details.\n * @param args The arguments to provide to the operation as variables.\n * @param options Request options like headers, etc.\n * @param context SSR context if relevant.\n * @returns Result from the graphql request, model-instantiated when relevant.\n */\nasync function _op(client, modelIntrospection, operationType, operation, args, options, context) {\n const { name: operationName } = operation;\n const auth = authModeParams(client, options);\n const headers = getCustomHeaders(client, options?.headers);\n const outerArgsString = outerArguments(operation);\n const innerArgsString = innerArguments(operation);\n const selectionSet = operationSelectionSet(modelIntrospection, operation);\n const returnTypeModelName = hasStringField(operation.type, 'model')\n ? operation.type.model\n : undefined;\n const query = `\n\t\t${operationType.toLocaleLowerCase()}${outerArgsString} {\n\t\t\t${operationName}${innerArgsString} ${selectionSet}\n\t\t}\n\t`;\n const variables = operationVariables(operation, args);\n try {\n const { data, extensions } = context\n ? (await client.graphql(context, {\n ...auth,\n query,\n variables,\n }, headers))\n : (await client.graphql({\n ...auth,\n query,\n variables,\n }, headers));\n // flatten response\n if (data) {\n const [key] = Object.keys(data);\n const flattenedResult = flattenItems(data)[key];\n // TODO: custom selection set. current selection set is default selection set only\n // custom selection set requires data-schema-type + runtime updates above.\n const [initialized] = returnTypeModelName\n ? initializeModel(client, returnTypeModelName, [flattenedResult], modelIntrospection, auth.authMode, auth.authToken, !!context)\n : [flattenedResult];\n return { data: initialized, extensions };\n }\n else {\n return { data: null, extensions };\n }\n }\n catch (error) {\n if (error.errors) {\n // graphql errors pass through\n return error;\n }\n else {\n // non-graphql errors re re-thrown\n throw error;\n }\n }\n}\n"],"names":[],"mappings":";;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,eAAe,CAAC,MAAM,EAAE,kBAAkB,EAAE,aAAa,EAAE,SAAS,EAAE,UAAU,EAAE;AAClG,IAAI,MAAM,aAAa,GAAG,OAAO,WAAW,EAAE,GAAG,EAAE,OAAO,KAAK;AAC/D,QAAQ,OAAO,GAAG,CAAC,MAAM,EAAE,kBAAkB,EAAE,aAAa,EAAE,SAAS,EAAE,GAAG,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC;AACpG,KAAK,CAAC;AACN,I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
2
|
import { graphql, cancel, isCancelError } from '../v6.mjs';
|
|
3
3
|
import '../generateClient.mjs';
|
|
4
4
|
import { generateModelsProperty } from './generateModelsProperty.mjs';
|
|
5
|
-
import { __amplify, __authMode, __authToken } from '../../types/index.mjs';
|
|
5
|
+
import { __amplify, __authMode, __authToken, __headers } from '../../types/index.mjs';
|
|
6
|
+
import { isApiGraphQLConfig } from '../utils/isApiGraphQLProviderConfig.mjs';
|
|
7
|
+
import { generateEnumsProperty } from '../utils/generateEnumsProperty.mjs';
|
|
8
|
+
import { generateCustomQueriesProperty, generateCustomMutationsProperty } from '../generateCustomOperationsProperty.mjs';
|
|
6
9
|
|
|
7
10
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
8
11
|
// SPDX-License-Identifier: Apache-2.0
|
|
@@ -22,11 +25,18 @@ function generateClientWithAmplifyInstance(params) {
|
|
|
22
25
|
[__amplify]: params.amplify,
|
|
23
26
|
[__authMode]: params.authMode,
|
|
24
27
|
[__authToken]: params.authToken,
|
|
28
|
+
[__headers]: params.headers,
|
|
25
29
|
graphql,
|
|
26
30
|
cancel,
|
|
27
31
|
isCancelError,
|
|
28
32
|
};
|
|
29
|
-
|
|
33
|
+
const apiGraphqlConfig = params.config?.API?.GraphQL;
|
|
34
|
+
if (isApiGraphQLConfig(apiGraphqlConfig)) {
|
|
35
|
+
client.models = generateModelsProperty(client, params);
|
|
36
|
+
client.enums = generateEnumsProperty(apiGraphqlConfig);
|
|
37
|
+
client.queries = generateCustomQueriesProperty(client, apiGraphqlConfig);
|
|
38
|
+
client.mutations = generateCustomMutationsProperty(client, apiGraphqlConfig);
|
|
39
|
+
}
|
|
30
40
|
return client;
|
|
31
41
|
}
|
|
32
42
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateClientWithAmplifyInstance.mjs","sources":["../../../../src/internals/server/generateClientWithAmplifyInstance.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { graphql, cancel, isCancelError } from '..';\nimport { generateModelsProperty } from './generateModelsProperty';\nimport { __amplify, __authMode, __authToken, } from '../../types';\n/**\n * @private\n *\n * Used internally by `adapter-nextjs` package.\n *\n * Creates a client that can be used to make GraphQL requests, using a provided `AmplifyClassV6`\n * compatible context object for config and auth fetching.\n *\n * @param params\n * @returns\n */\nexport function generateClientWithAmplifyInstance(params) {\n const client = {\n [__amplify]: params.amplify,\n [__authMode]: params.authMode,\n [__authToken]: params.authToken,\n graphql,\n cancel,\n isCancelError,\n };\n client.models = generateModelsProperty(client, params);\n return client;\n}\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"generateClientWithAmplifyInstance.mjs","sources":["../../../../src/internals/server/generateClientWithAmplifyInstance.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { graphql, cancel, isCancelError } from '..';\nimport { generateModelsProperty } from './generateModelsProperty';\nimport { __amplify, __authMode, __authToken, __headers, } from '../../types';\nimport { isApiGraphQLConfig } from '../utils/isApiGraphQLProviderConfig';\nimport { generateEnumsProperty } from '../utils/generateEnumsProperty';\nimport { generateCustomMutationsProperty, generateCustomQueriesProperty, } from '../generateCustomOperationsProperty';\n/**\n * @private\n *\n * Used internally by `adapter-nextjs` package.\n *\n * Creates a client that can be used to make GraphQL requests, using a provided `AmplifyClassV6`\n * compatible context object for config and auth fetching.\n *\n * @param params\n * @returns\n */\nexport function generateClientWithAmplifyInstance(params) {\n const client = {\n [__amplify]: params.amplify,\n [__authMode]: params.authMode,\n [__authToken]: params.authToken,\n [__headers]: params.headers,\n graphql,\n cancel,\n isCancelError,\n };\n const apiGraphqlConfig = params.config?.API?.GraphQL;\n if (isApiGraphQLConfig(apiGraphqlConfig)) {\n client.models = generateModelsProperty(client, params);\n client.enums = generateEnumsProperty(apiGraphqlConfig);\n client.queries = generateCustomQueriesProperty(client, apiGraphqlConfig);\n client.mutations = generateCustomMutationsProperty(client, apiGraphqlConfig);\n }\n return client;\n}\n"],"names":[],"mappings":";;;;;;;;;AAAA;AACA;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,iCAAiC,CAAC,MAAM,EAAE;AAC1D,IAAI,MAAM,MAAM,GAAG;AACnB,QAAQ,CAAC,SAAS,GAAG,MAAM,CAAC,OAAO;AACnC,QAAQ,CAAC,UAAU,GAAG,MAAM,CAAC,QAAQ;AACrC,QAAQ,CAAC,WAAW,GAAG,MAAM,CAAC,SAAS;AACvC,QAAQ,CAAC,SAAS,GAAG,MAAM,CAAC,OAAO;AACnC,QAAQ,OAAO;AACf,QAAQ,MAAM;AACd,QAAQ,aAAa;AACrB,KAAK,CAAC;AACN,IAAI,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC;AACzD,IAAI,IAAI,kBAAkB,CAAC,gBAAgB,CAAC,EAAE;AAC9C,QAAQ,MAAM,CAAC,MAAM,GAAG,sBAAsB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAC/D,QAAQ,MAAM,CAAC,KAAK,GAAG,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;AAC/D,QAAQ,MAAM,CAAC,OAAO,GAAG,6BAA6B,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;AACjF,QAAQ,MAAM,CAAC,SAAS,GAAG,+BAA+B,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;AACrF,KAAK;AACL,IAAI,OAAO,MAAM,CAAC;AAClB;;;;"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AmplifyClassV6, ResourcesConfig } from '@aws-amplify/core';
|
|
2
|
-
import { ModelTypes, CustomHeaders,
|
|
2
|
+
import { EnumTypes, ModelTypes, CustomHeaders, CustomQueries, CustomMutations } from '@aws-amplify/data-schema-types';
|
|
3
3
|
import { Source, DocumentNode, GraphQLError } from 'graphql';
|
|
4
4
|
export { OperationTypeNode } from 'graphql';
|
|
5
5
|
import { Observable } from 'rxjs';
|
|
@@ -295,6 +295,8 @@ export type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
|
|
|
295
295
|
isCancelError: (error: any) => boolean;
|
|
296
296
|
models: ModelTypes<T>;
|
|
297
297
|
enums: EnumTypes<T>;
|
|
298
|
+
queries: CustomQueries<T>;
|
|
299
|
+
mutations: CustomMutations<T>;
|
|
298
300
|
}>;
|
|
299
301
|
export type V6ClientSSRRequest<T extends Record<any, any> = never> = ExcludeNeverFields<{
|
|
300
302
|
[__amplify]: AmplifyClassV6;
|
|
@@ -305,6 +307,9 @@ export type V6ClientSSRRequest<T extends Record<any, any> = never> = ExcludeNeve
|
|
|
305
307
|
cancel: (promise: Promise<any>, message?: string) => boolean;
|
|
306
308
|
isCancelError: (error: any) => boolean;
|
|
307
309
|
models: ModelTypes<T, 'REQUEST'>;
|
|
310
|
+
enums: EnumTypes<T>;
|
|
311
|
+
queries: CustomQueries<T, 'REQUEST'>;
|
|
312
|
+
mutations: CustomMutations<T, 'REQUEST'>;
|
|
308
313
|
}>;
|
|
309
314
|
export type V6ClientSSRCookies<T extends Record<any, any> = never> = ExcludeNeverFields<{
|
|
310
315
|
[__amplify]: AmplifyClassV6;
|
|
@@ -315,6 +320,9 @@ export type V6ClientSSRCookies<T extends Record<any, any> = never> = ExcludeNeve
|
|
|
315
320
|
cancel: (promise: Promise<any>, message?: string) => boolean;
|
|
316
321
|
isCancelError: (error: any) => boolean;
|
|
317
322
|
models: ModelTypes<T, 'COOKIES'>;
|
|
323
|
+
enums: EnumTypes<T>;
|
|
324
|
+
queries: CustomQueries<T, 'COOKIES'>;
|
|
325
|
+
mutations: CustomMutations<T, 'COOKIES'>;
|
|
318
326
|
}>;
|
|
319
327
|
export type GraphQLMethod = <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: CustomHeaders | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
|
320
328
|
export type GraphQLMethodSSR = <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(contextSpec: AmplifyServer.ContextSpec, options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: CustomHeaders | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/api-graphql",
|
|
3
|
-
"version": "4.0.18-unstable.
|
|
3
|
+
"version": "4.0.18-unstable.7e4eff3.0+7e4eff3",
|
|
4
4
|
"description": "Api-graphql category of aws-amplify",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.mjs",
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
},
|
|
72
72
|
"homepage": "https://aws-amplify.github.io/",
|
|
73
73
|
"devDependencies": {
|
|
74
|
-
"@aws-amplify/data-schema": "^0.13.
|
|
74
|
+
"@aws-amplify/data-schema": "^0.13.6",
|
|
75
75
|
"@rollup/plugin-typescript": "11.1.5",
|
|
76
76
|
"rollup": "^4.9.6",
|
|
77
77
|
"typescript": "5.0.2"
|
|
@@ -84,9 +84,9 @@
|
|
|
84
84
|
"server"
|
|
85
85
|
],
|
|
86
86
|
"dependencies": {
|
|
87
|
-
"@aws-amplify/api-rest": "4.0.18-unstable.
|
|
88
|
-
"@aws-amplify/core": "6.0.18-unstable.
|
|
89
|
-
"@aws-amplify/data-schema-types": "^0.7.
|
|
87
|
+
"@aws-amplify/api-rest": "4.0.18-unstable.7e4eff3.0+7e4eff3",
|
|
88
|
+
"@aws-amplify/core": "6.0.18-unstable.7e4eff3.0+7e4eff3",
|
|
89
|
+
"@aws-amplify/data-schema-types": "^0.7.5",
|
|
90
90
|
"@aws-sdk/types": "3.387.0",
|
|
91
91
|
"graphql": "15.8.0",
|
|
92
92
|
"rxjs": "^7.8.1",
|
|
@@ -101,5 +101,5 @@
|
|
|
101
101
|
"limit": "91.7 kB"
|
|
102
102
|
}
|
|
103
103
|
],
|
|
104
|
-
"gitHead": "
|
|
104
|
+
"gitHead": "7e4eff3be3ec25520332330461ca2cefbf7bdbb7"
|
|
105
105
|
}
|
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { Hub } from '@aws-amplify/core';
|
|
4
|
-
import { EnumTypes, ModelTypes } from '@aws-amplify/data-schema-types';
|
|
5
4
|
import { graphql, cancel, isCancelError } from './v6';
|
|
5
|
+
import {
|
|
6
|
+
EnumTypes,
|
|
7
|
+
ModelTypes,
|
|
8
|
+
CustomQueries,
|
|
9
|
+
CustomMutations,
|
|
10
|
+
} from '@aws-amplify/data-schema-types';
|
|
6
11
|
import { generateEnumsProperty } from './utils/generateEnumsProperty';
|
|
7
12
|
import { generateModelsProperty } from './utils/generateModelsProperty';
|
|
8
13
|
import { isApiGraphQLConfig } from './utils/isApiGraphQLProviderConfig';
|
|
14
|
+
import {
|
|
15
|
+
generateCustomQueriesProperty,
|
|
16
|
+
generateCustomMutationsProperty,
|
|
17
|
+
} from './generateCustomOperationsProperty';
|
|
9
18
|
import {
|
|
10
19
|
V6Client,
|
|
11
20
|
__amplify,
|
|
@@ -38,6 +47,8 @@ export function generateClient<T extends Record<any, any> = never>(
|
|
|
38
47
|
isCancelError,
|
|
39
48
|
models: emptyProperty as ModelTypes<never>,
|
|
40
49
|
enums: emptyProperty as EnumTypes<never>,
|
|
50
|
+
queries: emptyProperty as CustomQueries<never>,
|
|
51
|
+
mutations: emptyProperty as CustomMutations<never>,
|
|
41
52
|
} as any;
|
|
42
53
|
|
|
43
54
|
const apiGraphqlConfig = params.amplify.getConfig().API?.GraphQL;
|
|
@@ -45,6 +56,11 @@ export function generateClient<T extends Record<any, any> = never>(
|
|
|
45
56
|
if (isApiGraphQLConfig(apiGraphqlConfig)) {
|
|
46
57
|
client.models = generateModelsProperty<T>(client, apiGraphqlConfig);
|
|
47
58
|
client.enums = generateEnumsProperty<T>(apiGraphqlConfig);
|
|
59
|
+
client.queries = generateCustomQueriesProperty<T>(client, apiGraphqlConfig);
|
|
60
|
+
client.mutations = generateCustomMutationsProperty<T>(
|
|
61
|
+
client,
|
|
62
|
+
apiGraphqlConfig,
|
|
63
|
+
);
|
|
48
64
|
} else {
|
|
49
65
|
// This happens when the `Amplify.configure()` call gets evaluated after the `generateClient()` call.
|
|
50
66
|
//
|
|
@@ -75,6 +91,14 @@ const generateModelsPropertyOnAmplifyConfigure = (clientRef: any) => {
|
|
|
75
91
|
if (isApiGraphQLConfig(apiGraphQLConfig)) {
|
|
76
92
|
clientRef.models = generateModelsProperty(clientRef, apiGraphQLConfig);
|
|
77
93
|
clientRef.enums = generateEnumsProperty(apiGraphQLConfig);
|
|
94
|
+
clientRef.queries = generateCustomQueriesProperty(
|
|
95
|
+
clientRef,
|
|
96
|
+
apiGraphQLConfig,
|
|
97
|
+
);
|
|
98
|
+
clientRef.mutations = generateCustomMutationsProperty(
|
|
99
|
+
clientRef,
|
|
100
|
+
apiGraphQLConfig,
|
|
101
|
+
);
|
|
78
102
|
}
|
|
79
103
|
});
|
|
80
104
|
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { CustomQueries, CustomMutations } from '@aws-amplify/data-schema-types';
|
|
4
|
+
import { V6Client, __authMode, __authToken, __amplify } from '../types';
|
|
5
|
+
|
|
6
|
+
import { customOpFactory } from './operations/custom';
|
|
7
|
+
import {
|
|
8
|
+
ModelIntrospectionSchema,
|
|
9
|
+
GraphQLProviderConfig,
|
|
10
|
+
} from '@aws-amplify/core/internals/utils';
|
|
11
|
+
|
|
12
|
+
type OpTypes = 'queries' | 'mutations';
|
|
13
|
+
|
|
14
|
+
type CustomOpsProperty<
|
|
15
|
+
T extends Record<any, any>,
|
|
16
|
+
OpType extends OpTypes,
|
|
17
|
+
> = OpType extends 'queries' ? CustomQueries<T> : CustomMutations<T>;
|
|
18
|
+
|
|
19
|
+
const operationTypeMap = {
|
|
20
|
+
queries: 'query',
|
|
21
|
+
mutations: 'mutation',
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
export function generateCustomOperationsProperty<
|
|
25
|
+
T extends Record<any, any>,
|
|
26
|
+
OpType extends OpTypes,
|
|
27
|
+
>(
|
|
28
|
+
client: V6Client<Record<string, any>>,
|
|
29
|
+
config: GraphQLProviderConfig['GraphQL'],
|
|
30
|
+
operationsType: OpType,
|
|
31
|
+
): OpType extends 'queries' ? CustomQueries<T> : CustomMutations<T> {
|
|
32
|
+
// some bundlers end up with `Amplify.configure` being called *after* generate client.
|
|
33
|
+
// if that occurs, we need to *not error* while we wait. handling for late configuration
|
|
34
|
+
// occurs in `generateClient()`. we do not need to subscribe to Hub events here.
|
|
35
|
+
if (!config) {
|
|
36
|
+
return {} as CustomOpsProperty<T, OpType>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const modelIntrospection: ModelIntrospectionSchema | undefined =
|
|
40
|
+
config.modelIntrospection;
|
|
41
|
+
|
|
42
|
+
// model intro schema might be absent if there's not actually a configured GraphQL API
|
|
43
|
+
if (!modelIntrospection) {
|
|
44
|
+
return {} as CustomOpsProperty<T, OpType>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// custom operations will be absent from model intro schema if no custom ops
|
|
48
|
+
// are present on the source schema.
|
|
49
|
+
const operations = modelIntrospection[operationsType];
|
|
50
|
+
if (!operations) {
|
|
51
|
+
return {} as CustomOpsProperty<T, OpType>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const ops = {} as CustomOpsProperty<T, OpType>;
|
|
55
|
+
const useContext = client[__amplify] === null;
|
|
56
|
+
for (const operation of Object.values(operations)) {
|
|
57
|
+
(ops as any)[operation.name] = customOpFactory(
|
|
58
|
+
client,
|
|
59
|
+
modelIntrospection,
|
|
60
|
+
operationTypeMap[operationsType],
|
|
61
|
+
operation,
|
|
62
|
+
useContext
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return ops;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function generateCustomMutationsProperty<T extends Record<any, any>>(
|
|
70
|
+
client: V6Client<Record<string, any>>,
|
|
71
|
+
config: GraphQLProviderConfig['GraphQL'],
|
|
72
|
+
) {
|
|
73
|
+
return generateCustomOperationsProperty<T, 'mutations'>(
|
|
74
|
+
client,
|
|
75
|
+
config,
|
|
76
|
+
'mutations',
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function generateCustomQueriesProperty<T extends Record<any, any>>(
|
|
81
|
+
client: V6Client<Record<string, any>>,
|
|
82
|
+
config: GraphQLProviderConfig['GraphQL'],
|
|
83
|
+
) {
|
|
84
|
+
return generateCustomOperationsProperty<T, 'queries'>(
|
|
85
|
+
client,
|
|
86
|
+
config,
|
|
87
|
+
'queries',
|
|
88
|
+
);
|
|
89
|
+
}
|