@ttoss/appsync-api 0.12.0 → 0.13.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 +9 -190
- package/dist/index.d.ts +2 -20
- package/dist/index.js +10 -210
- package/package.json +3 -21
- package/src/createApiTemplate.ts +0 -12
- package/src/index.ts +0 -2
- package/bin/cli.js +0 -2
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -41
- package/dist/esm/cli.js +0 -15
- package/dist/esm/server.js +0 -38
- package/dist/server.d.ts +0 -8
- package/dist/server.js +0 -81
- package/src/cli.ts +0 -18
- package/src/composeWithRelay/composeWithRelay.ts +0 -67
- package/src/composeWithRelay/globalId.ts +0 -32
- package/src/composeWithRelay/index.ts +0 -7
- package/src/composeWithRelay/nodeFieldConfig.ts +0 -82
- package/src/composeWithRelay/nodeInterface.ts +0 -31
- package/src/server.ts +0 -47
package/dist/esm/index.js
CHANGED
|
@@ -80,26 +80,13 @@ var getPackageLambdaLayerStackName = packageName => {
|
|
|
80
80
|
// package.json
|
|
81
81
|
var package_default = {
|
|
82
82
|
name: "@ttoss/appsync-api",
|
|
83
|
-
version: "0.
|
|
83
|
+
version: "0.13.0",
|
|
84
84
|
description: "A library for building GraphQL APIs for AWS AppSync.",
|
|
85
85
|
license: "UNLICENSED",
|
|
86
86
|
author: "ttoss",
|
|
87
87
|
contributors: ["Pedro Arantes <pedro@arantespp.com> (https://arantespp.com)"],
|
|
88
|
-
exports: {
|
|
89
|
-
".": {
|
|
90
|
-
import: "./dist/esm/index.js",
|
|
91
|
-
require: "./dist/index.js"
|
|
92
|
-
},
|
|
93
|
-
"./server": {
|
|
94
|
-
import: "./dist/esm/server.js",
|
|
95
|
-
require: "./dist/server.js"
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
88
|
main: "dist/index.js",
|
|
99
89
|
module: "dist/esm/index.js",
|
|
100
|
-
bin: {
|
|
101
|
-
"ttoss-appsync-api": "./bin/cli.js"
|
|
102
|
-
},
|
|
103
90
|
files: ["dist", "src"],
|
|
104
91
|
scripts: {
|
|
105
92
|
build: "tsup",
|
|
@@ -109,10 +96,7 @@ var package_default = {
|
|
|
109
96
|
typings: "dist/index.d.ts",
|
|
110
97
|
dependencies: {
|
|
111
98
|
"@ttoss/cloudformation": "workspace:^",
|
|
112
|
-
|
|
113
|
-
"graphql-compose-connection": "^8.2.1",
|
|
114
|
-
"graphql-helix": "^1.13.0",
|
|
115
|
-
yargs: "^17.7.2"
|
|
99
|
+
"@ttoss/graphql-api": "workspace:^"
|
|
116
100
|
},
|
|
117
101
|
peerDependencies: {
|
|
118
102
|
graphql: "^16.6.0",
|
|
@@ -122,8 +106,6 @@ var package_default = {
|
|
|
122
106
|
"@ttoss/config": "workspace:^",
|
|
123
107
|
"@ttoss/relay-amplify": "workspace:^",
|
|
124
108
|
"@types/aws-lambda": "^8.10.115",
|
|
125
|
-
"@types/express": "^4.17.17",
|
|
126
|
-
"@types/yargs": "^17.0.24",
|
|
127
109
|
carlin: "workspace:^",
|
|
128
110
|
graphql: "^16.6.0",
|
|
129
111
|
"graphql-compose": "^9.0.10",
|
|
@@ -151,21 +133,18 @@ var AppSyncGraphQLApiKeyLogicalId = "AppSyncGraphQLApiKey";
|
|
|
151
133
|
var createApiTemplate = ({
|
|
152
134
|
additionalAuthenticationProviders,
|
|
153
135
|
authenticationType = "AMAZON_COGNITO_USER_POOLS",
|
|
154
|
-
schemaComposer
|
|
136
|
+
schemaComposer,
|
|
155
137
|
dataSource,
|
|
156
138
|
lambdaFunction,
|
|
157
139
|
userPoolConfig
|
|
158
140
|
}) => {
|
|
159
|
-
const sdlWithoutComments =
|
|
141
|
+
const sdlWithoutComments = schemaComposer.toSDL({
|
|
160
142
|
commentDescriptions: false,
|
|
161
143
|
omitDescriptions: true,
|
|
162
144
|
omitScalars: true
|
|
163
145
|
});
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
});
|
|
167
|
-
graphql.validateSchema(schemaComposer2.buildSchema());
|
|
168
|
-
const resolveMethods = schemaComposer2.getResolveMethods();
|
|
146
|
+
graphql.validateSchema(schemaComposer.buildSchema());
|
|
147
|
+
const resolveMethods = schemaComposer.getResolveMethods();
|
|
169
148
|
const resolveMethodsEntries = Object.entries(resolveMethods).flatMap(([typeName, fieldResolvers]) => {
|
|
170
149
|
return Object.entries(fieldResolvers).map(([fieldName, resolver]) => {
|
|
171
150
|
if (typeof resolver !== "function") {
|
|
@@ -195,14 +174,6 @@ var createApiTemplate = ({
|
|
|
195
174
|
};
|
|
196
175
|
const template = {
|
|
197
176
|
AWSTemplateFormatVersion: "2010-09-09",
|
|
198
|
-
/**
|
|
199
|
-
* This is a workaround to use with build script.
|
|
200
|
-
*/
|
|
201
|
-
Metadata: {
|
|
202
|
-
Schema: {
|
|
203
|
-
Definition: sdlWithComments
|
|
204
|
-
}
|
|
205
|
-
},
|
|
206
177
|
Parameters: {
|
|
207
178
|
Environment: {
|
|
208
179
|
Default: "Staging",
|
|
@@ -382,7 +353,7 @@ var decodeCredentials = credentials => {
|
|
|
382
353
|
|
|
383
354
|
// src/createAppSyncResolverHandler.ts
|
|
384
355
|
var createAppSyncResolverHandler = ({
|
|
385
|
-
schemaComposer
|
|
356
|
+
schemaComposer
|
|
386
357
|
}) => {
|
|
387
358
|
return async (event, context) => {
|
|
388
359
|
const {
|
|
@@ -403,7 +374,7 @@ var createAppSyncResolverHandler = ({
|
|
|
403
374
|
}
|
|
404
375
|
return decodeCredentials(headersCredentials);
|
|
405
376
|
})();
|
|
406
|
-
const resolveMethods =
|
|
377
|
+
const resolveMethods = schemaComposer.getResolveMethods();
|
|
407
378
|
const resolver = resolveMethods[parentTypeName][fieldName];
|
|
408
379
|
return resolver(source, args, {
|
|
409
380
|
...context,
|
|
@@ -413,156 +384,4 @@ var createAppSyncResolverHandler = ({
|
|
|
413
384
|
}, info);
|
|
414
385
|
};
|
|
415
386
|
};
|
|
416
|
-
|
|
417
|
-
// src/composeWithRelay/composeWithRelay.ts
|
|
418
|
-
import { ObjectTypeComposer } from "graphql-compose";
|
|
419
|
-
|
|
420
|
-
// src/composeWithRelay/nodeFieldConfig.ts
|
|
421
|
-
import { getProjectionFromAST } from "graphql-compose";
|
|
422
|
-
|
|
423
|
-
// src/composeWithRelay/globalId.ts
|
|
424
|
-
var base64 = i => {
|
|
425
|
-
return Buffer.from(i, "ascii").toString("base64");
|
|
426
|
-
};
|
|
427
|
-
var unbase64 = i => {
|
|
428
|
-
return Buffer.from(i, "base64").toString("ascii");
|
|
429
|
-
};
|
|
430
|
-
var toGlobalId = (type, id) => {
|
|
431
|
-
return base64([type, id].join(":"));
|
|
432
|
-
};
|
|
433
|
-
var fromGlobalId = globalId => {
|
|
434
|
-
const unbasedGlobalId = unbase64(globalId);
|
|
435
|
-
const [type, id] = unbasedGlobalId.split(":");
|
|
436
|
-
return {
|
|
437
|
-
type,
|
|
438
|
-
id
|
|
439
|
-
};
|
|
440
|
-
};
|
|
441
|
-
|
|
442
|
-
// src/composeWithRelay/nodeFieldConfig.ts
|
|
443
|
-
var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
|
|
444
|
-
return {
|
|
445
|
-
description: "Fetches an object that has globally unique ID among all types",
|
|
446
|
-
type: nodeInterface,
|
|
447
|
-
args: {
|
|
448
|
-
id: {
|
|
449
|
-
type: "ID!",
|
|
450
|
-
description: "The globally unique ID among all types"
|
|
451
|
-
}
|
|
452
|
-
},
|
|
453
|
-
resolve: (source, args, context, info) => {
|
|
454
|
-
if (!args.id || !(typeof args.id === "string")) {
|
|
455
|
-
return null;
|
|
456
|
-
}
|
|
457
|
-
const {
|
|
458
|
-
type
|
|
459
|
-
} = fromGlobalId(args.id);
|
|
460
|
-
if (!typeMapForRelayNode[type]) {
|
|
461
|
-
return null;
|
|
462
|
-
}
|
|
463
|
-
const {
|
|
464
|
-
tc,
|
|
465
|
-
resolver: findById
|
|
466
|
-
} = typeMapForRelayNode[type];
|
|
467
|
-
if (findById && findById.resolve && tc) {
|
|
468
|
-
const graphqlType = tc.getType();
|
|
469
|
-
let projection;
|
|
470
|
-
if (info) {
|
|
471
|
-
projection = getProjectionFromAST({
|
|
472
|
-
...info,
|
|
473
|
-
returnType: graphqlType
|
|
474
|
-
});
|
|
475
|
-
} else {
|
|
476
|
-
projection = {};
|
|
477
|
-
}
|
|
478
|
-
const idArgName = Object.keys(findById.args)[0];
|
|
479
|
-
return findById.resolve({
|
|
480
|
-
source,
|
|
481
|
-
args: {
|
|
482
|
-
[idArgName]: args.id
|
|
483
|
-
},
|
|
484
|
-
// eg. mongoose has _id fieldname, so should map
|
|
485
|
-
context,
|
|
486
|
-
info,
|
|
487
|
-
projection
|
|
488
|
-
}).then(res => {
|
|
489
|
-
if (!res) return res;
|
|
490
|
-
res.__nodeType = graphqlType;
|
|
491
|
-
return res;
|
|
492
|
-
});
|
|
493
|
-
}
|
|
494
|
-
return null;
|
|
495
|
-
}
|
|
496
|
-
};
|
|
497
|
-
};
|
|
498
|
-
|
|
499
|
-
// src/composeWithRelay/nodeInterface.ts
|
|
500
|
-
import { InterfaceTypeComposer } from "graphql-compose";
|
|
501
|
-
var NodeTC = InterfaceTypeComposer.createTemp({
|
|
502
|
-
name: "Node",
|
|
503
|
-
description: "An object, that can be fetched by the globally unique ID among all types.",
|
|
504
|
-
fields: {
|
|
505
|
-
id: {
|
|
506
|
-
type: "ID!",
|
|
507
|
-
description: "The globally unique ID among all types."
|
|
508
|
-
}
|
|
509
|
-
},
|
|
510
|
-
resolveType: payload => {
|
|
511
|
-
return payload.__nodeType.name ? payload.__nodeType.name : null;
|
|
512
|
-
}
|
|
513
|
-
});
|
|
514
|
-
var NodeInterface = NodeTC.getType();
|
|
515
|
-
var getNodeInterface = sc => {
|
|
516
|
-
if (sc.hasInstance("Node", InterfaceTypeComposer)) {
|
|
517
|
-
return sc.get("Node");
|
|
518
|
-
}
|
|
519
|
-
sc.set("Node", NodeTC);
|
|
520
|
-
return NodeTC;
|
|
521
|
-
};
|
|
522
|
-
|
|
523
|
-
// src/composeWithRelay/composeWithRelay.ts
|
|
524
|
-
var TypeMapForRelayNode = {};
|
|
525
|
-
var composeWithRelay = tc => {
|
|
526
|
-
if (!(tc instanceof ObjectTypeComposer)) {
|
|
527
|
-
throw new Error("You should provide ObjectTypeComposer instance to composeWithRelay method");
|
|
528
|
-
}
|
|
529
|
-
const nodeInterface = getNodeInterface(tc.schemaComposer);
|
|
530
|
-
const nodeFieldConfig = getNodeFieldConfig(TypeMapForRelayNode, nodeInterface);
|
|
531
|
-
if (tc.getTypeName() === "Query" || tc.getTypeName() === "RootQuery") {
|
|
532
|
-
tc.setField("node", nodeFieldConfig);
|
|
533
|
-
return tc;
|
|
534
|
-
}
|
|
535
|
-
if (tc.getTypeName() === "Mutation" || tc.getTypeName() === "RootMutation") {
|
|
536
|
-
return tc;
|
|
537
|
-
}
|
|
538
|
-
if (!tc.hasRecordIdFn()) {
|
|
539
|
-
throw new Error(`ObjectTypeComposer(${tc.getTypeName()}) should have recordIdFn. This function returns ID from provided object.`);
|
|
540
|
-
}
|
|
541
|
-
const findById = tc.getResolver("findById");
|
|
542
|
-
if (!findById) {
|
|
543
|
-
throw new Error(`ObjectTypeComposer(${tc.getTypeName()}) provided to composeWithRelay should have findById resolver.`);
|
|
544
|
-
}
|
|
545
|
-
TypeMapForRelayNode[tc.getTypeName()] = {
|
|
546
|
-
resolver: findById,
|
|
547
|
-
tc
|
|
548
|
-
};
|
|
549
|
-
tc.addFields({
|
|
550
|
-
id: {
|
|
551
|
-
type: "ID!",
|
|
552
|
-
description: "The globally unique ID among all types",
|
|
553
|
-
resolve: source => {
|
|
554
|
-
return toGlobalId(tc.getTypeName(), tc.getRecordId(source));
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
});
|
|
558
|
-
tc.addInterface(nodeInterface);
|
|
559
|
-
return tc;
|
|
560
|
-
};
|
|
561
|
-
|
|
562
|
-
// src/composeWithRelay/index.ts
|
|
563
|
-
import { schemaComposer } from "graphql-compose";
|
|
564
|
-
composeWithRelay(schemaComposer.Query);
|
|
565
|
-
|
|
566
|
-
// src/index.ts
|
|
567
|
-
import { default as default2 } from "graphql-compose-connection";
|
|
568
|
-
export { default2 as composeWithConnection, composeWithRelay, createApiTemplate, createAppSyncResolverHandler, fromGlobalId, toGlobalId };
|
|
387
|
+
export { createApiTemplate, createAppSyncResolverHandler };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { SchemaComposer
|
|
1
|
+
import { SchemaComposer } from 'graphql-compose';
|
|
2
2
|
import { AppSyncResolverHandler as AppSyncResolverHandler$1 } from 'aws-lambda';
|
|
3
3
|
export { AppSyncIdentityCognito } from 'aws-lambda';
|
|
4
|
-
export { default as composeWithConnection } from 'graphql-compose-connection';
|
|
5
4
|
|
|
6
5
|
type Parameter = {
|
|
7
6
|
AllowedValues?: string[];
|
|
@@ -114,21 +113,4 @@ declare const createAppSyncResolverHandler: ({ schemaComposer, }: {
|
|
|
114
113
|
schemaComposer: SchemaComposer<any>;
|
|
115
114
|
}) => AppSyncResolverHandler<any, any, any>;
|
|
116
115
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
type ResolvedGlobalId = {
|
|
120
|
-
type: string;
|
|
121
|
-
id: string;
|
|
122
|
-
};
|
|
123
|
-
/**
|
|
124
|
-
* Takes a type name and an ID specific to that type name, and returns a
|
|
125
|
-
* "global ID" that is unique among all types.
|
|
126
|
-
*/
|
|
127
|
-
declare const toGlobalId: (type: string, id: string | number) => string;
|
|
128
|
-
/**
|
|
129
|
-
* Takes the "global ID" created by toGlobalID, and returns the type name and ID
|
|
130
|
-
* used to create it.
|
|
131
|
-
*/
|
|
132
|
-
declare const fromGlobalId: (globalId: string) => ResolvedGlobalId;
|
|
133
|
-
|
|
134
|
-
export { AppSyncResolverHandler, composeWithRelay, createApiTemplate, createAppSyncResolverHandler, fromGlobalId, toGlobalId };
|
|
116
|
+
export { AppSyncResolverHandler, createApiTemplate, createAppSyncResolverHandler };
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
2
|
"use strict";
|
|
3
3
|
|
|
4
|
-
var __create = Object.create;
|
|
5
4
|
var __defProp = Object.defineProperty;
|
|
6
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
9
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
8
|
var __export = (target, all) => {
|
|
11
9
|
for (var name in all) __defProp(target, name, {
|
|
@@ -22,15 +20,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
22
20
|
}
|
|
23
21
|
return to;
|
|
24
22
|
};
|
|
25
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
30
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
31
|
-
value: mod,
|
|
32
|
-
enumerable: true
|
|
33
|
-
}) : target, mod));
|
|
34
23
|
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
35
24
|
value: true
|
|
36
25
|
}), mod);
|
|
@@ -38,12 +27,8 @@ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
|
38
27
|
// src/index.ts
|
|
39
28
|
var src_exports = {};
|
|
40
29
|
__export(src_exports, {
|
|
41
|
-
composeWithConnection: () => import_graphql_compose_connection.default,
|
|
42
|
-
composeWithRelay: () => composeWithRelay,
|
|
43
30
|
createApiTemplate: () => createApiTemplate,
|
|
44
|
-
createAppSyncResolverHandler: () => createAppSyncResolverHandler
|
|
45
|
-
fromGlobalId: () => fromGlobalId,
|
|
46
|
-
toGlobalId: () => toGlobalId
|
|
31
|
+
createAppSyncResolverHandler: () => createAppSyncResolverHandler
|
|
47
32
|
});
|
|
48
33
|
module.exports = __toCommonJS(src_exports);
|
|
49
34
|
|
|
@@ -127,26 +112,13 @@ var getPackageLambdaLayerStackName = packageName => {
|
|
|
127
112
|
// package.json
|
|
128
113
|
var package_default = {
|
|
129
114
|
name: "@ttoss/appsync-api",
|
|
130
|
-
version: "0.
|
|
115
|
+
version: "0.13.0",
|
|
131
116
|
description: "A library for building GraphQL APIs for AWS AppSync.",
|
|
132
117
|
license: "UNLICENSED",
|
|
133
118
|
author: "ttoss",
|
|
134
119
|
contributors: ["Pedro Arantes <pedro@arantespp.com> (https://arantespp.com)"],
|
|
135
|
-
exports: {
|
|
136
|
-
".": {
|
|
137
|
-
import: "./dist/esm/index.js",
|
|
138
|
-
require: "./dist/index.js"
|
|
139
|
-
},
|
|
140
|
-
"./server": {
|
|
141
|
-
import: "./dist/esm/server.js",
|
|
142
|
-
require: "./dist/server.js"
|
|
143
|
-
}
|
|
144
|
-
},
|
|
145
120
|
main: "dist/index.js",
|
|
146
121
|
module: "dist/esm/index.js",
|
|
147
|
-
bin: {
|
|
148
|
-
"ttoss-appsync-api": "./bin/cli.js"
|
|
149
|
-
},
|
|
150
122
|
files: ["dist", "src"],
|
|
151
123
|
scripts: {
|
|
152
124
|
build: "tsup",
|
|
@@ -156,10 +128,7 @@ var package_default = {
|
|
|
156
128
|
typings: "dist/index.d.ts",
|
|
157
129
|
dependencies: {
|
|
158
130
|
"@ttoss/cloudformation": "workspace:^",
|
|
159
|
-
|
|
160
|
-
"graphql-compose-connection": "^8.2.1",
|
|
161
|
-
"graphql-helix": "^1.13.0",
|
|
162
|
-
yargs: "^17.7.2"
|
|
131
|
+
"@ttoss/graphql-api": "workspace:^"
|
|
163
132
|
},
|
|
164
133
|
peerDependencies: {
|
|
165
134
|
graphql: "^16.6.0",
|
|
@@ -169,8 +138,6 @@ var package_default = {
|
|
|
169
138
|
"@ttoss/config": "workspace:^",
|
|
170
139
|
"@ttoss/relay-amplify": "workspace:^",
|
|
171
140
|
"@types/aws-lambda": "^8.10.115",
|
|
172
|
-
"@types/express": "^4.17.17",
|
|
173
|
-
"@types/yargs": "^17.0.24",
|
|
174
141
|
carlin: "workspace:^",
|
|
175
142
|
graphql: "^16.6.0",
|
|
176
143
|
"graphql-compose": "^9.0.10",
|
|
@@ -198,21 +165,18 @@ var AppSyncGraphQLApiKeyLogicalId = "AppSyncGraphQLApiKey";
|
|
|
198
165
|
var createApiTemplate = ({
|
|
199
166
|
additionalAuthenticationProviders,
|
|
200
167
|
authenticationType = "AMAZON_COGNITO_USER_POOLS",
|
|
201
|
-
schemaComposer
|
|
168
|
+
schemaComposer,
|
|
202
169
|
dataSource,
|
|
203
170
|
lambdaFunction,
|
|
204
171
|
userPoolConfig
|
|
205
172
|
}) => {
|
|
206
|
-
const sdlWithoutComments =
|
|
173
|
+
const sdlWithoutComments = schemaComposer.toSDL({
|
|
207
174
|
commentDescriptions: false,
|
|
208
175
|
omitDescriptions: true,
|
|
209
176
|
omitScalars: true
|
|
210
177
|
});
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
});
|
|
214
|
-
import_graphql_compose.graphql.validateSchema(schemaComposer2.buildSchema());
|
|
215
|
-
const resolveMethods = schemaComposer2.getResolveMethods();
|
|
178
|
+
import_graphql_compose.graphql.validateSchema(schemaComposer.buildSchema());
|
|
179
|
+
const resolveMethods = schemaComposer.getResolveMethods();
|
|
216
180
|
const resolveMethodsEntries = Object.entries(resolveMethods).flatMap(([typeName, fieldResolvers]) => {
|
|
217
181
|
return Object.entries(fieldResolvers).map(([fieldName, resolver]) => {
|
|
218
182
|
if (typeof resolver !== "function") {
|
|
@@ -242,14 +206,6 @@ var createApiTemplate = ({
|
|
|
242
206
|
};
|
|
243
207
|
const template = {
|
|
244
208
|
AWSTemplateFormatVersion: "2010-09-09",
|
|
245
|
-
/**
|
|
246
|
-
* This is a workaround to use with build script.
|
|
247
|
-
*/
|
|
248
|
-
Metadata: {
|
|
249
|
-
Schema: {
|
|
250
|
-
Definition: sdlWithComments
|
|
251
|
-
}
|
|
252
|
-
},
|
|
253
209
|
Parameters: {
|
|
254
210
|
Environment: {
|
|
255
211
|
Default: "Staging",
|
|
@@ -429,7 +385,7 @@ var decodeCredentials = credentials => {
|
|
|
429
385
|
|
|
430
386
|
// src/createAppSyncResolverHandler.ts
|
|
431
387
|
var createAppSyncResolverHandler = ({
|
|
432
|
-
schemaComposer
|
|
388
|
+
schemaComposer
|
|
433
389
|
}) => {
|
|
434
390
|
return async (event, context) => {
|
|
435
391
|
const {
|
|
@@ -450,7 +406,7 @@ var createAppSyncResolverHandler = ({
|
|
|
450
406
|
}
|
|
451
407
|
return decodeCredentials(headersCredentials);
|
|
452
408
|
})();
|
|
453
|
-
const resolveMethods =
|
|
409
|
+
const resolveMethods = schemaComposer.getResolveMethods();
|
|
454
410
|
const resolver = resolveMethods[parentTypeName][fieldName];
|
|
455
411
|
return resolver(source, args, {
|
|
456
412
|
...context,
|
|
@@ -460,164 +416,8 @@ var createAppSyncResolverHandler = ({
|
|
|
460
416
|
}, info);
|
|
461
417
|
};
|
|
462
418
|
};
|
|
463
|
-
|
|
464
|
-
// src/composeWithRelay/composeWithRelay.ts
|
|
465
|
-
var import_graphql_compose4 = require("graphql-compose");
|
|
466
|
-
|
|
467
|
-
// src/composeWithRelay/nodeFieldConfig.ts
|
|
468
|
-
var import_graphql_compose2 = require("graphql-compose");
|
|
469
|
-
|
|
470
|
-
// src/composeWithRelay/globalId.ts
|
|
471
|
-
var base64 = i => {
|
|
472
|
-
return Buffer.from(i, "ascii").toString("base64");
|
|
473
|
-
};
|
|
474
|
-
var unbase64 = i => {
|
|
475
|
-
return Buffer.from(i, "base64").toString("ascii");
|
|
476
|
-
};
|
|
477
|
-
var toGlobalId = (type, id) => {
|
|
478
|
-
return base64([type, id].join(":"));
|
|
479
|
-
};
|
|
480
|
-
var fromGlobalId = globalId => {
|
|
481
|
-
const unbasedGlobalId = unbase64(globalId);
|
|
482
|
-
const [type, id] = unbasedGlobalId.split(":");
|
|
483
|
-
return {
|
|
484
|
-
type,
|
|
485
|
-
id
|
|
486
|
-
};
|
|
487
|
-
};
|
|
488
|
-
|
|
489
|
-
// src/composeWithRelay/nodeFieldConfig.ts
|
|
490
|
-
var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
|
|
491
|
-
return {
|
|
492
|
-
description: "Fetches an object that has globally unique ID among all types",
|
|
493
|
-
type: nodeInterface,
|
|
494
|
-
args: {
|
|
495
|
-
id: {
|
|
496
|
-
type: "ID!",
|
|
497
|
-
description: "The globally unique ID among all types"
|
|
498
|
-
}
|
|
499
|
-
},
|
|
500
|
-
resolve: (source, args, context, info) => {
|
|
501
|
-
if (!args.id || !(typeof args.id === "string")) {
|
|
502
|
-
return null;
|
|
503
|
-
}
|
|
504
|
-
const {
|
|
505
|
-
type
|
|
506
|
-
} = fromGlobalId(args.id);
|
|
507
|
-
if (!typeMapForRelayNode[type]) {
|
|
508
|
-
return null;
|
|
509
|
-
}
|
|
510
|
-
const {
|
|
511
|
-
tc,
|
|
512
|
-
resolver: findById
|
|
513
|
-
} = typeMapForRelayNode[type];
|
|
514
|
-
if (findById && findById.resolve && tc) {
|
|
515
|
-
const graphqlType = tc.getType();
|
|
516
|
-
let projection;
|
|
517
|
-
if (info) {
|
|
518
|
-
projection = (0, import_graphql_compose2.getProjectionFromAST)({
|
|
519
|
-
...info,
|
|
520
|
-
returnType: graphqlType
|
|
521
|
-
});
|
|
522
|
-
} else {
|
|
523
|
-
projection = {};
|
|
524
|
-
}
|
|
525
|
-
const idArgName = Object.keys(findById.args)[0];
|
|
526
|
-
return findById.resolve({
|
|
527
|
-
source,
|
|
528
|
-
args: {
|
|
529
|
-
[idArgName]: args.id
|
|
530
|
-
},
|
|
531
|
-
// eg. mongoose has _id fieldname, so should map
|
|
532
|
-
context,
|
|
533
|
-
info,
|
|
534
|
-
projection
|
|
535
|
-
}).then(res => {
|
|
536
|
-
if (!res) return res;
|
|
537
|
-
res.__nodeType = graphqlType;
|
|
538
|
-
return res;
|
|
539
|
-
});
|
|
540
|
-
}
|
|
541
|
-
return null;
|
|
542
|
-
}
|
|
543
|
-
};
|
|
544
|
-
};
|
|
545
|
-
|
|
546
|
-
// src/composeWithRelay/nodeInterface.ts
|
|
547
|
-
var import_graphql_compose3 = require("graphql-compose");
|
|
548
|
-
var NodeTC = import_graphql_compose3.InterfaceTypeComposer.createTemp({
|
|
549
|
-
name: "Node",
|
|
550
|
-
description: "An object, that can be fetched by the globally unique ID among all types.",
|
|
551
|
-
fields: {
|
|
552
|
-
id: {
|
|
553
|
-
type: "ID!",
|
|
554
|
-
description: "The globally unique ID among all types."
|
|
555
|
-
}
|
|
556
|
-
},
|
|
557
|
-
resolveType: payload => {
|
|
558
|
-
return payload.__nodeType.name ? payload.__nodeType.name : null;
|
|
559
|
-
}
|
|
560
|
-
});
|
|
561
|
-
var NodeInterface = NodeTC.getType();
|
|
562
|
-
var getNodeInterface = sc => {
|
|
563
|
-
if (sc.hasInstance("Node", import_graphql_compose3.InterfaceTypeComposer)) {
|
|
564
|
-
return sc.get("Node");
|
|
565
|
-
}
|
|
566
|
-
sc.set("Node", NodeTC);
|
|
567
|
-
return NodeTC;
|
|
568
|
-
};
|
|
569
|
-
|
|
570
|
-
// src/composeWithRelay/composeWithRelay.ts
|
|
571
|
-
var TypeMapForRelayNode = {};
|
|
572
|
-
var composeWithRelay = tc => {
|
|
573
|
-
if (!(tc instanceof import_graphql_compose4.ObjectTypeComposer)) {
|
|
574
|
-
throw new Error("You should provide ObjectTypeComposer instance to composeWithRelay method");
|
|
575
|
-
}
|
|
576
|
-
const nodeInterface = getNodeInterface(tc.schemaComposer);
|
|
577
|
-
const nodeFieldConfig = getNodeFieldConfig(TypeMapForRelayNode, nodeInterface);
|
|
578
|
-
if (tc.getTypeName() === "Query" || tc.getTypeName() === "RootQuery") {
|
|
579
|
-
tc.setField("node", nodeFieldConfig);
|
|
580
|
-
return tc;
|
|
581
|
-
}
|
|
582
|
-
if (tc.getTypeName() === "Mutation" || tc.getTypeName() === "RootMutation") {
|
|
583
|
-
return tc;
|
|
584
|
-
}
|
|
585
|
-
if (!tc.hasRecordIdFn()) {
|
|
586
|
-
throw new Error(`ObjectTypeComposer(${tc.getTypeName()}) should have recordIdFn. This function returns ID from provided object.`);
|
|
587
|
-
}
|
|
588
|
-
const findById = tc.getResolver("findById");
|
|
589
|
-
if (!findById) {
|
|
590
|
-
throw new Error(`ObjectTypeComposer(${tc.getTypeName()}) provided to composeWithRelay should have findById resolver.`);
|
|
591
|
-
}
|
|
592
|
-
TypeMapForRelayNode[tc.getTypeName()] = {
|
|
593
|
-
resolver: findById,
|
|
594
|
-
tc
|
|
595
|
-
};
|
|
596
|
-
tc.addFields({
|
|
597
|
-
id: {
|
|
598
|
-
type: "ID!",
|
|
599
|
-
description: "The globally unique ID among all types",
|
|
600
|
-
resolve: source => {
|
|
601
|
-
return toGlobalId(tc.getTypeName(), tc.getRecordId(source));
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
});
|
|
605
|
-
tc.addInterface(nodeInterface);
|
|
606
|
-
return tc;
|
|
607
|
-
};
|
|
608
|
-
|
|
609
|
-
// src/composeWithRelay/index.ts
|
|
610
|
-
var import_graphql_compose5 = require("graphql-compose");
|
|
611
|
-
composeWithRelay(import_graphql_compose5.schemaComposer.Query);
|
|
612
|
-
|
|
613
|
-
// src/index.ts
|
|
614
|
-
var import_graphql_compose_connection = __toESM(require("graphql-compose-connection"));
|
|
615
419
|
// Annotate the CommonJS export names for ESM import in node:
|
|
616
420
|
0 && (module.exports = {
|
|
617
|
-
composeWithConnection,
|
|
618
|
-
composeWithRelay,
|
|
619
421
|
createApiTemplate,
|
|
620
|
-
createAppSyncResolverHandler
|
|
621
|
-
fromGlobalId,
|
|
622
|
-
toGlobalId
|
|
422
|
+
createAppSyncResolverHandler
|
|
623
423
|
});
|
package/package.json
CHANGED
|
@@ -1,27 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/appsync-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "A library for building GraphQL APIs for AWS AppSync.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"author": "ttoss",
|
|
7
7
|
"contributors": [
|
|
8
8
|
"Pedro Arantes <pedro@arantespp.com> (https://arantespp.com)"
|
|
9
9
|
],
|
|
10
|
-
"exports": {
|
|
11
|
-
".": {
|
|
12
|
-
"import": "./dist/esm/index.js",
|
|
13
|
-
"require": "./dist/index.js"
|
|
14
|
-
},
|
|
15
|
-
"./server": {
|
|
16
|
-
"import": "./dist/esm/server.js",
|
|
17
|
-
"require": "./dist/server.js"
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
10
|
"main": "dist/index.js",
|
|
21
11
|
"module": "dist/esm/index.js",
|
|
22
|
-
"bin": {
|
|
23
|
-
"ttoss-appsync-api": "./bin/cli.js"
|
|
24
|
-
},
|
|
25
12
|
"files": [
|
|
26
13
|
"dist",
|
|
27
14
|
"src"
|
|
@@ -29,11 +16,8 @@
|
|
|
29
16
|
"sideEffects": false,
|
|
30
17
|
"typings": "dist/index.d.ts",
|
|
31
18
|
"dependencies": {
|
|
32
|
-
"
|
|
33
|
-
"graphql-
|
|
34
|
-
"graphql-helix": "^1.13.0",
|
|
35
|
-
"yargs": "^17.7.2",
|
|
36
|
-
"@ttoss/cloudformation": "^0.7.1"
|
|
19
|
+
"@ttoss/cloudformation": "^0.7.1",
|
|
20
|
+
"@ttoss/graphql-api": "^0.1.0"
|
|
37
21
|
},
|
|
38
22
|
"peerDependencies": {
|
|
39
23
|
"graphql": "^16.6.0",
|
|
@@ -41,8 +25,6 @@
|
|
|
41
25
|
},
|
|
42
26
|
"devDependencies": {
|
|
43
27
|
"@types/aws-lambda": "^8.10.115",
|
|
44
|
-
"@types/express": "^4.17.17",
|
|
45
|
-
"@types/yargs": "^17.0.24",
|
|
46
28
|
"graphql": "^16.6.0",
|
|
47
29
|
"graphql-compose": "^9.0.10",
|
|
48
30
|
"jest": "^29.5.0",
|
package/src/createApiTemplate.ts
CHANGED
|
@@ -74,10 +74,6 @@ export const createApiTemplate = ({
|
|
|
74
74
|
omitScalars: true,
|
|
75
75
|
});
|
|
76
76
|
|
|
77
|
-
const sdlWithComments = schemaComposer.toSDL({
|
|
78
|
-
omitScalars: true,
|
|
79
|
-
});
|
|
80
|
-
|
|
81
77
|
graphql.validateSchema(schemaComposer.buildSchema());
|
|
82
78
|
|
|
83
79
|
/**
|
|
@@ -125,14 +121,6 @@ export const createApiTemplate = ({
|
|
|
125
121
|
|
|
126
122
|
const template: CloudFormationTemplate = {
|
|
127
123
|
AWSTemplateFormatVersion: '2010-09-09',
|
|
128
|
-
/**
|
|
129
|
-
* This is a workaround to use with build script.
|
|
130
|
-
*/
|
|
131
|
-
Metadata: {
|
|
132
|
-
Schema: {
|
|
133
|
-
Definition: sdlWithComments,
|
|
134
|
-
},
|
|
135
|
-
},
|
|
136
124
|
Parameters: {
|
|
137
125
|
Environment: {
|
|
138
126
|
Default: 'Staging',
|
package/src/index.ts
CHANGED
|
@@ -4,5 +4,3 @@ export {
|
|
|
4
4
|
createAppSyncResolverHandler,
|
|
5
5
|
} from './createAppSyncResolverHandler';
|
|
6
6
|
export type { AppSyncIdentityCognito } from 'aws-lambda';
|
|
7
|
-
export { composeWithRelay, toGlobalId, fromGlobalId } from './composeWithRelay';
|
|
8
|
-
export { default as composeWithConnection } from 'graphql-compose-connection';
|
package/bin/cli.js
DELETED
package/dist/cli.d.ts
DELETED
package/dist/cli.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
-
"use strict";
|
|
3
|
-
|
|
4
|
-
var __create = Object.create;
|
|
5
|
-
var __defProp = Object.defineProperty;
|
|
6
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
-
get: () => from[key],
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
25
|
-
value: mod,
|
|
26
|
-
enumerable: true
|
|
27
|
-
}) : target, mod));
|
|
28
|
-
|
|
29
|
-
// src/cli.ts
|
|
30
|
-
var fs = __toESM(require("fs"));
|
|
31
|
-
var import_cloudformation = require("@ttoss/cloudformation");
|
|
32
|
-
var import_yargs = __toESM(require("yargs"));
|
|
33
|
-
var argv = (0, import_yargs.default)(process.argv.slice(2)).argv;
|
|
34
|
-
if (argv._.includes("build-schema")) {
|
|
35
|
-
const template = (0, import_cloudformation.findAndReadCloudFormationTemplate)({});
|
|
36
|
-
const sdl = template.Metadata.Schema.Definition;
|
|
37
|
-
fs.mkdirSync("schema", {
|
|
38
|
-
recursive: true
|
|
39
|
-
});
|
|
40
|
-
fs.writeFileSync("schema/schema.graphql", sdl);
|
|
41
|
-
}
|
package/dist/esm/cli.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
-
|
|
3
|
-
// src/cli.ts
|
|
4
|
-
import * as fs from "fs";
|
|
5
|
-
import { findAndReadCloudFormationTemplate } from "@ttoss/cloudformation";
|
|
6
|
-
import yargs from "yargs";
|
|
7
|
-
var argv = yargs(process.argv.slice(2)).argv;
|
|
8
|
-
if (argv._.includes("build-schema")) {
|
|
9
|
-
const template = findAndReadCloudFormationTemplate({});
|
|
10
|
-
const sdl = template.Metadata.Schema.Definition;
|
|
11
|
-
fs.mkdirSync("schema", {
|
|
12
|
-
recursive: true
|
|
13
|
-
});
|
|
14
|
-
fs.writeFileSync("schema/schema.graphql", sdl);
|
|
15
|
-
}
|
package/dist/esm/server.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
-
|
|
3
|
-
// src/server.ts
|
|
4
|
-
import { getGraphQLParameters, processRequest, renderGraphiQL, sendResult, shouldRenderGraphiQL } from "graphql-helix";
|
|
5
|
-
import express from "express";
|
|
6
|
-
var createServer = ({
|
|
7
|
-
schemaComposer
|
|
8
|
-
}) => {
|
|
9
|
-
const server = express();
|
|
10
|
-
server.use(express.json());
|
|
11
|
-
server.use("/graphql", async (req, res) => {
|
|
12
|
-
const request = {
|
|
13
|
-
body: req.body,
|
|
14
|
-
headers: req.headers,
|
|
15
|
-
method: req.method,
|
|
16
|
-
query: req.query
|
|
17
|
-
};
|
|
18
|
-
if (shouldRenderGraphiQL(request)) {
|
|
19
|
-
res.send(renderGraphiQL());
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
const {
|
|
23
|
-
operationName,
|
|
24
|
-
query,
|
|
25
|
-
variables
|
|
26
|
-
} = getGraphQLParameters(request);
|
|
27
|
-
const result = await processRequest({
|
|
28
|
-
operationName,
|
|
29
|
-
query,
|
|
30
|
-
variables,
|
|
31
|
-
request,
|
|
32
|
-
schema: schemaComposer.buildSchema()
|
|
33
|
-
});
|
|
34
|
-
sendResult(result, res);
|
|
35
|
-
});
|
|
36
|
-
return server;
|
|
37
|
-
};
|
|
38
|
-
export { createServer };
|
package/dist/server.d.ts
DELETED
package/dist/server.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
-
"use strict";
|
|
3
|
-
|
|
4
|
-
var __create = Object.create;
|
|
5
|
-
var __defProp = Object.defineProperty;
|
|
6
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __export = (target, all) => {
|
|
11
|
-
for (var name in all) __defProp(target, name, {
|
|
12
|
-
get: all[name],
|
|
13
|
-
enumerable: true
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
var __copyProps = (to, from, except, desc) => {
|
|
17
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
-
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
19
|
-
get: () => from[key],
|
|
20
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
return to;
|
|
24
|
-
};
|
|
25
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
30
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
31
|
-
value: mod,
|
|
32
|
-
enumerable: true
|
|
33
|
-
}) : target, mod));
|
|
34
|
-
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
35
|
-
value: true
|
|
36
|
-
}), mod);
|
|
37
|
-
|
|
38
|
-
// src/server.ts
|
|
39
|
-
var server_exports = {};
|
|
40
|
-
__export(server_exports, {
|
|
41
|
-
createServer: () => createServer
|
|
42
|
-
});
|
|
43
|
-
module.exports = __toCommonJS(server_exports);
|
|
44
|
-
var import_graphql_helix = require("graphql-helix");
|
|
45
|
-
var import_express = __toESM(require("express"));
|
|
46
|
-
var createServer = ({
|
|
47
|
-
schemaComposer
|
|
48
|
-
}) => {
|
|
49
|
-
const server = (0, import_express.default)();
|
|
50
|
-
server.use(import_express.default.json());
|
|
51
|
-
server.use("/graphql", async (req, res) => {
|
|
52
|
-
const request = {
|
|
53
|
-
body: req.body,
|
|
54
|
-
headers: req.headers,
|
|
55
|
-
method: req.method,
|
|
56
|
-
query: req.query
|
|
57
|
-
};
|
|
58
|
-
if ((0, import_graphql_helix.shouldRenderGraphiQL)(request)) {
|
|
59
|
-
res.send((0, import_graphql_helix.renderGraphiQL)());
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
const {
|
|
63
|
-
operationName,
|
|
64
|
-
query,
|
|
65
|
-
variables
|
|
66
|
-
} = (0, import_graphql_helix.getGraphQLParameters)(request);
|
|
67
|
-
const result = await (0, import_graphql_helix.processRequest)({
|
|
68
|
-
operationName,
|
|
69
|
-
query,
|
|
70
|
-
variables,
|
|
71
|
-
request,
|
|
72
|
-
schema: schemaComposer.buildSchema()
|
|
73
|
-
});
|
|
74
|
-
(0, import_graphql_helix.sendResult)(result, res);
|
|
75
|
-
});
|
|
76
|
-
return server;
|
|
77
|
-
};
|
|
78
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
79
|
-
0 && (module.exports = {
|
|
80
|
-
createServer
|
|
81
|
-
});
|
package/src/cli.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import { findAndReadCloudFormationTemplate } from '@ttoss/cloudformation';
|
|
3
|
-
import yargs from 'yargs';
|
|
4
|
-
|
|
5
|
-
const argv: any = yargs(process.argv.slice(2)).argv;
|
|
6
|
-
|
|
7
|
-
if (argv._.includes('build-schema')) {
|
|
8
|
-
const template = findAndReadCloudFormationTemplate({});
|
|
9
|
-
|
|
10
|
-
const sdl = template.Metadata.Schema.Definition;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Save to schema/schema.graphql. schema folder might not exist.
|
|
14
|
-
*/
|
|
15
|
-
fs.mkdirSync('schema', { recursive: true });
|
|
16
|
-
|
|
17
|
-
fs.writeFileSync('schema/schema.graphql', sdl);
|
|
18
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { ObjectTypeComposer } from 'graphql-compose';
|
|
2
|
-
import { getNodeFieldConfig } from './nodeFieldConfig';
|
|
3
|
-
import { getNodeInterface } from './nodeInterface';
|
|
4
|
-
import { toGlobalId } from './globalId';
|
|
5
|
-
|
|
6
|
-
// all wrapped typeComposers with Relay, stored in this variable
|
|
7
|
-
// for futher type resolving via NodeInterface.resolveType method
|
|
8
|
-
export const TypeMapForRelayNode: any = {};
|
|
9
|
-
|
|
10
|
-
export const composeWithRelay = <TContext>(
|
|
11
|
-
tc: ObjectTypeComposer<any, TContext>
|
|
12
|
-
): ObjectTypeComposer<any, TContext> => {
|
|
13
|
-
if (!(tc instanceof ObjectTypeComposer)) {
|
|
14
|
-
throw new Error(
|
|
15
|
-
'You should provide ObjectTypeComposer instance to composeWithRelay method'
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const nodeInterface = getNodeInterface(tc.schemaComposer);
|
|
20
|
-
const nodeFieldConfig = getNodeFieldConfig(
|
|
21
|
-
TypeMapForRelayNode,
|
|
22
|
-
nodeInterface
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
if (tc.getTypeName() === 'Query' || tc.getTypeName() === 'RootQuery') {
|
|
26
|
-
tc.setField('node', nodeFieldConfig);
|
|
27
|
-
return tc;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (tc.getTypeName() === 'Mutation' || tc.getTypeName() === 'RootMutation') {
|
|
31
|
-
// just skip
|
|
32
|
-
return tc;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (!tc.hasRecordIdFn()) {
|
|
36
|
-
throw new Error(
|
|
37
|
-
`ObjectTypeComposer(${tc.getTypeName()}) should have recordIdFn. ` +
|
|
38
|
-
'This function returns ID from provided object.'
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const findById = tc.getResolver('findById');
|
|
43
|
-
if (!findById) {
|
|
44
|
-
throw new Error(
|
|
45
|
-
`ObjectTypeComposer(${tc.getTypeName()}) provided to composeWithRelay ` +
|
|
46
|
-
'should have findById resolver.'
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
TypeMapForRelayNode[tc.getTypeName()] = {
|
|
50
|
-
resolver: findById,
|
|
51
|
-
tc,
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
tc.addFields({
|
|
55
|
-
id: {
|
|
56
|
-
type: 'ID!',
|
|
57
|
-
description: 'The globally unique ID among all types',
|
|
58
|
-
resolve: (source) => {
|
|
59
|
-
return toGlobalId(tc.getTypeName(), tc.getRecordId(source));
|
|
60
|
-
},
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
tc.addInterface(nodeInterface);
|
|
65
|
-
|
|
66
|
-
return tc;
|
|
67
|
-
};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export type Base64String = string;
|
|
2
|
-
|
|
3
|
-
export type ResolvedGlobalId = {
|
|
4
|
-
type: string;
|
|
5
|
-
id: string;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export const base64 = (i: string): Base64String => {
|
|
9
|
-
return Buffer.from(i, 'ascii').toString('base64');
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export const unbase64 = (i: Base64String): string => {
|
|
13
|
-
return Buffer.from(i, 'base64').toString('ascii');
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Takes a type name and an ID specific to that type name, and returns a
|
|
18
|
-
* "global ID" that is unique among all types.
|
|
19
|
-
*/
|
|
20
|
-
export const toGlobalId = (type: string, id: string | number): string => {
|
|
21
|
-
return base64([type, id].join(':'));
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Takes the "global ID" created by toGlobalID, and returns the type name and ID
|
|
26
|
-
* used to create it.
|
|
27
|
-
*/
|
|
28
|
-
export const fromGlobalId = (globalId: string): ResolvedGlobalId => {
|
|
29
|
-
const unbasedGlobalId = unbase64(globalId);
|
|
30
|
-
const [type, id] = unbasedGlobalId.split(':');
|
|
31
|
-
return { type, id };
|
|
32
|
-
};
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type InterfaceTypeComposer,
|
|
3
|
-
type ObjectTypeComposerFieldConfigDefinition,
|
|
4
|
-
getProjectionFromAST,
|
|
5
|
-
} from 'graphql-compose';
|
|
6
|
-
import { fromGlobalId } from './globalId';
|
|
7
|
-
import type { GraphQLResolveInfo } from 'graphql-compose/lib/graphql';
|
|
8
|
-
import type { ObjectTypeComposer, Resolver } from 'graphql-compose';
|
|
9
|
-
|
|
10
|
-
export type TypeMapForRelayNode<TSource, TContext> = {
|
|
11
|
-
[typeName: string]: {
|
|
12
|
-
resolver: Resolver<TSource, TContext>;
|
|
13
|
-
tc: ObjectTypeComposer<TSource, TContext>;
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
// this fieldConfig must be set to RootQuery.node field
|
|
18
|
-
export const getNodeFieldConfig = (
|
|
19
|
-
typeMapForRelayNode: TypeMapForRelayNode<any, any>,
|
|
20
|
-
nodeInterface: InterfaceTypeComposer<any, any>
|
|
21
|
-
): ObjectTypeComposerFieldConfigDefinition<any, any> => {
|
|
22
|
-
return {
|
|
23
|
-
description:
|
|
24
|
-
'Fetches an object that has globally unique ID among all types',
|
|
25
|
-
type: nodeInterface,
|
|
26
|
-
args: {
|
|
27
|
-
id: {
|
|
28
|
-
type: 'ID!',
|
|
29
|
-
description: 'The globally unique ID among all types',
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
resolve: (
|
|
33
|
-
source: any,
|
|
34
|
-
args: { [argName: string]: any },
|
|
35
|
-
context: any,
|
|
36
|
-
info: GraphQLResolveInfo
|
|
37
|
-
) => {
|
|
38
|
-
if (!args.id || !(typeof args.id === 'string')) {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
const { type } = fromGlobalId(args.id);
|
|
42
|
-
|
|
43
|
-
if (!typeMapForRelayNode[type]) {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
const { tc, resolver: findById } = typeMapForRelayNode[type];
|
|
47
|
-
if (findById && findById.resolve && tc) {
|
|
48
|
-
const graphqlType = tc.getType();
|
|
49
|
-
|
|
50
|
-
// set `returnType` for proper work of `getProjectionFromAST`
|
|
51
|
-
// it will correctly add required fields for `relation` to `projection`
|
|
52
|
-
let projection;
|
|
53
|
-
if (info) {
|
|
54
|
-
projection = getProjectionFromAST({
|
|
55
|
-
...info,
|
|
56
|
-
returnType: graphqlType,
|
|
57
|
-
});
|
|
58
|
-
} else {
|
|
59
|
-
projection = {};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// suppose that first argument is argument with id field
|
|
63
|
-
const idArgName = Object.keys(findById.args)[0];
|
|
64
|
-
return findById
|
|
65
|
-
.resolve({
|
|
66
|
-
source,
|
|
67
|
-
args: { [idArgName]: args.id }, // eg. mongoose has _id fieldname, so should map
|
|
68
|
-
context,
|
|
69
|
-
info,
|
|
70
|
-
projection,
|
|
71
|
-
})
|
|
72
|
-
.then((res: any) => {
|
|
73
|
-
if (!res) return res;
|
|
74
|
-
res.__nodeType = graphqlType;
|
|
75
|
-
return res;
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return null;
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { InterfaceTypeComposer, type SchemaComposer } from 'graphql-compose';
|
|
2
|
-
|
|
3
|
-
const NodeTC = InterfaceTypeComposer.createTemp({
|
|
4
|
-
name: 'Node',
|
|
5
|
-
description:
|
|
6
|
-
'An object, that can be fetched by the globally unique ID among all types.',
|
|
7
|
-
fields: {
|
|
8
|
-
id: {
|
|
9
|
-
type: 'ID!',
|
|
10
|
-
description: 'The globally unique ID among all types.',
|
|
11
|
-
},
|
|
12
|
-
},
|
|
13
|
-
resolveType: (payload: any) => {
|
|
14
|
-
// `payload.__nodeType` was added to payload via nodeFieldConfig.resolve
|
|
15
|
-
return payload.__nodeType.name ? payload.__nodeType.name : null;
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
export const NodeInterface = NodeTC.getType();
|
|
20
|
-
|
|
21
|
-
export const getNodeInterface = <TContext>(
|
|
22
|
-
sc: SchemaComposer<TContext>
|
|
23
|
-
): InterfaceTypeComposer<any, TContext> => {
|
|
24
|
-
if (sc.hasInstance('Node', InterfaceTypeComposer)) {
|
|
25
|
-
return sc.get('Node') as any;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
sc.set('Node', NodeTC);
|
|
29
|
-
|
|
30
|
-
return NodeTC;
|
|
31
|
-
};
|
package/src/server.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { type SchemaComposer } from 'graphql-compose';
|
|
2
|
-
import {
|
|
3
|
-
getGraphQLParameters,
|
|
4
|
-
processRequest,
|
|
5
|
-
renderGraphiQL,
|
|
6
|
-
sendResult,
|
|
7
|
-
shouldRenderGraphiQL,
|
|
8
|
-
} from 'graphql-helix';
|
|
9
|
-
import express, { Express } from 'express';
|
|
10
|
-
|
|
11
|
-
export const createServer = ({
|
|
12
|
-
schemaComposer,
|
|
13
|
-
}: {
|
|
14
|
-
schemaComposer: SchemaComposer<any>;
|
|
15
|
-
}): Express => {
|
|
16
|
-
const server = express();
|
|
17
|
-
|
|
18
|
-
server.use(express.json());
|
|
19
|
-
|
|
20
|
-
server.use('/graphql', async (req, res) => {
|
|
21
|
-
const request = {
|
|
22
|
-
body: req.body,
|
|
23
|
-
headers: req.headers,
|
|
24
|
-
method: req.method,
|
|
25
|
-
query: req.query,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
if (shouldRenderGraphiQL(request)) {
|
|
29
|
-
res.send(renderGraphiQL());
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const { operationName, query, variables } = getGraphQLParameters(request);
|
|
34
|
-
|
|
35
|
-
const result = await processRequest({
|
|
36
|
-
operationName,
|
|
37
|
-
query,
|
|
38
|
-
variables,
|
|
39
|
-
request,
|
|
40
|
-
schema: schemaComposer.buildSchema(),
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
sendResult(result, res);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
return server;
|
|
47
|
-
};
|