@ttoss/appsync-api 0.23.12 → 0.24.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/esm/index.js CHANGED
@@ -5,6 +5,15 @@ var __name = (target, value) => __defProp(target, "name", {
5
5
  configurable: true
6
6
  });
7
7
 
8
+ // src/appSyncMiddleware.ts
9
+ var createAppSyncMiddleware = /* @__PURE__ */__name(fn => {
10
+ return (resolve, source, args, context, info) => {
11
+ return Promise.resolve(fn((src, innerArgs, ctx, innerInfo) => {
12
+ return resolve(src, innerArgs, ctx, innerInfo);
13
+ }, source, args, context, info));
14
+ };
15
+ }, "createAppSyncMiddleware");
16
+
8
17
  // src/createApiTemplate.ts
9
18
  import { graphql } from "@ttoss/graphql-api";
10
19
  var AppSyncGraphQLApiLogicalId = "AppSyncGraphQLApi";
@@ -404,4 +413,4 @@ var AWSEmailTC = schemaComposer.createScalarTC(`scalar AWSEmail`);
404
413
  var AWSURLTC = schemaComposer.createScalarTC(`scalar AWSURL`);
405
414
  var AWSPhoneTC = schemaComposer.createScalarTC(`scalar AWSPhone`);
406
415
  var AWSIPAddressTC = schemaComposer.createScalarTC(`scalar AWSIPAddress`);
407
- export { AWSDateTC, AWSDateTimeTC, AWSEmailTC, AWSIPAddressTC, AWSJSONTC, AWSPhoneTC, AWSTimeTC, AWSTimestampTC, AWSURLTC, createApiTemplate, createAppSyncResolverHandler };
416
+ export { AWSDateTC, AWSDateTimeTC, AWSEmailTC, AWSIPAddressTC, AWSJSONTC, AWSPhoneTC, AWSTimeTC, AWSTimestampTC, AWSURLTC, createApiTemplate, createAppSyncMiddleware, createAppSyncResolverHandler };
package/dist/index.d.mts CHANGED
@@ -1,8 +1,62 @@
1
+ import { IMiddlewareFunction } from '@ttoss/graphql-api/shield';
1
2
  import * as _ttoss_graphql_api from '@ttoss/graphql-api';
2
3
  import { SchemaComposer, BuildSchemaInput } from '@ttoss/graphql-api';
3
4
  import { AppSyncResolverHandler as AppSyncResolverHandler$1, Context, AppSyncIdentity } from 'aws-lambda';
4
5
  export { AppSyncIdentityCognito } from 'aws-lambda';
5
6
 
7
+ /**
8
+ * The shape of the `info` object passed to AppSync resolvers at runtime.
9
+ *
10
+ * This differs from the standard `GraphQLResolveInfo` used by `graphql-middleware`.
11
+ * AppSync provides a flat `parentTypeName: string` field instead of the nested
12
+ * `parentType: { name: string }` object found in standard GraphQL execution.
13
+ *
14
+ * @see https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html
15
+ */
16
+ type AppSyncInfo = {
17
+ /** The name of the field that is currently being resolved. */
18
+ fieldName: string;
19
+ /** The name of the parent type for the field that is currently being resolved. */
20
+ parentTypeName: string;
21
+ /** A map which holds all variables that are passed into the GraphQL request. */
22
+ variables: Record<string, unknown>;
23
+ /** A list representation of the fields in the GraphQL selection set. */
24
+ selectionSetList: string[];
25
+ /** A string representation of the selection set, formatted as GraphQL SDL. */
26
+ selectionSetGraphQL: string;
27
+ };
28
+ type AppSyncMiddlewareFn<TSource, TContext, TArgs> = (resolve: (source: TSource, args: TArgs, context: TContext, info: AppSyncInfo) => unknown | Promise<unknown>, source: TSource, args: TArgs, context: TContext, info: AppSyncInfo) => unknown | Promise<unknown>;
29
+ /**
30
+ * Creates a properly-typed AppSync middleware function.
31
+ *
32
+ * When using `@ttoss/appsync-api`, the `info` object passed to resolvers has
33
+ * the AppSync-specific shape ({@link AppSyncInfo}), not the standard
34
+ * `GraphQLResolveInfo` expected by `graphql-middleware`. This helper lets you
35
+ * write middlewares with the correct AppSync `info` type while remaining
36
+ * compatible with the `BuildSchemaInput.middlewares` array.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import { createAppSyncMiddleware } from '@ttoss/appsync-api';
41
+ *
42
+ * const timingMiddleware = createAppSyncMiddleware(
43
+ * async (resolve, source, args, context, info) => {
44
+ * const start = Date.now();
45
+ * const resolverName = `${info.parentTypeName}.${info.fieldName}`;
46
+ * try {
47
+ * const result = await resolve(source, args, context, info);
48
+ * console.log(`${resolverName} took ${Date.now() - start}ms`);
49
+ * return result;
50
+ * } catch (error) {
51
+ * console.error(`${resolverName} failed after ${Date.now() - start}ms`);
52
+ * throw error;
53
+ * }
54
+ * }
55
+ * );
56
+ * ```
57
+ */
58
+ declare const createAppSyncMiddleware: <TSource = unknown, TContext = unknown, TArgs = unknown>(fn: AppSyncMiddlewareFn<TSource, TContext, TArgs>) => IMiddlewareFunction<TSource, TContext, TArgs>;
59
+
6
60
  type CloudFormationRef = {
7
61
  Ref: string;
8
62
  };
