@ttoss/appsync-api 0.18.3 → 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 +8 -38
- package/dist/esm/index.js +7 -22
- package/dist/index.js +7 -22
- package/package.json +15 -11
- package/src/createAppSyncResolverHandler.ts +10 -14
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
|
-
|
|
8
|
+
pnpm add @ttoss/appsync-api @ttoss/graphql-api graphql
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
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
|
-
##
|
|
62
|
+
## API
|
|
63
63
|
|
|
64
|
-
###
|
|
64
|
+
### Resolvers Context
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
The `createAppSyncResolverHandler` function adds the `context` object to the resolvers. This object contains the following properties:
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
// ../relay-amplify/src/encodeCredentials.ts
|
|
271
|
-
var decodeCredentials = credentials => {
|
|
272
|
-
return JSON.parse(Buffer.from(credentials, "base64").toString("utf8"));
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
// src/createAppSyncResolverHandler.ts
|
|
276
269
|
var createAppSyncResolverHandler = ({
|
|
277
270
|
...buildSchemaInput
|
|
278
271
|
}) => {
|
|
279
|
-
return async (event,
|
|
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
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
}
|
|
299
|
-
return decodeCredentials(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
|
-
// ../relay-amplify/src/encodeCredentials.ts
|
|
303
|
-
var decodeCredentials = credentials => {
|
|
304
|
-
return JSON.parse(Buffer.from(credentials, "base64").toString("utf8"));
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
// src/createAppSyncResolverHandler.ts
|
|
308
301
|
var createAppSyncResolverHandler = ({
|
|
309
302
|
...buildSchemaInput
|
|
310
303
|
}) => {
|
|
311
|
-
return async (event,
|
|
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
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
}
|
|
331
|
-
return decodeCredentials(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.
|
|
3
|
+
"version": "0.18.5",
|
|
4
4
|
"description": "A library for building GraphQL APIs for AWS AppSync.",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
@@ -11,31 +11,35 @@
|
|
|
11
11
|
"url": "https://github.com/ttoss/ttoss.git",
|
|
12
12
|
"directory": "packages/appsync-api"
|
|
13
13
|
},
|
|
14
|
-
"
|
|
15
|
-
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": "./dist/esm/index.js",
|
|
17
|
+
"require": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
16
21
|
"files": [
|
|
17
22
|
"dist",
|
|
18
23
|
"src"
|
|
19
24
|
],
|
|
20
25
|
"sideEffects": false,
|
|
21
|
-
"typings": "dist/index.d.ts",
|
|
22
26
|
"dependencies": {
|
|
23
|
-
"@ttoss/cloudformation": "^0.9.
|
|
27
|
+
"@ttoss/cloudformation": "^0.9.1"
|
|
24
28
|
},
|
|
25
29
|
"peerDependencies": {
|
|
26
30
|
"graphql": "^16.6.0",
|
|
27
|
-
"@ttoss/graphql-api": "^0.5.
|
|
31
|
+
"@ttoss/graphql-api": "^0.5.2"
|
|
28
32
|
},
|
|
29
33
|
"devDependencies": {
|
|
30
34
|
"@types/aws-lambda": "^8.10.130",
|
|
31
35
|
"graphql": "^16.8.1",
|
|
32
36
|
"graphql-shield": "^7.6.5",
|
|
33
37
|
"jest": "^29.7.0",
|
|
34
|
-
"tsup": "^8.0.
|
|
35
|
-
"@ttoss/config": "^1.31.
|
|
36
|
-
"@ttoss/graphql-api": "^0.5.
|
|
37
|
-
"@ttoss/
|
|
38
|
-
"carlin": "^1.31.
|
|
38
|
+
"tsup": "^8.0.2",
|
|
39
|
+
"@ttoss/config": "^1.31.5",
|
|
40
|
+
"@ttoss/graphql-api": "^0.5.2",
|
|
41
|
+
"@ttoss/ids": "^0.1.1",
|
|
42
|
+
"carlin": "^1.31.12"
|
|
39
43
|
},
|
|
40
44
|
"keywords": [
|
|
41
45
|
"api",
|
|
@@ -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 { decodeCredentials } from '@ttoss/relay-amplify/src/encodeCredentials';
|
|
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,
|
|
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
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (!headersCredentials) {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return decodeCredentials(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
|
-
|
|
85
|
+
context,
|
|
90
86
|
info as any
|
|
91
87
|
);
|
|
92
88
|
|