@ptdgrp/typedgql 1.0.0-beta.10
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 +45 -0
- package/README.md +121 -0
- package/README.zh-CN.md +113 -0
- package/dist/index.cjs +911 -0
- package/dist/index.d.cts +299 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +299 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +896 -0
- package/dist/index.mjs.map +1 -0
- package/dist/node.cjs +6 -0
- package/dist/node.d.cts +41 -0
- package/dist/node.d.cts.map +1 -0
- package/dist/node.d.mts +41 -0
- package/dist/node.d.mts.map +1 -0
- package/dist/node.mjs +3 -0
- package/dist/options-CaWo97vV.d.cts +99 -0
- package/dist/options-CaWo97vV.d.cts.map +1 -0
- package/dist/options-D2L-tv7C.d.mts +99 -0
- package/dist/options-D2L-tv7C.d.mts.map +1 -0
- package/dist/schema-loader-D4z1rOMz.mjs +1913 -0
- package/dist/schema-loader-D4z1rOMz.mjs.map +1 -0
- package/dist/schema-loader-dDSoNpTn.cjs +1929 -0
- package/dist/vite.cjs +148 -0
- package/dist/vite.d.cts +61 -0
- package/dist/vite.d.cts.map +1 -0
- package/dist/vite.d.mts +61 -0
- package/dist/vite.d.mts.map +1 -0
- package/dist/vite.mjs +148 -0
- package/dist/vite.mjs.map +1 -0
- package/package.json +87 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 tonitrnel
|
|
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.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
MIT License
|
|
26
|
+
|
|
27
|
+
Copyright (c) ChenTao (babyfish.ct@gmail.com)
|
|
28
|
+
|
|
29
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
30
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
31
|
+
in the Software without restriction, including without limitation the rights
|
|
32
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
33
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
34
|
+
furnished to do so, subject to the following conditions:
|
|
35
|
+
|
|
36
|
+
The above copyright notice and this permission notice shall be included in all
|
|
37
|
+
copies or substantial portions of the Software.
|
|
38
|
+
|
|
39
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
40
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
41
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
42
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
43
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
44
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
45
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# @ptdgrp/typedgql
|
|
2
|
+
|
|
3
|
+
[](https://github.com/tonitrnel/typedgql/actions/workflows/publish-npm.yml)
|
|
4
|
+
|
|
5
|
+
`typedgql` is a TypeScript-first GraphQL client codegen + runtime library focused on end-to-end type safety and a fluent query-building experience.
|
|
6
|
+
|
|
7
|
+
For Chinese documentation, see [README.zh-CN.md](./README.zh-CN.md).
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Generate strongly typed client code from a GraphQL schema
|
|
12
|
+
- Fluent DSL for queries and mutations, for example:
|
|
13
|
+
`G.query((q) => q.posts((p) => p.id.title))`
|
|
14
|
+
- Decouple selection building from variable values:
|
|
15
|
+
build once, pass `variables` at `execute(...)` time
|
|
16
|
+
- Zero third-party runtime dependency
|
|
17
|
+
(only depends on your GraphQL executor)
|
|
18
|
+
- Supports ESM/CJS
|
|
19
|
+
- Default output directory:
|
|
20
|
+
`node_modules/@ptdgrp/typedgql/__generated`
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm add @ptdgrp/typedgql
|
|
26
|
+
pnpm add -D graphql typescript
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
For advanced usage (Subscription, directives, GraphQL mapping), see:
|
|
32
|
+
|
|
33
|
+
- [Advanced Usage (Chinese)](./docs/advanced-usage.zh-CN.md)
|
|
34
|
+
|
|
35
|
+
### 1. Vite Plugin (Recommended)
|
|
36
|
+
|
|
37
|
+
Configure `vite.config.ts`:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { defineConfig } from "vite";
|
|
41
|
+
import { typedgql } from "@ptdgrp/typedgql/vite";
|
|
42
|
+
|
|
43
|
+
export default defineConfig({
|
|
44
|
+
plugins: [
|
|
45
|
+
typedgql({ schema: "./schema.graphql" }),
|
|
46
|
+
// or remote schema:
|
|
47
|
+
// typedgql({ schema: "http://localhost:4000/graphql" }),
|
|
48
|
+
],
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Codegen runs automatically when Vite starts, and re-runs when the schema changes.
|
|
53
|
+
|
|
54
|
+
### 2. Manual Generation in Node
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { Generator, loadLocalSchema } from "@ptdgrp/typedgql/node";
|
|
58
|
+
|
|
59
|
+
const generator = new Generator({
|
|
60
|
+
schemaLoader: () => loadLocalSchema("./schema.graphql"),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await generator.generate();
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 3. Runtime Execution (Basic Example)
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { G, execute, setGraphQLExecutor } from "@ptdgrp/typedgql";
|
|
70
|
+
|
|
71
|
+
setGraphQLExecutor(async (request, variables) => {
|
|
72
|
+
const res = await fetch("http://localhost:8080/graphql", {
|
|
73
|
+
method: "POST",
|
|
74
|
+
headers: { "Content-Type": "application/json" },
|
|
75
|
+
body: JSON.stringify({ query: request, variables }),
|
|
76
|
+
});
|
|
77
|
+
return res.json();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const selection = G.query((q) =>
|
|
81
|
+
q.posts((post) => post.id.title.author((author) => author.id.name)),
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const data = await execute(selection);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 4. Query With Variables (Recommended)
|
|
88
|
+
|
|
89
|
+
Selections are reusable. Pass variables when calling `execute(...)`.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { G, execute } from "@ptdgrp/typedgql";
|
|
93
|
+
|
|
94
|
+
const selection = G.query((q) => q.post((post) => post.id.title.content));
|
|
95
|
+
|
|
96
|
+
const data = await execute(selection, {
|
|
97
|
+
variables: { id: "p2" },
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 5. Explicit Variable Placeholder (Optional)
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { G, execute, ParameterRef } from "@ptdgrp/typedgql";
|
|
105
|
+
|
|
106
|
+
const selection = G.query((q) =>
|
|
107
|
+
q.post({ id: ParameterRef.of("postId") }, (post) => post.id.title),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const data = await execute(selection, {
|
|
111
|
+
variables: { postId: "p2" },
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
MIT. See [LICENSE](./LICENSE).
|
|
118
|
+
|
|
119
|
+
## Credits
|
|
120
|
+
|
|
121
|
+
This project evolves from ideas in [graphql-ts-client](https://github.com/babyfish-ct/graphql-ts-client). Thanks to [ChenTao](https://github.com/babyfish-ct) for the foundational work.
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# @ptdgrp/typedgql
|
|
2
|
+
|
|
3
|
+
`typedgql` 是一个面向 TypeScript 的 GraphQL 客户端代码生成与运行时库,目标是提供“端到端类型安全 + 链式查询构建体验”。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 基于 GraphQL Schema 生成强类型客户端代码
|
|
8
|
+
- 链式 DSL 构建查询与变更,例如 `G.query((q) => q.posts((p) => p.id.title))`
|
|
9
|
+
- 查询选择与请求变量分离:先构建 selection,再在 `execute` 时传 `variables`
|
|
10
|
+
- 零运行时三方依赖(仅依赖你提供的 GraphQL executor)
|
|
11
|
+
- 支持 ESM/CJS
|
|
12
|
+
- 默认生成到 `node_modules/@ptdgrp/typedgql/__generated`
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pnpm add @ptdgrp/typedgql
|
|
18
|
+
pnpm add -D graphql typescript
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 用法
|
|
22
|
+
|
|
23
|
+
进阶内容(Subscription、指令、GraphQL 对照)见:
|
|
24
|
+
|
|
25
|
+
- [typedgql 进阶用法(中文)](./docs/advanced-usage.zh-CN.md)
|
|
26
|
+
|
|
27
|
+
### 1. Vite 插件方式(推荐)
|
|
28
|
+
|
|
29
|
+
在 `vite.config.ts` 中配置:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { defineConfig } from "vite";
|
|
33
|
+
import { typedgql } from "@ptdgrp/typedgql/vite";
|
|
34
|
+
|
|
35
|
+
export default defineConfig({
|
|
36
|
+
plugins: [
|
|
37
|
+
typedgql({ schema: "./schema.graphql" }),
|
|
38
|
+
// 或远程 schema:
|
|
39
|
+
// typedgql({ schema: "http://localhost:4000/graphql" }),
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
启动 Vite 时会自动生成代码;schema 变更后会自动重新生成。
|
|
45
|
+
|
|
46
|
+
### 2. Node 手动生成
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import { Generator, loadLocalSchema } from "@ptdgrp/typedgql/node";
|
|
50
|
+
|
|
51
|
+
const generator = new Generator({
|
|
52
|
+
schemaLoader: () => loadLocalSchema("./schema.graphql"),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
await generator.generate();
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 3. 运行时执行(基础示例)
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { G, execute, setGraphQLExecutor } from "@ptdgrp/typedgql";
|
|
62
|
+
|
|
63
|
+
setGraphQLExecutor(async (request, variables) => {
|
|
64
|
+
const res = await fetch("http://localhost:8080/graphql", {
|
|
65
|
+
method: "POST",
|
|
66
|
+
headers: { "Content-Type": "application/json" },
|
|
67
|
+
body: JSON.stringify({ query: request, variables }),
|
|
68
|
+
});
|
|
69
|
+
return res.json();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const selection = G.query((q) =>
|
|
73
|
+
q.posts((post) => post.id.title.author((author) => author.id.name)),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const data = await execute(selection);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 4. 带变量查询(推荐写法)
|
|
80
|
+
|
|
81
|
+
`selection` 与变量传值解耦:selection 可复用,变量在执行时传入。
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { G, execute } from "@ptdgrp/typedgql";
|
|
85
|
+
|
|
86
|
+
const selection = G.query((q) => q.post((post) => post.id.title.content));
|
|
87
|
+
|
|
88
|
+
const data = await execute(selection, {
|
|
89
|
+
variables: { id: "p2" },
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 5. 显式变量占位(可选)
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { G, execute, ParameterRef } from "@ptdgrp/typedgql";
|
|
97
|
+
|
|
98
|
+
const selection = G.query((q) =>
|
|
99
|
+
q.post({ id: ParameterRef.of("postId") }, (post) => post.id.title),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const data = await execute(selection, {
|
|
103
|
+
variables: { postId: "p2" },
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
MIT,详见 [LICENSE](./LICENSE)。
|
|
110
|
+
|
|
111
|
+
## Credits
|
|
112
|
+
|
|
113
|
+
本项目基于 [graphql-ts-client](https://github.com/babyfish-ct/graphql-ts-client) 的设计思路演进,感谢 [ChenTao](https://github.com/babyfish-ct) 提供的优秀基础。
|