@ttoss/appsync-api 0.18.4 → 0.18.5

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/README.md CHANGED
@@ -5,10 +5,10 @@ This package provides a opinionated way to create an AppSync API using [`@ttoss/
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- yarn add @ttoss/appsync-api @ttoss/graphql-api graphql
8
+ pnpm add @ttoss/appsync-api @ttoss/graphql-api graphql
9
9
  ```
10
10
 
11
- ## Quickstart
11
+ ## Getting Started
12
12
 
13
13
  You can create and deploy an AppSync API in four steps:
14
14
 
@@ -59,45 +59,15 @@ Now you can deploy your API using `carlin deploy`:
59
59
  carlin deploy
60
60
  ```
61
61
 
62
- ## Advanced Usage
62
+ ## API
63
63
 
64
- ### Middlewares
64
+ ### Resolvers Context
65
65
 
66
- You can add middlewares compatible with [`graphql-middleware`](https://github.com/dimatill/graphql-middleware) to the server using the `middlewares` option.
66
+ The `createAppSyncResolverHandler` function adds the `context` object to the resolvers. This object contains the following properties:
67
67
 
68
- ```ts
69
- import { createAppSyncResolverHandler } from '@ttoss/appsync-api';
70
- import { schemaComposer } from './schemaComposer';
71
- import { allow, deny, shield } from 'graphql-shield';
72
-
73
- const NotAuthorizedError = new Error('Not authorized!');
74
- /**
75
- * The error name is the same value `errorType` on GraphQL errors response.
76
- */
77
- NotAuthorizedError.name = 'NotAuthorizedError';
78
-
79
- const permissions = shield(
80
- {
81
- Query: {
82
- '*': deny,
83
- author: allow,
84
- },
85
- Author: {
86
- id: allow,
87
- name: allow,
88
- },
89
- },
90
- {
91
- fallbackRule: deny,
92
- fallbackError: NotAuthorizedError,
93
- }
94
- );
95
-
96
- export const handler = createAppSyncResolverHandler({
97
- schemaComposer,
98
- middlewares: [permissions],
99
- });
100
- ```
68
+ - `handler` - [AWS Lambda context object](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html).
69
+ - `request` - AppSync request object (see [Request section](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference-js.html)).
70
+ - `identity` - AppSync identity object (see [Identity section](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference-js.html)).
101
71
 
102
72
  ### Custom domain name
103
73
 
package/dist/esm/index.js CHANGED
@@ -266,17 +266,10 @@ var createApiTemplate = ({
266
266
 
267
267
  // src/createAppSyncResolverHandler.ts
268
268
  import { buildSchema } from "@ttoss/graphql-api";
269
-
270
- // ../auth-core/src/encodeDecode.ts
271
- var decode = encoded => {
272
- return JSON.parse(Buffer.from(encoded, "base64").toString("utf8"));
273
- };
274
-
275
- // src/createAppSyncResolverHandler.ts
276
269
  var createAppSyncResolverHandler = ({
277
270
  ...buildSchemaInput
278
271
  }) => {
279
- return async (event, context) => {
272
+ return async (event, appSyncHandlerContext) => {
280
273
  const {
281
274
  schemaComposer
282
275
  } = buildSchemaInput;
@@ -290,14 +283,11 @@ var createAppSyncResolverHandler = ({
290
283
  parentTypeName,
291
284
  fieldName
292
285
  } = info;
293
- const headers = request?.headers || {};
294
- const credentials = (() => {
295
- const headersCredentials = headers?.["x-credentials"];
296
- if (!headersCredentials) {
297
- return null;
298
- }
299
- return decode(headersCredentials);
300
- })();
286
+ const context = {
287
+ handler: appSyncHandlerContext,
288
+ request,
289
+ identity: event.identity
290
+ };
301
291
  const schema = buildSchema(buildSchemaInput);
302
292
  const parentType = schema.getType(parentTypeName);
303
293
  if (!parentType) {
@@ -333,12 +323,7 @@ var createAppSyncResolverHandler = ({
333
323
  ...enumArgs
334
324
  };
335
325
  })();
336
- const response = await resolver(source, argsWithEnumValues, {
337
- ...context,
338
- identity: event.identity,
339
- credentials,
340
- headers
341
- }, info);
326
+ const response = await resolver(source, argsWithEnumValues, context, info);
342
327
  if (response instanceof Error) {
343
328
  throw response;
344
329
  }
package/dist/index.js CHANGED
@@ -298,17 +298,10 @@ var createApiTemplate = ({
298
298
 
299
299
  // src/createAppSyncResolverHandler.ts
300
300
  var import_graphql_api2 = require("@ttoss/graphql-api");
301
-
302
- // ../auth-core/src/encodeDecode.ts
303
- var decode = encoded => {
304
- return JSON.parse(Buffer.from(encoded, "base64").toString("utf8"));
305
- };
306
-
307
- // src/createAppSyncResolverHandler.ts
308
301
  var createAppSyncResolverHandler = ({
309
302
  ...buildSchemaInput
310
303
  }) => {
311
- return async (event, context) => {
304
+ return async (event, appSyncHandlerContext) => {
312
305
  const {
313
306
  schemaComposer
314
307
  } = buildSchemaInput;
@@ -322,14 +315,11 @@ var createAppSyncResolverHandler = ({
322
315
  parentTypeName,
323
316
  fieldName
324
317
  } = info;
325
- const headers = request?.headers || {};
326
- const credentials = (() => {
327
- const headersCredentials = headers?.["x-credentials"];
328
- if (!headersCredentials) {
329
- return null;
330
- }
331
- return decode(headersCredentials);
332
- })();
318
+ const context = {
319
+ handler: appSyncHandlerContext,
320
+ request,
321
+ identity: event.identity
322
+ };
333
323
  const schema = (0, import_graphql_api2.buildSchema)(buildSchemaInput);
334
324
  const parentType = schema.getType(parentTypeName);
335
325
  if (!parentType) {
@@ -365,12 +355,7 @@ var createAppSyncResolverHandler = ({
365
355
  ...enumArgs
366
356
  };
367
357
  })();
368
- const response = await resolver(source, argsWithEnumValues, {
369
- ...context,
370
- identity: event.identity,
371
- credentials,
372
- headers
373
- }, info);
358
+ const response = await resolver(source, argsWithEnumValues, context, info);
374
359
  if (response instanceof Error) {
375
360
  throw response;
376
361
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/appsync-api",
3
- "version": "0.18.4",
3
+ "version": "0.18.5",
4
4
  "description": "A library for building GraphQL APIs for AWS AppSync.",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "peerDependencies": {
30
30
  "graphql": "^16.6.0",
31
- "@ttoss/graphql-api": "^0.5.1"
31
+ "@ttoss/graphql-api": "^0.5.2"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/aws-lambda": "^8.10.130",
@@ -36,9 +36,9 @@
36
36
  "graphql-shield": "^7.6.5",
37
37
  "jest": "^29.7.0",
38
38
  "tsup": "^8.0.2",
39
- "@ttoss/auth-core": "^0.0.3",
40
39
  "@ttoss/config": "^1.31.5",
41
- "@ttoss/graphql-api": "^0.5.1",
40
+ "@ttoss/graphql-api": "^0.5.2",
41
+ "@ttoss/ids": "^0.1.1",
42
42
  "carlin": "^1.31.12"
43
43
  },
44
44
  "keywords": [
@@ -1,7 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  import { BuildSchemaInput, buildSchema } from '@ttoss/graphql-api';
3
3
  import { type GraphQLObjectType } from 'graphql';
4
- import { decode } from '@ttoss/auth-core';
5
4
  import type { AppSyncResolverHandler as AwsAppSyncResolverHandler } from 'aws-lambda';
6
5
 
7
6
  export type AppSyncResolverHandler<
@@ -13,24 +12,21 @@ export type AppSyncResolverHandler<
13
12
  export const createAppSyncResolverHandler = ({
14
13
  ...buildSchemaInput
15
14
  }: BuildSchemaInput): AppSyncResolverHandler<any, any, any> => {
16
- return async (event, context) => {
15
+ return async (event, appSyncHandlerContext) => {
17
16
  const { schemaComposer } = buildSchemaInput;
18
17
 
18
+ /**
19
+ * https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference-js.html
20
+ */
19
21
  const { info, arguments: args, source, request } = event;
20
22
 
21
23
  const { parentTypeName, fieldName } = info;
22
24
 
23
- const headers = request?.headers || {};
24
-
25
- const credentials = (() => {
26
- const headersCredentials = headers?.['x-credentials'];
27
-
28
- if (!headersCredentials) {
29
- return null;
30
- }
31
-
32
- return decode(headersCredentials);
33
- })();
25
+ const context = {
26
+ handler: appSyncHandlerContext,
27
+ request,
28
+ identity: event.identity,
29
+ };
34
30
 
35
31
  const schema = buildSchema(buildSchemaInput);
36
32
 
@@ -86,7 +82,7 @@ export const createAppSyncResolverHandler = ({
86
82
  const response = await resolver(
87
83
  source,
88
84
  argsWithEnumValues,
89
- { ...context, identity: event.identity, credentials, headers },
85
+ context,
90
86
  info as any
91
87
  );
92
88