@@ -125,26 +179,27 @@ type BaseAppSyncContext = {
125
179
  /** The caller's identity (Cognito, IAM, Lambda, or OIDC). Null when using API key auth. */
126
180
  identity: AppSyncIdentity | null | undefined;
127
181
  };
182
+ /**
183
+ * Optional async function called once per request to enrich the resolver
184
+ * context. The returned object is shallow-merged into the base context and
185
+ * made available to every resolver.
186
+ *
187
+ * Use this for per-request setup such as resolving a `userId` from Cognito.
188
+ * For authorization rules or before/after resolver logic, prefer `middlewares`.
189
+ *
190
+ * @example
191
+ * ```ts
192
+ * createAppSyncResolverHandler({
193
+ * schemaComposer,
194
+ * createContext: async ({ identity }) => ({
195
+ * userId: await getUserIdFromCognitoSub(identity?.sub),
196
+ * }),
197
+ * });
198
+ * ```
199
+ */
200
+ type CreateContext = (baseContext: BaseAppSyncContext) => Promise<Record<string, any>> | Record<string, any>;
128
201
  declare const createAppSyncResolverHandler: ({ createContext, ...buildSchemaInput }: BuildSchemaInput & {
129
- /**
130
- * Optional async function called once per request to enrich the resolver
131
- * context. The returned object is shallow-merged into the base context and
132
- * made available to every resolver.
133
- *
134
- * Use this for per-request setup such as resolving a `userId` from Cognito.
135
- * For authorization rules or before/after resolver logic, prefer `middlewares`.
136
- *
137
- * @example
138
- * ```ts
139
- * createAppSyncResolverHandler({
140
- * schemaComposer,
141
- * createContext: async ({ identity }) => ({
142
- * userId: await getUserIdFromCognitoSub(identity?.sub),
143
- * }),
144
- * });
145
- * ```
146
- */
147
- createContext?: (baseContext: BaseAppSyncContext) => Promise<Record<string, any>> | Record<string, any>;
202
+ createContext?: CreateContext;
148
203
  }) => AppSyncResolverHandler<any, any, any>;
149
204
 
150
205
  /** AWS AppSync scalar for JSON data. Represents a JSON object or array. */
@@ -166,4 +221,4 @@ declare const AWSPhoneTC: _ttoss_graphql_api.ScalarTypeComposer<any>;
166
221
  /** AWS AppSync scalar for IPv4 and IPv6 addresses. */
167
222
  declare const AWSIPAddressTC: _ttoss_graphql_api.ScalarTypeComposer<any>;
168
223
 
169
- export { AWSDateTC, AWSDateTimeTC, AWSEmailTC, AWSIPAddressTC, AWSJSONTC, AWSPhoneTC, AWSTimeTC, AWSTimestampTC, AWSURLTC, type AppSyncResolverHandler, type BaseAppSyncContext, createApiTemplate, createAppSyncResolverHandler };
224
+ export { AWSDateTC, AWSDateTimeTC, AWSEmailTC, AWSIPAddressTC, AWSJSONTC, AWSPhoneTC, AWSTimeTC, AWSTimestampTC, AWSURLTC, type AppSyncInfo, type AppSyncResolverHandler, type BaseAppSyncContext, type CreateContext, createApiTemplate, createAppSyncMiddleware, createAppSyncResolverHandler };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,62 @@
1
+ import { IMiddlewareFunction } from '@ttoss/graphql-api/shield';
1
2
  import * as _ttoss_graphql_api from '@ttoss/graphql-api';
2
3
  import { SchemaComposer, BuildSchemaInput } from '@ttoss/graphql-api';
3
4
  import { AppSyncResolverHandler as AppSyncResolverHandler$1, Context, AppSyncIdentity } from 'aws-lambda';
