@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 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 keys of the `sort` object. In our example, it's `ASC` and `DESC`.
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 argv = (0, import_yargs.default)(process.argv.slice(2)).argv;
47
- (async () => {
48
- if (argv._.includes("build-schema")) {
49
- import_npmlog.default.info(logPrefix, "Building schema...");
50
- await fs.promises.mkdir("schema", {
51
- recursive: true
52
- });
53
- try {
54
- await fs.promises.access("schema/types.ts");
55
- } catch {
56
- await fs.promises.writeFile("schema/types.ts", "");
57
- }
58
- const {
59
- schemaComposer
60
- } = require(path.resolve(process.cwd(), "src", "schemaComposer.ts"));
61
- const sdl = schemaComposer.toSDL();
62
- const codegenConfig = {
63
- documents: [],
64
- config: {
65
- declarationKind: {
66
- type: "interface",
67
- interface: "interface"
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
- filename: "schema/types.ts",
72
- schema: (0, import_graphql.parse)(sdl),
73
- plugins: [{
74
- typescript: {}
75
- }],
76
- pluginMap: {
77
- typescript: typescriptPlugin
78
- }
79
- };
80
- await fs.promises.writeFile("schema/schema.graphql", sdl);
81
- import_npmlog.default.info(logPrefix, "Generating types...");
82
- const typesOutput = await (0, import_core.codegen)(codegenConfig);
83
- const typesOutputIgnore = ["/* eslint-disable */"].join("\n");
84
- await fs.promises.writeFile("schema/types.ts", `${typesOutputIgnore}
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
- import_npmlog.default.info(logPrefix, "Schema and types generated!");
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 argv = yargs(process.argv.slice(2)).argv;
22
- (async () => {
23
- if (argv._.includes("build-schema")) {
24
- log.info(logPrefix, "Building schema...");
25
- await fs.promises.mkdir("schema", {
26
- recursive: true
27
- });
28
- try {
29
- await fs.promises.access("schema/types.ts");
30
- } catch {
31
- await fs.promises.writeFile("schema/types.ts", "");
32
- }
33
- const {
34
- schemaComposer
35
- } = __require(path.resolve(process.cwd(), "src", "schemaComposer.ts"));
36
- const sdl = schemaComposer.toSDL();
37
- const codegenConfig = {
38
- documents: [],
39
- config: {
40
- declarationKind: {
41
- type: "interface",
42
- interface: "interface"
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
- filename: "schema/types.ts",
47
- schema: parse(sdl),
48
- plugins: [{
49
- typescript: {}
50
- }],
51
- pluginMap: {
52
- typescript: typescriptPlugin
53
- }
54
- };
55
- await fs.promises.writeFile("schema/schema.graphql", sdl);
56
- log.info(logPrefix, "Generating types...");
57
- const typesOutput = await codegen(codegenConfig);
58
- const typesOutputIgnore = ["/* eslint-disable */"].join("\n");
59
- await fs.promises.writeFile("schema/types.ts", `${typesOutputIgnore}
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
- log.info(logPrefix, "Schema and types generated!");
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.4.7",
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.1",
53
- "@ttoss/config": "^1.31.4"
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
- const argv: any = yargs(process.argv.slice(2)).argv;
21
+ const buildSchema = async ({ directory }: { directory: string }) => {
22
+ log.info(logPrefix, 'Building schema...');
22
23
 
23
- (async () => {
24
- if (argv._.includes('build-schema')) {
25
- log.info(logPrefix, 'Building schema...');
24
+ await fs.promises.mkdir('schema', { recursive: true });
26
25
 
27
- await fs.promises.mkdir('schema', { recursive: true });
28
-
29
- /**
30
- * Create schema/types.ts if it doesn't exist. We need to do this because
31
- * graphql-codegen will throw an error if the file doesn't exist and the
32
- * code import the types from that file.
33
- */
34
- try {
35
- await fs.promises.access('schema/types.ts');
36
- } catch {
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
- // eslint-disable-next-line @typescript-eslint/no-var-requires
41
- const { schemaComposer } = require(
42
- path.resolve(process.cwd(), 'src', 'schemaComposer.ts')
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
- const sdl = schemaComposer.toSDL();
42
+ const sdl = schemaComposer.toSDL();
46
43
 
47
- /**
48
- * https://the-guild.dev/graphql/codegen/docs/advanced/programmatic-usage
49
- */
50
- const codegenConfig = {
51
- documents: [],
52
- config: {
53
- declarationKind: {
54
- type: 'interface',
55
- interface: 'interface',
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
- filename: 'schema/types.ts',
60
- schema: parse(sdl),
61
- plugins: [
62
- {
63
- typescript: {},
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
- await fs.promises.writeFile('schema/schema.graphql', sdl);
68
+ await fs.promises.writeFile('schema/schema.graphql', sdl);
72
69
 
73
- log.info(logPrefix, 'Generating types...');
70
+ log.info(logPrefix, 'Generating types...');
74
71
 
75
- const typesOutput = await codegen(codegenConfig);
72
+ const typesOutput = await codegen(codegenConfig);
76
73
 
77
- const typesOutputIgnore = ['/* eslint-disable */'].join('\n');
74
+ const typesOutputIgnore = ['/* eslint-disable */'].join('\n');
78
75
 
79
- await fs.promises.writeFile(
80
- 'schema/types.ts',
81
- `${typesOutputIgnore}\n${typesOutput}`
82
- );
76
+ await fs.promises.writeFile(
77
+ 'schema/types.ts',
78
+ `${typesOutputIgnore}\n${typesOutput}`
79
+ );
83
80
 
84
- log.info(logPrefix, 'Schema and types generated!');
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();