@ttoss/graphql-api 0.5.1 → 0.5.3
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 +33 -1
- package/dist/cli.js +20 -0
- package/dist/esm/cli.js +20 -0
- package/dist/esm/index.js +8 -24
- package/dist/index.d.mts +4 -18
- package/dist/index.d.ts +4 -18
- package/dist/index.js +9 -29
- package/package.json +6 -4
- package/src/buildSchema.ts +15 -9
- package/src/cli.ts +23 -1
- package/src/composeWithRelay/composeWithRelay.ts +3 -2
- package/src/composeWithRelay/index.ts +0 -1
- package/src/composeWithRelay/nodeFieldConfig.ts +6 -2
- package/src/index.ts +6 -2
- package/src/composeWithRelay/globalId.ts +0 -32
package/README.md
CHANGED
|
@@ -34,6 +34,19 @@ const UserTC = schemaComposer.createObjectTC({
|
|
|
34
34
|
});
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
+
This library uses the `tsconfig.json` file from the target package it is being applied on. If you are using relative imports in your package you can skip this section, but, if you use path aliases in your typescript code by leveraging the [`paths`](https://www.typescriptlang.org/tsconfig#paths) property, the [`baseUrl`](https://www.typescriptlang.org/tsconfig#baseUrl) must be filled accordingly.This is needed because in order to interpret the path aliases, `ts-node` uses [`tsconfig-paths`](https://github.com/dividab/tsconfig-paths) to resolve the modules that uses this config, and `tsconfig-paths` needs both `baseUrl` and `paths` values to be non-null. A `tsconfig.json` example that follows such recommendations is given below:
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"compilerOptions": {
|
|
42
|
+
"baseUrl": ".",
|
|
43
|
+
"paths": {
|
|
44
|
+
"src/*": ["src/*"]
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
37
50
|
### Resolvers
|
|
38
51
|
|
|
39
52
|
### Integrate All Modules
|
|
@@ -369,9 +382,28 @@ const permissions = shield(
|
|
|
369
382
|
}
|
|
370
383
|
);
|
|
371
384
|
|
|
385
|
+
/**
|
|
386
|
+
* Apply middlewares to all resolvers.
|
|
387
|
+
*/
|
|
388
|
+
const logInput = async (resolve, source, args, context, info) => {
|
|
389
|
+
console.log(`1. logInput: ${JSON.stringify(args)}`)
|
|
390
|
+
const result = await resolve(source, args, context, info)
|
|
391
|
+
console.log(`5. logInput`)
|
|
392
|
+
return result
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Apply middlewares only to a specific resolver.
|
|
397
|
+
*/
|
|
398
|
+
const logOnQueryMe = {
|
|
399
|
+
Query: {
|
|
400
|
+
me: logInput
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
372
404
|
const schema = buildSchema({
|
|
373
405
|
schemaComposer,
|
|
374
|
-
middlewares; [permissions],
|
|
406
|
+
middlewares; [permissions, logInput, logOnQueryMe],
|
|
375
407
|
})
|
|
376
408
|
```
|
|
377
409
|
|
package/dist/cli.js
CHANGED
|
@@ -34,9 +34,28 @@ var import_core = require("@graphql-codegen/core");
|
|
|
34
34
|
var import_helpers = require("yargs/helpers");
|
|
35
35
|
var import_graphql = require("graphql");
|
|
36
36
|
var import_ts_node = require("ts-node");
|
|
37
|
+
var import_tsconfig_paths = require("tsconfig-paths");
|
|
37
38
|
var import_npmlog = __toESM(require("npmlog"));
|
|
38
39
|
var import_yargs = __toESM(require("yargs"));
|
|
39
40
|
var logPrefix = "graphql-api";
|
|
41
|
+
var tsConfig = require(path.resolve(process.cwd(), "tsconfig.json"));
|
|
42
|
+
var cleanup = () => {};
|
|
43
|
+
try {
|
|
44
|
+
const baseUrl = tsConfig?.compilerOptions?.baseUrl;
|
|
45
|
+
const paths = tsConfig?.compilerOptions?.paths;
|
|
46
|
+
if (baseUrl && !paths || !baseUrl && paths) {
|
|
47
|
+
throw new Error("tsconfig.json must have 'baseUrl' and 'paths' properties.");
|
|
48
|
+
}
|
|
49
|
+
if (baseUrl && paths) {
|
|
50
|
+
cleanup = (0, import_tsconfig_paths.register)({
|
|
51
|
+
baseUrl: tsConfig.compilerOptions.baseUrl,
|
|
52
|
+
paths: tsConfig.compilerOptions.paths
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
} catch (error) {
|
|
56
|
+
error instanceof Error && import_npmlog.default.error(logPrefix, error.message);
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
40
59
|
(0, import_ts_node.register)({
|
|
41
60
|
transpileOnly: true,
|
|
42
61
|
compilerOptions: {
|
|
@@ -84,6 +103,7 @@ var buildSchema = async ({
|
|
|
84
103
|
const typesOutputIgnore = ["/* eslint-disable */"].join("\n");
|
|
85
104
|
await fs.promises.writeFile("schema/types.ts", `${typesOutputIgnore}
|
|
86
105
|
${typesOutput}`);
|
|
106
|
+
cleanup();
|
|
87
107
|
import_npmlog.default.info(logPrefix, "Schema and types generated!");
|
|
88
108
|
};
|
|
89
109
|
(0, import_yargs.default)((0, import_helpers.hideBin)(process.argv)).command("build-schema", "fetch the contents of the URL", yargs2 => {
|
package/dist/esm/cli.js
CHANGED
|
@@ -9,9 +9,28 @@ import { codegen } from "@graphql-codegen/core";
|
|
|
9
9
|
import { hideBin } from "yargs/helpers";
|
|
10
10
|
import { parse } from "graphql";
|
|
11
11
|
import { register } from "ts-node";
|
|
12
|
+
import { register as registerTsPaths } from "tsconfig-paths";
|
|
12
13
|
import log from "npmlog";
|
|
13
14
|
import yargs from "yargs";
|
|
14
15
|
var logPrefix = "graphql-api";
|
|
16
|
+
var tsConfig = __require(path.resolve(process.cwd(), "tsconfig.json"));
|
|
17
|
+
var cleanup = () => {};
|
|
18
|
+
try {
|
|
19
|
+
const baseUrl = tsConfig?.compilerOptions?.baseUrl;
|
|
20
|
+
const paths = tsConfig?.compilerOptions?.paths;
|
|
21
|
+
if (baseUrl && !paths || !baseUrl && paths) {
|
|
22
|
+
throw new Error("tsconfig.json must have 'baseUrl' and 'paths' properties.");
|
|
23
|
+
}
|
|
24
|
+
if (baseUrl && paths) {
|
|
25
|
+
cleanup = registerTsPaths({
|
|
26
|
+
baseUrl: tsConfig.compilerOptions.baseUrl,
|
|
27
|
+
paths: tsConfig.compilerOptions.paths
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
} catch (error) {
|
|
31
|
+
error instanceof Error && log.error(logPrefix, error.message);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
15
34
|
register({
|
|
16
35
|
transpileOnly: true,
|
|
17
36
|
compilerOptions: {
|
|
@@ -59,6 +78,7 @@ var buildSchema = async ({
|
|
|
59
78
|
const typesOutputIgnore = ["/* eslint-disable */"].join("\n");
|
|
60
79
|
await fs.promises.writeFile("schema/types.ts", `${typesOutputIgnore}
|
|
61
80
|
${typesOutput}`);
|
|
81
|
+
cleanup();
|
|
62
82
|
log.info(logPrefix, "Schema and types generated!");
|
|
63
83
|
};
|
|
64
84
|
yargs(hideBin(process.argv)).command("build-schema", "fetch the contents of the URL", yargs2 => {
|
package/dist/esm/index.js
CHANGED
|
@@ -6,27 +6,7 @@ import { ObjectTypeComposer } from "graphql-compose";
|
|
|
6
6
|
|
|
7
7
|
// src/composeWithRelay/nodeFieldConfig.ts
|
|
8
8
|
import { getProjectionFromAST } from "graphql-compose";
|
|
9
|
-
|
|
10
|
-
// src/composeWithRelay/globalId.ts
|
|
11
|
-
var base64 = i => {
|
|
12
|
-
return Buffer.from(i, "ascii").toString("base64");
|
|
13
|
-
};
|
|
14
|
-
var unbase64 = i => {
|
|
15
|
-
return Buffer.from(i, "base64").toString("ascii");
|
|
16
|
-
};
|
|
17
|
-
var toGlobalId = (type, recordId) => {
|
|
18
|
-
return base64([type, recordId].join(":"));
|
|
19
|
-
};
|
|
20
|
-
var fromGlobalId = globalId => {
|
|
21
|
-
const unbasedGlobalId = unbase64(globalId);
|
|
22
|
-
const [type, recordId] = unbasedGlobalId.split(":");
|
|
23
|
-
return {
|
|
24
|
-
type,
|
|
25
|
-
recordId
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// src/composeWithRelay/nodeFieldConfig.ts
|
|
9
|
+
import { fromGlobalId } from "@ttoss/ids";
|
|
30
10
|
var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
|
|
31
11
|
return {
|
|
32
12
|
description: "Fetches an object that has globally unique ID among all types",
|
|
@@ -37,6 +17,7 @@ var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
|
|
|
37
17
|
description: "The globally unique ID among all types"
|
|
38
18
|
}
|
|
39
19
|
},
|
|
20
|
+
// eslint-disable-next-line max-params
|
|
40
21
|
resolve: (source, args, context, info) => {
|
|
41
22
|
if (!args.id || !(typeof args.id === "string")) {
|
|
42
23
|
return null;
|
|
@@ -73,7 +54,9 @@ var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
|
|
|
73
54
|
info,
|
|
74
55
|
projection
|
|
75
56
|
}).then(res => {
|
|
76
|
-
if (!res)
|
|
57
|
+
if (!res) {
|
|
58
|
+
return res;
|
|
59
|
+
}
|
|
77
60
|
res.__nodeType = graphqlType;
|
|
78
61
|
return res;
|
|
79
62
|
});
|
|
@@ -108,6 +91,7 @@ var getNodeInterface = sc => {
|
|
|
108
91
|
};
|
|
109
92
|
|
|
110
93
|
// src/composeWithRelay/composeWithRelay.ts
|
|
94
|
+
import { toGlobalId } from "@ttoss/ids";
|
|
111
95
|
var TypeMapForRelayNode = {};
|
|
112
96
|
var composeWithRelay = tc => {
|
|
113
97
|
if (!(tc instanceof ObjectTypeComposer)) {
|
|
@@ -138,7 +122,7 @@ var composeWithRelay = tc => {
|
|
|
138
122
|
type: "ID!",
|
|
139
123
|
description: "The globally unique ID among all types",
|
|
140
124
|
resolve: source => {
|
|
141
|
-
return toGlobalId(tc.getTypeName(), tc.getRecordId(source));
|
|
125
|
+
return toGlobalId(tc.getTypeName(), tc.getRecordId(source).toString());
|
|
142
126
|
}
|
|
143
127
|
}
|
|
144
128
|
});
|
|
@@ -174,4 +158,4 @@ var buildSchema = ({
|
|
|
174
158
|
}
|
|
175
159
|
return schema;
|
|
176
160
|
};
|
|
177
|
-
export { buildSchema, default2 as composeWithConnection, composeWithRelay
|
|
161
|
+
export { buildSchema, default2 as composeWithConnection, composeWithRelay };
|
package/dist/index.d.mts
CHANGED
|
@@ -3,28 +3,14 @@ export * from 'graphql-compose';
|
|
|
3
3
|
export { default as composeWithConnection } from 'graphql-compose-connection';
|
|
4
4
|
import { GraphQLSchema } from 'graphql';
|
|
5
5
|
import { IMiddleware, IMiddlewareGenerator } from 'graphql-middleware';
|
|
6
|
+
export { IMiddleware as Middleware } from 'graphql-middleware';
|
|
6
7
|
|
|
7
8
|
declare const composeWithRelay: <TContext>(tc: ObjectTypeComposer<any, TContext>) => ObjectTypeComposer<any, TContext>;
|
|
8
9
|
|
|
9
|
-
type
|
|
10
|
-
type: string;
|
|
11
|
-
recordId: string;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Takes a type name and an ID specific to that type name, and returns a
|
|
15
|
-
* "global ID" that is unique among all types.
|
|
16
|
-
*/
|
|
17
|
-
declare const toGlobalId: (type: string, recordId: string | number) => string;
|
|
18
|
-
/**
|
|
19
|
-
* Takes the "global ID" created by toGlobalID, and returns the type name and ID
|
|
20
|
-
* used to create it.
|
|
21
|
-
*/
|
|
22
|
-
declare const fromGlobalId: (globalId: string) => ResolvedGlobalId;
|
|
23
|
-
|
|
24
|
-
type BuildSchemaInput<TContext = any> = {
|
|
10
|
+
type BuildSchemaInput<TContext = unknown> = {
|
|
25
11
|
schemaComposer: SchemaComposer<TContext>;
|
|
26
|
-
middlewares?: (IMiddleware | IMiddlewareGenerator<
|
|
12
|
+
middlewares?: (IMiddleware | IMiddlewareGenerator<unknown, TContext, unknown>)[];
|
|
27
13
|
};
|
|
28
14
|
declare const buildSchema: ({ schemaComposer, middlewares, }: BuildSchemaInput) => GraphQLSchema;
|
|
29
15
|
|
|
30
|
-
export { type BuildSchemaInput, buildSchema, composeWithRelay
|
|
16
|
+
export { type BuildSchemaInput, buildSchema, composeWithRelay };
|
package/dist/index.d.ts
CHANGED
|
@@ -3,28 +3,14 @@ export * from 'graphql-compose';
|
|
|
3
3
|
export { default as composeWithConnection } from 'graphql-compose-connection';
|
|
4
4
|
import { GraphQLSchema } from 'graphql';
|
|
5
5
|
import { IMiddleware, IMiddlewareGenerator } from 'graphql-middleware';
|
|
6
|
+
export { IMiddleware as Middleware } from 'graphql-middleware';
|
|
6
7
|
|
|
7
8
|
declare const composeWithRelay: <TContext>(tc: ObjectTypeComposer<any, TContext>) => ObjectTypeComposer<any, TContext>;
|
|
8
9
|
|
|
9
|
-
type
|
|
10
|
-
type: string;
|
|
11
|
-
recordId: string;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Takes a type name and an ID specific to that type name, and returns a
|
|
15
|
-
* "global ID" that is unique among all types.
|
|
16
|
-
*/
|
|
17
|
-
declare const toGlobalId: (type: string, recordId: string | number) => string;
|
|
18
|
-
/**
|
|
19
|
-
* Takes the "global ID" created by toGlobalID, and returns the type name and ID
|
|
20
|
-
* used to create it.
|
|
21
|
-
*/
|
|
22
|
-
declare const fromGlobalId: (globalId: string) => ResolvedGlobalId;
|
|
23
|
-
|
|
24
|
-
type BuildSchemaInput<TContext = any> = {
|
|
10
|
+
type BuildSchemaInput<TContext = unknown> = {
|
|
25
11
|
schemaComposer: SchemaComposer<TContext>;
|
|
26
|
-
middlewares?: (IMiddleware | IMiddlewareGenerator<
|
|
12
|
+
middlewares?: (IMiddleware | IMiddlewareGenerator<unknown, TContext, unknown>)[];
|
|
27
13
|
};
|
|
28
14
|
declare const buildSchema: ({ schemaComposer, middlewares, }: BuildSchemaInput) => GraphQLSchema;
|
|
29
15
|
|
|
30
|
-
export { type BuildSchemaInput, buildSchema, composeWithRelay
|
|
16
|
+
export { type BuildSchemaInput, buildSchema, composeWithRelay };
|
package/dist/index.js
CHANGED
|
@@ -41,9 +41,7 @@ var src_exports = {};
|
|
|
41
41
|
__export(src_exports, {
|
|
42
42
|
buildSchema: () => buildSchema,
|
|
43
43
|
composeWithConnection: () => import_graphql_compose_connection.default,
|
|
44
|
-
composeWithRelay: () => composeWithRelay
|
|
45
|
-
fromGlobalId: () => fromGlobalId,
|
|
46
|
-
toGlobalId: () => toGlobalId
|
|
44
|
+
composeWithRelay: () => composeWithRelay
|
|
47
45
|
});
|
|
48
46
|
module.exports = __toCommonJS(src_exports);
|
|
49
47
|
|
|
@@ -52,27 +50,7 @@ var import_graphql_compose3 = require("graphql-compose");
|
|
|
52
50
|
|
|
53
51
|
// src/composeWithRelay/nodeFieldConfig.ts
|
|
54
52
|
var import_graphql_compose = require("graphql-compose");
|
|
55
|
-
|
|
56
|
-
// src/composeWithRelay/globalId.ts
|
|
57
|
-
var base64 = i => {
|
|
58
|
-
return Buffer.from(i, "ascii").toString("base64");
|
|
59
|
-
};
|
|
60
|
-
var unbase64 = i => {
|
|
61
|
-
return Buffer.from(i, "base64").toString("ascii");
|
|
62
|
-
};
|
|
63
|
-
var toGlobalId = (type, recordId) => {
|
|
64
|
-
return base64([type, recordId].join(":"));
|
|
65
|
-
};
|
|
66
|
-
var fromGlobalId = globalId => {
|
|
67
|
-
const unbasedGlobalId = unbase64(globalId);
|
|
68
|
-
const [type, recordId] = unbasedGlobalId.split(":");
|
|
69
|
-
return {
|
|
70
|
-
type,
|
|
71
|
-
recordId
|
|
72
|
-
};
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
// src/composeWithRelay/nodeFieldConfig.ts
|
|
53
|
+
var import_ids = require("@ttoss/ids");
|
|
76
54
|
var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
|
|
77
55
|
return {
|
|
78
56
|
description: "Fetches an object that has globally unique ID among all types",
|
|
@@ -83,13 +61,14 @@ var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
|
|
|
83
61
|
description: "The globally unique ID among all types"
|
|
84
62
|
}
|
|
85
63
|
},
|
|
64
|
+
// eslint-disable-next-line max-params
|
|
86
65
|
resolve: (source, args, context, info) => {
|
|
87
66
|
if (!args.id || !(typeof args.id === "string")) {
|
|
88
67
|
return null;
|
|
89
68
|
}
|
|
90
69
|
const {
|
|
91
70
|
type
|
|
92
|
-
} = fromGlobalId(args.id);
|
|
71
|
+
} = (0, import_ids.fromGlobalId)(args.id);
|
|
93
72
|
if (!typeMapForRelayNode[type]) {
|
|
94
73
|
return null;
|
|
95
74
|
}
|
|
@@ -119,7 +98,9 @@ var getNodeFieldConfig = (typeMapForRelayNode, nodeInterface) => {
|
|
|
119
98
|
info,
|
|
120
99
|
projection
|
|
121
100
|
}).then(res => {
|
|
122
|
-
if (!res)
|
|
101
|
+
if (!res) {
|
|
102
|
+
return res;
|
|
103
|
+
}
|
|
123
104
|
res.__nodeType = graphqlType;
|
|
124
105
|
return res;
|
|
125
106
|
});
|
|
@@ -154,6 +135,7 @@ var getNodeInterface = sc => {
|
|
|
154
135
|
};
|
|
155
136
|
|
|
156
137
|
// src/composeWithRelay/composeWithRelay.ts
|
|
138
|
+
var import_ids2 = require("@ttoss/ids");
|
|
157
139
|
var TypeMapForRelayNode = {};
|
|
158
140
|
var composeWithRelay = tc => {
|
|
159
141
|
if (!(tc instanceof import_graphql_compose3.ObjectTypeComposer)) {
|
|
@@ -184,7 +166,7 @@ var composeWithRelay = tc => {
|
|
|
184
166
|
type: "ID!",
|
|
185
167
|
description: "The globally unique ID among all types",
|
|
186
168
|
resolve: source => {
|
|
187
|
-
return toGlobalId(tc.getTypeName(), tc.getRecordId(source));
|
|
169
|
+
return (0, import_ids2.toGlobalId)(tc.getTypeName(), tc.getRecordId(source).toString());
|
|
188
170
|
}
|
|
189
171
|
}
|
|
190
172
|
});
|
|
@@ -225,7 +207,5 @@ var buildSchema = ({
|
|
|
225
207
|
buildSchema,
|
|
226
208
|
composeWithConnection,
|
|
227
209
|
composeWithRelay,
|
|
228
|
-
fromGlobalId,
|
|
229
|
-
toGlobalId,
|
|
230
210
|
...require("graphql-compose")
|
|
231
211
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/graphql-api",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "A library for building GraphQL APIs using ttoss ecosystem.",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
@@ -32,15 +32,17 @@
|
|
|
32
32
|
],
|
|
33
33
|
"sideEffects": false,
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@graphql-codegen/core": "^4.0.
|
|
36
|
-
"@graphql-codegen/typescript": "^4.0.
|
|
35
|
+
"@graphql-codegen/core": "^4.0.2",
|
|
36
|
+
"@graphql-codegen/typescript": "^4.0.6",
|
|
37
37
|
"graphql-compose": "^9.0.10",
|
|
38
38
|
"graphql-compose-connection": "^8.2.1",
|
|
39
39
|
"graphql-middleware": "^6.1.35",
|
|
40
40
|
"graphql-shield": "^7.6.5",
|
|
41
41
|
"npmlog": "^7.0.1",
|
|
42
42
|
"ts-node": "^10.9.2",
|
|
43
|
-
"
|
|
43
|
+
"tsconfig-paths": "^4.2.0",
|
|
44
|
+
"yargs": "^17.7.2",
|
|
45
|
+
"@ttoss/ids": "^0.1.1"
|
|
44
46
|
},
|
|
45
47
|
"peerDependencies": {
|
|
46
48
|
"graphql": "^16.6.0"
|
package/src/buildSchema.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { type GraphQLSchema } from 'graphql';
|
|
2
2
|
import {
|
|
3
|
-
type IMiddleware,
|
|
4
|
-
type IMiddlewareGenerator,
|
|
3
|
+
type IMiddleware as Middleware,
|
|
4
|
+
type IMiddlewareGenerator as MiddlewareGenerator,
|
|
5
5
|
applyMiddleware,
|
|
6
6
|
} from 'graphql-middleware';
|
|
7
7
|
import { type SchemaComposer } from 'graphql-compose';
|
|
8
8
|
|
|
9
|
-
export type {
|
|
9
|
+
export type { Middleware, MiddlewareGenerator };
|
|
10
10
|
|
|
11
|
-
export type BuildSchemaInput<TContext =
|
|
11
|
+
export type BuildSchemaInput<TContext = unknown> = {
|
|
12
12
|
schemaComposer: SchemaComposer<TContext>;
|
|
13
|
-
middlewares?: (
|
|
13
|
+
middlewares?: (
|
|
14
|
+
| Middleware
|
|
15
|
+
| MiddlewareGenerator<unknown, TContext, unknown>
|
|
16
|
+
)[];
|
|
14
17
|
};
|
|
15
18
|
|
|
16
19
|
export const buildSchema = ({
|
|
@@ -30,10 +33,13 @@ export const buildSchema = ({
|
|
|
30
33
|
/**
|
|
31
34
|
* https://github.com/dimatill/graphql-middleware/issues/433#issuecomment-1170187160
|
|
32
35
|
*/
|
|
33
|
-
if (
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
if (
|
|
37
|
+
(middleware as MiddlewareGenerator<unknown, unknown, unknown>)
|
|
38
|
+
.generate
|
|
39
|
+
) {
|
|
40
|
+
return (
|
|
41
|
+
middleware as MiddlewareGenerator<unknown, unknown, unknown>
|
|
42
|
+
).generate(schema);
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
return middleware;
|
package/src/cli.ts
CHANGED
|
@@ -5,10 +5,32 @@ import { codegen } from '@graphql-codegen/core';
|
|
|
5
5
|
import { hideBin } from 'yargs/helpers';
|
|
6
6
|
import { parse } from 'graphql';
|
|
7
7
|
import { register } from 'ts-node';
|
|
8
|
+
import { register as registerTsPaths } from 'tsconfig-paths';
|
|
8
9
|
import log from 'npmlog';
|
|
9
10
|
import yargs from 'yargs';
|
|
10
11
|
|
|
11
12
|
const logPrefix = 'graphql-api';
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
14
|
+
const tsConfig = require(path.resolve(process.cwd(), 'tsconfig.json'));
|
|
15
|
+
let cleanup = () => {};
|
|
16
|
+
try {
|
|
17
|
+
const baseUrl = tsConfig?.compilerOptions?.baseUrl;
|
|
18
|
+
const paths = tsConfig?.compilerOptions?.paths;
|
|
19
|
+
if ((baseUrl && !paths) || (!baseUrl && paths)) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
"tsconfig.json must have 'baseUrl' and 'paths' properties."
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
if (baseUrl && paths) {
|
|
25
|
+
cleanup = registerTsPaths({
|
|
26
|
+
baseUrl: tsConfig.compilerOptions.baseUrl,
|
|
27
|
+
paths: tsConfig.compilerOptions.paths,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
} catch (error: unknown) {
|
|
31
|
+
error instanceof Error && log.error(logPrefix, error.message);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
12
34
|
|
|
13
35
|
register({
|
|
14
36
|
transpileOnly: true,
|
|
@@ -77,7 +99,7 @@ const buildSchema = async ({ directory }: { directory: string }) => {
|
|
|
77
99
|
'schema/types.ts',
|
|
78
100
|
`${typesOutputIgnore}\n${typesOutput}`
|
|
79
101
|
);
|
|
80
|
-
|
|
102
|
+
cleanup();
|
|
81
103
|
log.info(logPrefix, 'Schema and types generated!');
|
|
82
104
|
};
|
|
83
105
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1
2
|
import { ObjectTypeComposer } from 'graphql-compose';
|
|
2
3
|
import { getNodeFieldConfig } from './nodeFieldConfig';
|
|
3
4
|
import { getNodeInterface } from './nodeInterface';
|
|
4
|
-
import { toGlobalId } from '
|
|
5
|
+
import { toGlobalId } from '@ttoss/ids';
|
|
5
6
|
|
|
6
7
|
// all wrapped typeComposers with Relay, stored in this variable
|
|
7
8
|
// for futher type resolving via NodeInterface.resolveType method
|
|
@@ -56,7 +57,7 @@ export const composeWithRelay = <TContext>(
|
|
|
56
57
|
type: 'ID!',
|
|
57
58
|
description: 'The globally unique ID among all types',
|
|
58
59
|
resolve: (source) => {
|
|
59
|
-
return toGlobalId(tc.getTypeName(), tc.getRecordId(source));
|
|
60
|
+
return toGlobalId(tc.getTypeName(), tc.getRecordId(source).toString());
|
|
60
61
|
},
|
|
61
62
|
},
|
|
62
63
|
});
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1
2
|
import {
|
|
2
3
|
type InterfaceTypeComposer,
|
|
3
4
|
type ObjectTypeComposerFieldConfigDefinition,
|
|
4
5
|
getProjectionFromAST,
|
|
5
6
|
} from 'graphql-compose';
|
|
6
|
-
import { fromGlobalId } from '
|
|
7
|
+
import { fromGlobalId } from '@ttoss/ids';
|
|
7
8
|
import type { GraphQLResolveInfo } from 'graphql-compose/lib/graphql';
|
|
8
9
|
import type { ObjectTypeComposer, Resolver } from 'graphql-compose';
|
|
9
10
|
|
|
@@ -29,6 +30,7 @@ export const getNodeFieldConfig = (
|
|
|
29
30
|
description: 'The globally unique ID among all types',
|
|
30
31
|
},
|
|
31
32
|
},
|
|
33
|
+
// eslint-disable-next-line max-params
|
|
32
34
|
resolve: (
|
|
33
35
|
source: any,
|
|
34
36
|
args: { [argName: string]: any },
|
|
@@ -70,7 +72,9 @@ export const getNodeFieldConfig = (
|
|
|
70
72
|
projection,
|
|
71
73
|
})
|
|
72
74
|
.then((res: any) => {
|
|
73
|
-
if (!res)
|
|
75
|
+
if (!res) {
|
|
76
|
+
return res;
|
|
77
|
+
}
|
|
74
78
|
res.__nodeType = graphqlType;
|
|
75
79
|
return res;
|
|
76
80
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
export { composeWithRelay
|
|
1
|
+
export { composeWithRelay } from './composeWithRelay';
|
|
2
2
|
export { default as composeWithConnection } from 'graphql-compose-connection';
|
|
3
3
|
export * from 'graphql-compose';
|
|
4
|
-
export {
|
|
4
|
+
export {
|
|
5
|
+
buildSchema,
|
|
6
|
+
type BuildSchemaInput,
|
|
7
|
+
type Middleware,
|
|
8
|
+
} from './buildSchema';
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export type Base64String = string;
|
|
2
|
-
|
|
3
|
-
export type ResolvedGlobalId = {
|
|
4
|
-
type: string;
|
|
5
|
-
recordId: 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, recordId: string | number): string => {
|
|
21
|
-
return base64([type, recordId].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, recordId] = unbasedGlobalId.split(':');
|
|
31
|
-
return { type, recordId };
|
|
32
|
-
};
|