4
5
  export { AppSyncIdentityCognito } from 'aws-lambda';
5
6
 
7
+ /**
8
+ * The shape of the `info` object passed to AppSync resolvers at runtime.
9
+ *
10
+ * This differs from the standard `GraphQLResolveInfo` used by `graphql-middleware`.
11
+ * AppSync provides a flat `parentTypeName: string` field instead of the nested
12
+ * `parentType: { name: string }` object found in standard GraphQL execution.
13
+ *
14
+ * @see https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html
15
+ */
16
+ type AppSyncInfo = {
17
+ /** The name of the field that is currently being resolved. */
18
+ fieldName: string;
19
+ /** The name of the parent type for the field that is currently being resolved. */
20
+ parentTypeName: string;
21
+ /** A map which holds all variables that are passed into the GraphQL request. */
22
+ variables: Record<string, unknown>;
23
+ /** A list representation of the fields in the GraphQL selection set. */
24
+ selectionSetList: string[];
25
+ /** A string representation of the selection set, formatted as GraphQL SDL. */
26
+ selectionSetGraphQL: string;
27
+ };
28
+ type AppSyncMiddlewareFn<TSource, TContext, TArgs> = (resolve: (source: TSource, args: TArgs, context: TContext, info: AppSyncInfo) => unknown | Promise<unknown>, source: TSource, args: TArgs, context: TContext, info: AppSyncInfo) => unknown | Promise<unknown>;
29
+ /**
30
+ * Creates a properly-typed AppSync middleware function.
31
+ *
32
+ * When using `@ttoss/appsync-api`, the `info` object passed to resolvers has
33
+ * the AppSync-specific shape ({@link AppSyncInfo}), not the standard
34
+ * `GraphQLResolveInfo` expected by `graphql-middleware`. This helper lets you
35
+ * write middlewares with the correct AppSync `info` type while remaining
36
+ * compatible with the `BuildSchemaInput.middlewares` array.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import { createAppSyncMiddleware } from '@ttoss/appsync-api';
41
+ *
42
+ * const timingMiddleware = createAppSyncMiddleware(
43
+ * async (resolve, source, args, context, info) => {
44
+ * const start = Date.now();
45
+ * const resolverName = `${info.parentTypeName}.${info.fieldName}`;
46
+ * try {
47
+ * const result = await resolve(source, args, context, info);
48
+ * console.log(`${resolverName} took ${Date.now() - start}ms`);
49
+ * return result;
50
+ * } catch (error) {
51
+ * console.error(`${resolverName} failed after ${Date.now() - start}ms`);
52
+ * throw error;
53
+ * }
54
+ * }
55
+ * );
56
+ * ```
57
+ */
58
+ declare const createAppSyncMiddleware: <TSource = unknown, TContext = unknown, TArgs = unknown>(fn: AppSyncMiddlewareFn<TSource, TContext, TArgs>) => IMiddlewareFunction<TSource, TContext, TArgs>;
59
+
6
60
  type CloudFormationRef = {
7
61
  Ref: string;
8
62
  };
@@ -125,26 +179,27 @@ type BaseAppSyncContext = {
125
179
  /** The caller's identity (Cognito, IAM, Lambda, or OIDC). Null when using API key auth. */
126
180
  identity: AppSyncIdentity | null | undefined;
127
181
  };
182
+ /**
183
+ * Optional async function called once per request to enrich the resolver
184
+ * context. The returned object is shallow-merged into the base context and
185
+ * made available to every resolver.
186
+ *
187
+ * Use this for per-request setup such as resolving a `userId` from Cognito.
188
+ * For authorization rules or before/after resolver logic, prefer `middlewares`.
189
+ *
190
+ * @example
191
+ * ```ts
192
+ * createAppSyncResolverHandler({
193
+ * schemaComposer,
194
+ * createContext: async ({ identity }) => ({
195
+ * userId: await getUserIdFromCognitoSub(identity?.sub),
196
+ * }),
197
+ * });
198
+ * ```
199
+ */
200
+ type CreateContext = (baseContext: BaseAppSyncContext) => Promise<Record<string, any>> | Record<string, any>;
128
201
  declare const createAppSyncResolverHandler: ({ createContext, ...buildSchemaInput }: BuildSchemaInput & {
129
- /**
130
- * Optional async function called once per request to enrich the resolver
131
- * context. The returned object is shallow-merged into the base context and
132
- * made available to every resolver.
133
- *
134
- * Use this for per-request setup such as resolving a `userId` from Cognito.
135
- * For authorization rules or before/after resolver logic, prefer `middlewares`.
136
- *
137
- * @example
138
- * ```ts
139
- * createAppSyncResolverHandler({
140
- * schemaComposer,
141
- * createContext: async ({ identity }) => ({
142
- * userId: await getUserIdFromCognitoSub(identity?.sub),
143
- * }),
144
- * });
145
- * ```
146
- */
147
- createContext?: (baseContext: BaseAppSyncContext) => Promise<Record<string, any>> | Record<string, any>;
202
+ createContext?: CreateContext;
148
203
  }) => AppSyncResolverHandler<any, any, any>;
