@ttoss/graphql-api-cli 0.8.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024, Terezinha Tech Operations (ttoss)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # @ttoss/graphql-api-cli
2
+
3
+ This package generates schema and TypeScript types for your GraphQL API.
4
+
5
+ ## ESM Only
6
+
7
+ This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ pnpm add -D @ttoss/graphql-api-cli
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ As Relay needs an introspection query to work, this package provides a way to build the GraphQL schema by running `ttoss-graphql-api build-schema`. It build the schema using the `schemaComposer` from `src/schemaComposer.ts` file and save the schema in `schema/schema.graphql` file and TypeScript types in `schema/types.ts` file.
18
+
19
+ ```bash
20
+ ttoss-graphql-api build-schema
21
+ ```
22
+
23
+ You can add the `build-schema` script to your `package.json`:
24
+
25
+ ```json
26
+ {
27
+ "scripts": {
28
+ "build-schema": "ttoss-graphql-api build-schema"
29
+ }
30
+ }
31
+ ```
32
+
33
+ ## Options
34
+
35
+ ### `--directory`/`-d`
36
+
37
+ If your `schemaComposer` is in a different directory, you can pass the `--directory`/`-d` option to `ttoss-graphql-api build-schema` command:
38
+
39
+ ```bash
40
+ ttoss-graphql-api build-schema -d tests
41
+ ```
42
+
43
+ ### `--external`
44
+
45
+ External dependencies to ignore during build. If you don't set this option, the `build-schema` command will use the `dependencies` from your `package.json` file.
46
+
47
+ ```bash
48
+ ttoss-graphql-api build-schema --external graphql-compose,graphql
49
+ ```
package/bin/cli.js ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import '../dist/esm/index.js';
@@ -0,0 +1,148 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+
3
+ // src/index.ts
4
+ import * as esbuild from "esbuild";
5
+ import * as fs from "node:fs";
6
+ import * as path from "node:path";
7
+ import * as typescriptPlugin from "@graphql-codegen/typescript";
8
+ import { Command } from "commander";
9
+ import { codegen } from "@graphql-codegen/core";
10
+ import { parse } from "graphql";
11
+ import log from "npmlog";
12
+
13
+ // package.json
14
+ var package_default = {
15
+ name: "@ttoss/graphql-api-cli",
16
+ version: "0.8.1",
17
+ description: "A library for building GraphQL APIs types and schema.",
18
+ license: "MIT",
19
+ author: "ttoss",
20
+ contributors: ["Pedro Arantes <pedro@arantespp.com> (https://arantespp.com)"],
21
+ repository: {
22
+ type: "git",
23
+ url: "https://github.com/ttoss/ttoss.git",
24
+ directory: "packages/graphql-api-cli"
25
+ },
26
+ type: "module",
27
+ bin: {
28
+ "ttoss-graphql-api": "./bin/cli.js"
29
+ },
30
+ files: ["dist"],
31
+ scripts: {
32
+ "build-config": "tsup-node"
33
+ },
34
+ sideEffects: false,
35
+ peerDependencies: {
36
+ graphql: "^16.6.0"
37
+ },
38
+ devDependencies: {
39
+ "@graphql-codegen/core": "^4.0.2",
40
+ "@graphql-codegen/typescript": "^4.1.2",
41
+ "@ttoss/config": "workspace:^",
42
+ commander: "^12.1.0",
43
+ esbuild: "^0.24.0",
44
+ graphql: "^16.9.0",
45
+ npmlog: "^7.0.1",
46
+ tsup: "^8.3.5"
47
+ },
48
+ keywords: ["api", "graphql"],
49
+ publishConfig: {
50
+ access: "public",
51
+ provenance: true
52
+ }
53
+ };
54
+
55
+ // src/index.ts
56
+ var logPrefix = "graphql-api";
57
+ var importSchemaComposer = async ({
58
+ external,
59
+ schemaComposerPath
60
+ }) => {
61
+ const lastEntryPointName = schemaComposerPath.split("/").pop();
62
+ const filename = lastEntryPointName?.split(".")[0];
63
+ const outfile = path.resolve(process.cwd(), "out", filename + ".js");
64
+ const packageJsonPath = path.resolve(process.cwd(), "package.json");
65
+ const packageJson = await fs.promises.readFile(packageJsonPath, "utf-8");
66
+ const dependencies = Object.keys(JSON.parse(packageJson).dependencies).filter(dependency => {
67
+ if (dependency.startsWith("@ttoss/")) {
68
+ return false;
69
+ }
70
+ if (dependency === "graphql") {
71
+ return false;
72
+ }
73
+ return true;
74
+ });
75
+ const result = await esbuild.build({
76
+ bundle: true,
77
+ entryPoints: [schemaComposerPath],
78
+ external: external || dependencies,
79
+ format: "esm",
80
+ outfile,
81
+ platform: "node",
82
+ target: "ES2023",
83
+ treeShaking: true
84
+ });
85
+ if (result.errors.length > 0) {
86
+ console.error("Error building config file: ", filename);
87
+ throw result.errors;
88
+ }
89
+ try {
90
+ return await import(outfile);
91
+ } catch (error) {
92
+ console.error("Failed importing build config file: ", filename);
93
+ throw error;
94
+ }
95
+ };
96
+ var buildSchema = async ({
97
+ directory,
98
+ external
99
+ }) => {
100
+ log.info(logPrefix, "Building schema...");
101
+ await fs.promises.mkdir("schema", {
102
+ recursive: true
103
+ });
104
+ try {
105
+ await fs.promises.access("schema/types.ts");
106
+ } catch {
107
+ await fs.promises.writeFile("schema/types.ts", "");
108
+ }
109
+ const schemaComposerPath = path.resolve(process.cwd(), directory, "schemaComposer.ts");
110
+ const {
111
+ schemaComposer
112
+ } = await importSchemaComposer({
113
+ external,
114
+ schemaComposerPath
115
+ });
116
+ const sdl = schemaComposer.toSDL();
117
+ const codegenConfig = {
118
+ documents: [],
119
+ config: {
120
+ declarationKind: {
121
+ type: "interface",
122
+ interface: "interface"
123
+ },
124
+ namingConvention: "keep"
125
+ },
126
+ filename: "schema/types.ts",
127
+ schema: parse(sdl),
128
+ plugins: [{
129
+ typescript: {}
130
+ }],
131
+ pluginMap: {
132
+ typescript: typescriptPlugin
133
+ }
134
+ };
135
+ await fs.promises.writeFile("schema/schema.graphql", sdl);
136
+ log.info(logPrefix, "Generating types...");
137
+ const typesOutput = await codegen(codegenConfig);
138
+ const typesOutputIgnore = ["/* eslint-disable */"].join("\n");
139
+ await fs.promises.writeFile("schema/types.ts", `${typesOutputIgnore}
140
+ ${typesOutput}`);
141
+ log.info(logPrefix, "Schema and types generated!");
142
+ };
143
+ var program = new Command();
144
+ program.name("ttoss-graphql-api").version(package_default.version, "-v, --version", "Output the current version of the GraphQL API");
145
+ program.command("build-schema").option("-d, --directory <directory>", "Schema composer directory", "src").option("--external <external...>", "External dependencies to ignore during build").action(options => {
146
+ return buildSchema(options);
147
+ });
148
+ program.parse(process.argv);
@@ -0,0 +1,2 @@
1
+
2
+ export { }
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@ttoss/graphql-api-cli",
3
+ "version": "0.8.1",
4
+ "description": "A library for building GraphQL APIs types and schema.",
5
+ "license": "MIT",
6
+ "author": "ttoss",
7
+ "contributors": [
8
+ "Pedro Arantes <pedro@arantespp.com> (https://arantespp.com)"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/ttoss/ttoss.git",
13
+ "directory": "packages/graphql-api-cli"
14
+ },
15
+ "type": "module",
16
+ "bin": {
17
+ "ttoss-graphql-api": "./bin/cli.js"
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "sideEffects": false,
23
+ "peerDependencies": {
24
+ "graphql": "^16.6.0"
25
+ },
26
+ "devDependencies": {
27
+ "@graphql-codegen/core": "^4.0.2",
28
+ "@graphql-codegen/typescript": "^4.1.2",
29
+ "commander": "^12.1.0",
30
+ "esbuild": "^0.24.0",
31
+ "graphql": "^16.9.0",
32
+ "npmlog": "^7.0.1",
33
+ "tsup": "^8.3.5",
34
+ "@ttoss/config": "^1.35.0"
35
+ },
36
+ "keywords": [
37
+ "api",
38
+ "graphql"
39
+ ],
40
+ "publishConfig": {
41
+ "access": "public",
42
+ "provenance": true
43
+ },
44
+ "scripts": {
45
+ "build-config": "tsup-node"
46
+ }
47
+ }