@ttoss/graphql-api 0.4.7 → 0.5.1
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 +10 -4
- package/dist/cli.js +53 -41
- package/dist/esm/cli.js +53 -41
- package/package.json +3 -3
- package/src/cli.ts +72 -54
package/README.md
CHANGED
|
@@ -206,7 +206,7 @@ The resolver `connection` has the following arguments based on the [Relay Connec
|
|
|
206
206
|
- `before`: the cursor to start the query.
|
|
207
207
|
- `limit`: the limit of nodes to return. It's the `first` or `last` argument plus one. It's used to know if there are more nodes to return to set `hasNextPage` or `hasPreviousPage` [PageInfo](https://relay.dev/graphql/connections.htm#sec-undefined.PageInfo) fields. For example, if `first` is `10`, `limit` will be `11`. If the resolver returns `11` nodes, the resolver will return `10` but it knows there are more nodes to return, so `hasNextPage` will be `true`.
|
|
208
208
|
- `skip`: it's the `count` minus `last`. It only works on [backward pagination](https://relay.dev/graphql/connections.htm#sec-Backward-pagination-arguments).
|
|
209
|
-
- `sort`: the sort option to use. It's the
|
|
209
|
+
- `sort`: the sort option to use. It's the `value` of the `sort` object. In our example, it's `{ scanIndexForward: true }` for `ASC` and `{ scanIndexForward: false }`, for `DESC`.
|
|
210
210
|
- `filter`: the filter to use. It'll exist if you add the `filter` to `findManyResolver` for example, the implementation below will add the `filter` argument with the `name` and `book` fields:
|
|
211
211
|
|
|
212
212
|
```ts
|
|
@@ -290,7 +290,7 @@ The resolver that will be used to count the nodes.
|
|
|
290
290
|
|
|
291
291
|
It's an object that defines the sort options. Each key is the sort name and the value is an object with the following properties:
|
|
292
292
|
|
|
293
|
-
- `value`: and object that the resolver will receive as the `sort` argument.
|
|
293
|
+
- `value`: and object that the `args` resolver will receive as the `sort` argument. It'll also be the values of the sort enum composer created (check the implementation details [here](https://github.com/graphql-compose/graphql-compose-connection/blob/master/src/types/sortInputType.ts).)
|
|
294
294
|
- `cursorFields`: an array of fields that will be used to create the cursor.
|
|
295
295
|
- `beforeCursorQuery` and `afterCursorQuery`: methods that will be used to create the `rawQuery` object for the `findManyResolver`. They receive the following arguments:
|
|
296
296
|
|
|
@@ -391,16 +391,22 @@ As Relay needs an introspection query to work, this package provides a way to bu
|
|
|
391
391
|
ttoss-graphl-api build-schema
|
|
392
392
|
```
|
|
393
393
|
|
|
394
|
-
You can add the `build` script to your `package.json`:
|
|
394
|
+
You can add the `build-schema` script to your `package.json`:
|
|
395
395
|
|
|
396
396
|
```json
|
|
397
397
|
{
|
|
398
398
|
"scripts": {
|
|
399
|
-
"build": "ttoss-graphl-api build-schema"
|
|
399
|
+
"build-schema": "ttoss-graphl-api build-schema"
|
|
400
400
|
}
|
|
401
401
|
}
|
|
402
402
|
```
|
|
403
403
|
|
|
404
|
+
If your `schemaComposer` is in a different directory, you can pass the `--directory`/`-d` option to `ttoss-graphl-api build-schema` command:
|
|
405
|
+
|
|
406
|
+
```bash
|
|
407
|
+
ttoss-graphl-api build-schema -d tests
|
|
408
|
+
```
|
|
409
|
+
|
|
404
410
|
## How to Create Tests
|
|
405
411
|
|
|
406
412
|
We recommend testing the whole GraphQL API using the `graphql` object and the schema composer to provide the schema. For example:
|
package/dist/cli.js
CHANGED
|
@@ -31,6 +31,7 @@ var fs = __toESM(require("fs"));
|
|
|
31
31
|
var path = __toESM(require("path"));
|
|
32
32
|
var typescriptPlugin = __toESM(require("@graphql-codegen/typescript"));
|
|
33
33
|
var import_core = require("@graphql-codegen/core");
|
|
34
|
+
var import_helpers = require("yargs/helpers");
|
|
34
35
|
var import_graphql = require("graphql");
|
|
35
36
|
var import_ts_node = require("ts-node");
|
|
36
37
|
var import_npmlog = __toESM(require("npmlog"));
|
|
@@ -43,46 +44,57 @@ var logPrefix = "graphql-api";
|
|
|
43
44
|
moduleResolution: "NodeNext"
|
|
44
45
|
}
|
|
45
46
|
});
|
|
46
|
-
var
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
},
|
|
69
|
-
namingConvention: "keep"
|
|
47
|
+
var buildSchema = async ({
|
|
48
|
+
directory
|
|
49
|
+
}) => {
|
|
50
|
+
import_npmlog.default.info(logPrefix, "Building schema...");
|
|
51
|
+
await fs.promises.mkdir("schema", {
|
|
52
|
+
recursive: true
|
|
53
|
+
});
|
|
54
|
+
try {
|
|
55
|
+
await fs.promises.access("schema/types.ts");
|
|
56
|
+
} catch {
|
|
57
|
+
await fs.promises.writeFile("schema/types.ts", "");
|
|
58
|
+
}
|
|
59
|
+
const {
|
|
60
|
+
schemaComposer
|
|
61
|
+
} = require(path.resolve(process.cwd(), directory, "schemaComposer.ts"));
|
|
62
|
+
const sdl = schemaComposer.toSDL();
|
|
63
|
+
const codegenConfig = {
|
|
64
|
+
documents: [],
|
|
65
|
+
config: {
|
|
66
|
+
declarationKind: {
|
|
67
|
+
type: "interface",
|
|
68
|
+
interface: "interface"
|
|
70
69
|
},
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
70
|
+
namingConvention: "keep"
|
|
71
|
+
},
|
|
72
|
+
filename: "schema/types.ts",
|
|
73
|
+
schema: (0, import_graphql.parse)(sdl),
|
|
74
|
+
plugins: [{
|
|
75
|
+
typescript: {}
|
|
76
|
+
}],
|
|
77
|
+
pluginMap: {
|
|
78
|
+
typescript: typescriptPlugin
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
await fs.promises.writeFile("schema/schema.graphql", sdl);
|
|
82
|
+
import_npmlog.default.info(logPrefix, "Generating types...");
|
|
83
|
+
const typesOutput = await (0, import_core.codegen)(codegenConfig);
|
|
84
|
+
const typesOutputIgnore = ["/* eslint-disable */"].join("\n");
|
|
85
|
+
await fs.promises.writeFile("schema/types.ts", `${typesOutputIgnore}
|
|
85
86
|
${typesOutput}`);
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
import_npmlog.default.info(logPrefix, "Schema and types generated!");
|
|
88
|
+
};
|
|
89
|
+
(0, import_yargs.default)((0, import_helpers.hideBin)(process.argv)).command("build-schema", "fetch the contents of the URL", yargs2 => {
|
|
90
|
+
return yargs2.options({
|
|
91
|
+
directory: {
|
|
92
|
+
alias: ["d"],
|
|
93
|
+
type: "string",
|
|
94
|
+
describe: "Schema composer directory relative to the project root",
|
|
95
|
+
default: "src"
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}, argv => {
|
|
99
|
+
return buildSchema(argv);
|
|
100
|
+
}).demandCommand(1).strictOptions().parse();
|
package/dist/esm/cli.js
CHANGED
|
@@ -6,6 +6,7 @@ import * as fs from "node:fs";
|
|
|
6
6
|
import * as path from "node:path";
|
|
7
7
|
import * as typescriptPlugin from "@graphql-codegen/typescript";
|
|
8
8
|
import { codegen } from "@graphql-codegen/core";
|
|
9
|
+
import { hideBin } from "yargs/helpers";
|
|
9
10
|
import { parse } from "graphql";
|
|
10
11
|
import { register } from "ts-node";
|
|
11
12
|
import log from "npmlog";
|
|
@@ -18,46 +19,57 @@ register({
|
|
|
18
19
|
moduleResolution: "NodeNext"
|
|
19
20
|
}
|
|
20
21
|
});
|
|
21
|
-
var
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
},
|
|
44
|
-
namingConvention: "keep"
|
|
22
|
+
var buildSchema = async ({
|
|
23
|
+
directory
|
|
24
|
+
}) => {
|
|
25
|
+
log.info(logPrefix, "Building schema...");
|
|
26
|
+
await fs.promises.mkdir("schema", {
|
|
27
|
+
recursive: true
|
|
28
|
+
});
|
|
29
|
+
try {
|
|
30
|
+
await fs.promises.access("schema/types.ts");
|
|
31
|
+
} catch {
|
|
32
|
+
await fs.promises.writeFile("schema/types.ts", "");
|
|
33
|
+
}
|
|
34
|
+
const {
|
|
35
|
+
schemaComposer
|
|
36
|
+
} = __require(path.resolve(process.cwd(), directory, "schemaComposer.ts"));
|
|
37
|
+
const sdl = schemaComposer.toSDL();
|
|
38
|
+
const codegenConfig = {
|
|
39
|
+
documents: [],
|
|
40
|
+
config: {
|
|
41
|
+
declarationKind: {
|
|
42
|
+
type: "interface",
|
|
43
|
+
interface: "interface"
|
|
45
44
|
},
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
45
|
+
namingConvention: "keep"
|
|
46
|
+
},
|
|
47
|
+
filename: "schema/types.ts",
|
|
48
|
+
schema: parse(sdl),
|
|
49
|
+
plugins: [{
|
|
50
|
+
typescript: {}
|
|
51
|
+
}],
|
|
52
|
+
pluginMap: {
|
|
53
|
+
typescript: typescriptPlugin
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
await fs.promises.writeFile("schema/schema.graphql", sdl);
|
|
57
|
+
log.info(logPrefix, "Generating types...");
|
|
58
|
+
const typesOutput = await codegen(codegenConfig);
|
|
59
|
+
const typesOutputIgnore = ["/* eslint-disable */"].join("\n");
|
|
60
|
+
await fs.promises.writeFile("schema/types.ts", `${typesOutputIgnore}
|
|
60
61
|
${typesOutput}`);
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
log.info(logPrefix, "Schema and types generated!");
|
|
63
|
+
};
|
|
64
|
+
yargs(hideBin(process.argv)).command("build-schema", "fetch the contents of the URL", yargs2 => {
|
|
65
|
+
return yargs2.options({
|
|
66
|
+
directory: {
|
|
67
|
+
alias: ["d"],
|
|
68
|
+
type: "string",
|
|
69
|
+
describe: "Schema composer directory relative to the project root",
|
|
70
|
+
default: "src"
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}, argv => {
|
|
74
|
+
return buildSchema(argv);
|
|
75
|
+
}).demandCommand(1).strictOptions().parse();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/graphql-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "A library for building GraphQL APIs using ttoss ecosystem.",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
@@ -49,8 +49,8 @@
|
|
|
49
49
|
"@types/yargs": "^17.0.32",
|
|
50
50
|
"graphql": "^16.8.1",
|
|
51
51
|
"jest": "^29.7.0",
|
|
52
|
-
"tsup": "^8.0.
|
|
53
|
-
"@ttoss/config": "^1.31.
|
|
52
|
+
"tsup": "^8.0.2",
|
|
53
|
+
"@ttoss/config": "^1.31.5"
|
|
54
54
|
},
|
|
55
55
|
"keywords": [
|
|
56
56
|
"api",
|
package/src/cli.ts
CHANGED
|
@@ -2,6 +2,7 @@ import * as fs from 'node:fs';
|
|
|
2
2
|
import * as path from 'node:path';
|
|
3
3
|
import * as typescriptPlugin from '@graphql-codegen/typescript';
|
|
4
4
|
import { codegen } from '@graphql-codegen/core';
|
|
5
|
+
import { hideBin } from 'yargs/helpers';
|
|
5
6
|
import { parse } from 'graphql';
|
|
6
7
|
import { register } from 'ts-node';
|
|
7
8
|
import log from 'npmlog';
|
|
@@ -17,70 +18,87 @@ register({
|
|
|
17
18
|
},
|
|
18
19
|
});
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
const buildSchema = async ({ directory }: { directory: string }) => {
|
|
22
|
+
log.info(logPrefix, 'Building schema...');
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
if (argv._.includes('build-schema')) {
|
|
25
|
-
log.info(logPrefix, 'Building schema...');
|
|
24
|
+
await fs.promises.mkdir('schema', { recursive: true });
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
await fs.promises.writeFile('schema/types.ts', '');
|
|
38
|
-
}
|
|
26
|
+
/**
|
|
27
|
+
* Create schema/types.ts if it doesn't exist. We need to do this because
|
|
28
|
+
* graphql-codegen will throw an error if the file doesn't exist and the
|
|
29
|
+
* code import the types from that file.
|
|
30
|
+
*/
|
|
31
|
+
try {
|
|
32
|
+
await fs.promises.access('schema/types.ts');
|
|
33
|
+
} catch {
|
|
34
|
+
await fs.promises.writeFile('schema/types.ts', '');
|
|
35
|
+
}
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
38
|
+
const { schemaComposer } = require(
|
|
39
|
+
path.resolve(process.cwd(), directory, 'schemaComposer.ts')
|
|
40
|
+
);
|
|
44
41
|
|
|
45
|
-
|
|
42
|
+
const sdl = schemaComposer.toSDL();
|
|
46
43
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
},
|
|
57
|
-
namingConvention: 'keep',
|
|
44
|
+
/**
|
|
45
|
+
* https://the-guild.dev/graphql/codegen/docs/advanced/programmatic-usage
|
|
46
|
+
*/
|
|
47
|
+
const codegenConfig = {
|
|
48
|
+
documents: [],
|
|
49
|
+
config: {
|
|
50
|
+
declarationKind: {
|
|
51
|
+
type: 'interface',
|
|
52
|
+
interface: 'interface',
|
|
58
53
|
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
pluginMap: {
|
|
67
|
-
typescript: typescriptPlugin,
|
|
54
|
+
namingConvention: 'keep',
|
|
55
|
+
},
|
|
56
|
+
filename: 'schema/types.ts',
|
|
57
|
+
schema: parse(sdl),
|
|
58
|
+
plugins: [
|
|
59
|
+
{
|
|
60
|
+
typescript: {},
|
|
68
61
|
},
|
|
69
|
-
|
|
62
|
+
],
|
|
63
|
+
pluginMap: {
|
|
64
|
+
typescript: typescriptPlugin,
|
|
65
|
+
},
|
|
66
|
+
};
|
|
70
67
|
|
|
71
|
-
|
|
68
|
+
await fs.promises.writeFile('schema/schema.graphql', sdl);
|
|
72
69
|
|
|
73
|
-
|
|
70
|
+
log.info(logPrefix, 'Generating types...');
|
|
74
71
|
|
|
75
|
-
|
|
72
|
+
const typesOutput = await codegen(codegenConfig);
|
|
76
73
|
|
|
77
|
-
|
|
74
|
+
const typesOutputIgnore = ['/* eslint-disable */'].join('\n');
|
|
78
75
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
await fs.promises.writeFile(
|
|
77
|
+
'schema/types.ts',
|
|
78
|
+
`${typesOutputIgnore}\n${typesOutput}`
|
|
79
|
+
);
|
|
83
80
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
81
|
+
log.info(logPrefix, 'Schema and types generated!');
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
yargs(hideBin(process.argv))
|
|
85
|
+
.command(
|
|
86
|
+
'build-schema',
|
|
87
|
+
'fetch the contents of the URL',
|
|
88
|
+
(yargs) => {
|
|
89
|
+
return yargs.options({
|
|
90
|
+
directory: {
|
|
91
|
+
alias: ['d'],
|
|
92
|
+
type: 'string',
|
|
93
|
+
describe: 'Schema composer directory relative to the project root',
|
|
94
|
+
default: 'src',
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
(argv) => {
|
|
99
|
+
return buildSchema(argv);
|
|
100
|
+
}
|
|
101
|
+
)
|
|
102
|
+
.demandCommand(1)
|
|
103
|
+
.strictOptions()
|
|
104
|
+
.parse();
|