@ttoss/appsync-api 0.7.4 → 0.8.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/server.js CHANGED
@@ -19,6 +19,10 @@ var __copyProps = (to, from, except, desc) => {
19
19
  return to;
20
20
  };
21
21
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
22
26
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
27
  mod
24
28
  ));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/appsync-api",
3
- "version": "0.7.4",
3
+ "version": "0.8.0",
4
4
  "description": "A library for building GraphQL APIs for AWS AppSync.",
5
5
  "license": "UNLICENSED",
6
6
  "author": "ttoss",
@@ -33,19 +33,20 @@
33
33
  "sideEffects": false,
34
34
  "typings": "dist/index.d.ts",
35
35
  "dependencies": {
36
- "@ttoss/cloudformation": "^0.5.0",
36
+ "@ttoss/cloudformation": "^0.5.1",
37
37
  "express": "^4.18.2",
38
+ "graphql-compose-connection": "^8.2.1",
38
39
  "graphql-helix": "^1.13.0",
39
- "minimist": "^1.2.7"
40
+ "minimist": "^1.2.8"
40
41
  },
41
42
  "peerDependencies": {
42
43
  "graphql": "^16.6.0",
43
44
  "graphql-compose": "^9.0.10"
44
45
  },
45
46
  "devDependencies": {
46
- "@ttoss/config": "^1.28.0",
47
+ "@ttoss/config": "^1.28.1",
47
48
  "@types/aws-lambda": "^8.10.110",
48
- "carlin": "^1.23.1",
49
+ "carlin": "^1.23.2",
49
50
  "graphql": "^16.6.0",
50
51
  "graphql-compose": "^9.0.10"
51
52
  },
@@ -68,5 +69,5 @@
68
69
  ]
69
70
  }
70
71
  },
71
- "gitHead": "6fbc10064e36429416efae0435cea250c5a39d52"
72
+ "gitHead": "84ebce1283e142519776d7045c484269d3dbabe4"
72
73
  }
@@ -0,0 +1,67 @@
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
+ };
@@ -0,0 +1,32 @@
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
+ };
@@ -0,0 +1,7 @@
1
+ import { composeWithRelay } from './composeWithRelay';
2
+ import { schemaComposer } from 'graphql-compose';
3
+
4
+ composeWithRelay(schemaComposer.Query);
5
+
6
+ export { composeWithRelay };
7
+ export { fromGlobalId, toGlobalId } from './globalId';
@@ -0,0 +1,82 @@
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
+ };
@@ -0,0 +1,31 @@
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/index.ts CHANGED
@@ -4,3 +4,5 @@ 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';
@@ -1,27 +0,0 @@
1
- /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __commonJS = (cb, mod) => function __require() {
9
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
- mod
22
- ));
23
-
24
- export {
25
- __commonJS,
26
- __toESM
27
- };