149
204
 
150
205
  /** AWS AppSync scalar for JSON data. Represents a JSON object or array. */
@@ -166,4 +221,4 @@ declare const AWSPhoneTC: _ttoss_graphql_api.ScalarTypeComposer<any>;
166
221
  /** AWS AppSync scalar for IPv4 and IPv6 addresses. */
167
222
  declare const AWSIPAddressTC: _ttoss_graphql_api.ScalarTypeComposer<any>;
168
223
 
169
- export { AWSDateTC, AWSDateTimeTC, AWSEmailTC, AWSIPAddressTC, AWSJSONTC, AWSPhoneTC, AWSTimeTC, AWSTimestampTC, AWSURLTC, type AppSyncResolverHandler, type BaseAppSyncContext, createApiTemplate, createAppSyncResolverHandler };
224
+ export { AWSDateTC, AWSDateTimeTC, AWSEmailTC, AWSIPAddressTC, AWSJSONTC, AWSPhoneTC, AWSTimeTC, AWSTimestampTC, AWSURLTC, type AppSyncInfo, type AppSyncResolverHandler, type BaseAppSyncContext, type CreateContext, createApiTemplate, createAppSyncMiddleware, createAppSyncResolverHandler };
package/dist/index.js CHANGED
@@ -41,10 +41,20 @@ __export(index_exports, {
41
41
  AWSTimestampTC: () => AWSTimestampTC,
42
42
  AWSURLTC: () => AWSURLTC,
43
43
  createApiTemplate: () => createApiTemplate,
44
+ createAppSyncMiddleware: () => createAppSyncMiddleware,
44
45
  createAppSyncResolverHandler: () => createAppSyncResolverHandler
45
46
  });
46
47
  module.exports = __toCommonJS(index_exports);
47
48
 
49
+ // src/appSyncMiddleware.ts
50
+ var createAppSyncMiddleware = /* @__PURE__ */__name(fn => {
51
+ return (resolve, source, args, context, info) => {
52
+ return Promise.resolve(fn((src, innerArgs, ctx, innerInfo) => {
53
+ return resolve(src, innerArgs, ctx, innerInfo);
54
+ }, source, args, context, info));
55
+ };
56
+ }, "createAppSyncMiddleware");
57
+
48
58
  // src/createApiTemplate.ts
49
59
  var import_graphql_api = require("@ttoss/graphql-api");
50
60
  var AppSyncGraphQLApiLogicalId = "AppSyncGraphQLApi";
@@ -456,5 +466,6 @@ var AWSIPAddressTC = import_graphql_api3.schemaComposer.createScalarTC(`scalar A
456
466
  AWSTimestampTC,
457
467
  AWSURLTC,
458
468
  createApiTemplate,
469
+ createAppSyncMiddleware,
459
470
  createAppSyncResolverHandler
460
471
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/appsync-api",
3
- "version": "0.23.12",
3
+ "version": "0.24.0",
4
4
  "description": "A library for building GraphQL APIs for AWS AppSync.",
5
5
  "license": "MIT",
6
6
  "author": "ttoss",
@@ -24,11 +24,11 @@
24
24
  ],
25
25
  "sideEffects": false,
26
26
  "dependencies": {
27
- "@ttoss/cloudformation": "^0.12.11"
27
+ "@ttoss/cloudformation": "^0.12.12"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "graphql": "^16.6.0",
31
- "@ttoss/graphql-api": "^0.9.10"
31
+ "@ttoss/graphql-api": "^0.9.11"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/aws-lambda": "^8.10.152",
@@ -36,9 +36,9 @@
36
36
  "graphql-shield": "^7.6.5",
37
37
  "jest": "^30.3.0",
38
38
  "tsup": "^8.5.1",
39
- "@ttoss/config": "^1.37.9",
40
- "@ttoss/graphql-api": "^0.9.10",
41
- "@ttoss/ids": "^0.4.9"
39
+ "@ttoss/config": "^1.37.10",
40
+ "@ttoss/graphql-api": "^0.9.11",
41
+ "@ttoss/ids": "^0.4.10"
42
42
  },
43
43
  "keywords": [
44
44
  "api",