@jackchuka/sdl-decompose 1.0.0
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 +238 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +75 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +146 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +15 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
# SDL Decompose
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/%40jackchuka%2Fsdl-decompose)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
A powerful tool to decompose GraphQL SDL (Schema Definition Language) by operation name, producing a minimal SDL containing only the types and fields needed for a specific operation.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- 🎯 **Precise decomposition**: Extract only the types needed for a specific GraphQL operation
|
|
11
|
+
- 📁 **File or stdin input**: Read SDL from files or pipe it through stdin
|
|
12
|
+
- 🔄 **All operation types**: Support for queries, mutations, and subscriptions
|
|
13
|
+
- 📝 **TypeScript support**: Full TypeScript definitions included
|
|
14
|
+
- 🛠️ **CLI and programmatic API**: Use from command line or integrate into your code
|
|
15
|
+
- ⚡ **Fast and lightweight**: Minimal dependencies, built on top of `graphql-js`
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# Global installation
|
|
21
|
+
npm install -g @jackchuka/sdl-decompose
|
|
22
|
+
|
|
23
|
+
# Or use with npx (no installation required)
|
|
24
|
+
npx @jackchuka/sdl-decompose --help
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## CLI Usage
|
|
28
|
+
|
|
29
|
+
### Basic Usage
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# From file
|
|
33
|
+
npx @jackchuka/sdl-decompose --sdl schema.graphql --operation getUser
|
|
34
|
+
|
|
35
|
+
# From stdin
|
|
36
|
+
cat schema.graphql | npx @jackchuka/sdl-decompose --operation getUser
|
|
37
|
+
|
|
38
|
+
# With mutation
|
|
39
|
+
npx @jackchuka/sdl-decompose --sdl schema.graphql --operation createUser --type mutation
|
|
40
|
+
|
|
41
|
+
# Save to file
|
|
42
|
+
npx @jackchuka/sdl-decompose --sdl schema.graphql --operation getUser --output partial.graphql
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Options
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
Usage: sdl-decompose [options]
|
|
49
|
+
|
|
50
|
+
Options:
|
|
51
|
+
-V, --version output the version number
|
|
52
|
+
-s, --sdl <file> Path to SDL file (optional, reads from stdin if not provided)
|
|
53
|
+
-o, --operation <name> Operation name to decompose (required)
|
|
54
|
+
-t, --type <type> Operation type: query, mutation, subscription (default: "query")
|
|
55
|
+
--output <file> Output file path (optional, prints to stdout if not provided)
|
|
56
|
+
--include-builtins Include builtin scalar types in output (default: false)
|
|
57
|
+
-h, --help display help for command
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Examples
|
|
61
|
+
|
|
62
|
+
#### Example 1: Basic Query Decomposition
|
|
63
|
+
|
|
64
|
+
Given this schema:
|
|
65
|
+
```graphql
|
|
66
|
+
type Query {
|
|
67
|
+
getUser(id: ID!): User
|
|
68
|
+
getPost(id: ID!): Post
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
type User {
|
|
72
|
+
id: ID!
|
|
73
|
+
name: String!
|
|
74
|
+
email: String!
|
|
75
|
+
posts: [Post!]!
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type Post {
|
|
79
|
+
id: ID!
|
|
80
|
+
title: String!
|
|
81
|
+
content: String!
|
|
82
|
+
author: User!
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Running:
|
|
87
|
+
```bash
|
|
88
|
+
npx @jackchuka/sdl-decompose --sdl schema.graphql --operation getUser
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Outputs:
|
|
92
|
+
```graphql
|
|
93
|
+
type Query {
|
|
94
|
+
getUser(id: ID!): User
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
type User {
|
|
98
|
+
id: ID!
|
|
99
|
+
name: String!
|
|
100
|
+
email: String!
|
|
101
|
+
posts: [Post!]!
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
type Post {
|
|
105
|
+
id: ID!
|
|
106
|
+
title: String!
|
|
107
|
+
content: String!
|
|
108
|
+
author: User!
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### Example 2: Mutation with Output File
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
npx @jackchuka/sdl-decompose --sdl schema.graphql --operation createUser --type mutation --output create-user.graphql
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### Example 3: Using with Pipes
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
# Download schema and decompose in one command
|
|
122
|
+
curl -s https://api.example.com/graphql/schema | npx @jackchuka/sdl-decompose --operation getUser
|
|
123
|
+
|
|
124
|
+
# Process multiple operations
|
|
125
|
+
for op in getUser getPost; do
|
|
126
|
+
npx @jackchuka/sdl-decompose --sdl schema.graphql --operation $op --output "${op}.graphql"
|
|
127
|
+
done
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Programmatic API
|
|
131
|
+
|
|
132
|
+
### Installation
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
npm install @jackchuka/sdl-decompose
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Usage
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
import { decomposeGraphQL } from '@jackchuka/sdl-decompose';
|
|
142
|
+
|
|
143
|
+
const fullSDL = `
|
|
144
|
+
type Query {
|
|
145
|
+
getUser(id: ID!): User
|
|
146
|
+
getPost(id: ID!): Post
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
type User {
|
|
150
|
+
id: ID!
|
|
151
|
+
name: String!
|
|
152
|
+
posts: [Post!]!
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
type Post {
|
|
156
|
+
id: ID!
|
|
157
|
+
title: String!
|
|
158
|
+
author: User!
|
|
159
|
+
}
|
|
160
|
+
`;
|
|
161
|
+
|
|
162
|
+
const result = decomposeGraphQL(fullSDL, 'getUser', 'query', {
|
|
163
|
+
includeBuiltinScalars: false
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
console.log(result.sdl);
|
|
167
|
+
console.log('Collected types:', Array.from(result.collectedTypes));
|
|
168
|
+
console.log('Operation found:', result.operationFound);
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### API Reference
|
|
172
|
+
|
|
173
|
+
#### `decomposeGraphQL(fullSDL, operationName, operationType?, options?)`
|
|
174
|
+
|
|
175
|
+
**Parameters:**
|
|
176
|
+
- `fullSDL` (string): The complete GraphQL SDL
|
|
177
|
+
- `operationName` (string): Name of the operation to decompose
|
|
178
|
+
- `operationType` (string, optional): Type of operation - `'query'`, `'mutation'`, or `'subscription'`. Defaults to `'query'`
|
|
179
|
+
- `options` (object, optional): Configuration options
|
|
180
|
+
|
|
181
|
+
**Options:**
|
|
182
|
+
- `includeBuiltinScalars` (boolean): Include built-in scalar types (String, Int, Float, Boolean, ID) in the output. Defaults to `false`
|
|
183
|
+
|
|
184
|
+
**Returns:**
|
|
185
|
+
```typescript
|
|
186
|
+
interface DecomposeResult {
|
|
187
|
+
sdl: string; // The decomposed SDL
|
|
188
|
+
collectedTypes: Set<string>; // Set of type names that were collected
|
|
189
|
+
operationFound: boolean; // Whether the operation was found
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### TypeScript Types
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
interface DecomposeOptions {
|
|
197
|
+
includeBuiltinScalars?: boolean;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
interface DecomposeResult {
|
|
201
|
+
sdl: string;
|
|
202
|
+
collectedTypes: Set<string>;
|
|
203
|
+
operationFound: boolean;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
type OperationType = 'query' | 'mutation' | 'subscription';
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Use Cases
|
|
210
|
+
|
|
211
|
+
- **Schema Federation**: Extract specific operations for federated services
|
|
212
|
+
- **Code Generation**: Generate types for specific operations only
|
|
213
|
+
- **Testing**: Create minimal schemas for testing specific functionality
|
|
214
|
+
- **Documentation**: Generate focused schema documentation
|
|
215
|
+
- **Bundle Size Optimization**: Include only necessary schema parts in client bundles
|
|
216
|
+
- **API Development**: Develop and test individual operations in isolation
|
|
217
|
+
|
|
218
|
+
## How It Works
|
|
219
|
+
|
|
220
|
+
SDL Decompose analyzes your GraphQL schema and:
|
|
221
|
+
|
|
222
|
+
1. **Finds the target operation** in the appropriate root type (Query, Mutation, or Subscription)
|
|
223
|
+
2. **Traverses the type graph** starting from the operation's return type and arguments
|
|
224
|
+
3. **Collects all referenced types** including nested types, input types, and union/interface implementations
|
|
225
|
+
4. **Reconstructs minimal SDL** containing only the necessary types and the target operation
|
|
226
|
+
|
|
227
|
+
## Contributing
|
|
228
|
+
|
|
229
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
230
|
+
|
|
231
|
+
## License
|
|
232
|
+
|
|
233
|
+
MIT © [jackchuka](https://github.com/jackchuka)
|
|
234
|
+
|
|
235
|
+
## Related Projects
|
|
236
|
+
|
|
237
|
+
- [GraphQL Tools](https://www.graphql-tools.com/) - Comprehensive GraphQL utilities
|
|
238
|
+
- [GraphQL Code Generator](https://www.graphql-code-generator.com/) - Generate code from GraphQL schemas
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const commander_1 = require("commander");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const index_1 = require("./index");
|
|
8
|
+
const program = new commander_1.Command();
|
|
9
|
+
async function readSDL(sdlFile) {
|
|
10
|
+
if (sdlFile) {
|
|
11
|
+
const sdlPath = (0, path_1.resolve)(sdlFile);
|
|
12
|
+
if (!(0, fs_1.existsSync)(sdlPath)) {
|
|
13
|
+
throw new Error(`SDL file not found: ${sdlPath}`);
|
|
14
|
+
}
|
|
15
|
+
return (0, fs_1.readFileSync)(sdlPath, 'utf8');
|
|
16
|
+
}
|
|
17
|
+
// Read from stdin
|
|
18
|
+
const chunks = [];
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
process.stdin.on('data', (chunk) => {
|
|
21
|
+
chunks.push(chunk);
|
|
22
|
+
});
|
|
23
|
+
process.stdin.on('end', () => {
|
|
24
|
+
const data = Buffer.concat(chunks).toString('utf8');
|
|
25
|
+
if (!data.trim()) {
|
|
26
|
+
reject(new Error('No SDL content provided via stdin'));
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
resolve(data);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
process.stdin.on('error', reject);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
program
|
|
36
|
+
.name('sdl-decompose')
|
|
37
|
+
.description('Decompose GraphQL SDL by operation name to produce partial SDL')
|
|
38
|
+
.version('1.0.0')
|
|
39
|
+
.option('-s, --sdl <file>', 'Path to SDL file (optional, reads from stdin if not provided)')
|
|
40
|
+
.requiredOption('-o, --operation <name>', 'Operation name to decompose')
|
|
41
|
+
.option('-t, --type <type>', 'Operation type: query, mutation, subscription', 'query')
|
|
42
|
+
.option('--output <file>', 'Output file path (optional, prints to stdout if not provided)')
|
|
43
|
+
.option('--include-builtins', 'Include builtin scalar types in output', false)
|
|
44
|
+
.action(async (options) => {
|
|
45
|
+
const { sdl: sdlFile, operation: operationName, type: operationType, output: outputFile, includeBuiltins } = options;
|
|
46
|
+
// Validate operation type
|
|
47
|
+
if (!['query', 'mutation', 'subscription'].includes(operationType)) {
|
|
48
|
+
console.error('Error: --type must be one of: query, mutation, subscription');
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const fullSDL = await readSDL(sdlFile);
|
|
53
|
+
const result = (0, index_1.decomposeGraphQL)(fullSDL, operationName, operationType, {
|
|
54
|
+
includeBuiltinScalars: includeBuiltins
|
|
55
|
+
});
|
|
56
|
+
if (!result.operationFound) {
|
|
57
|
+
console.error(`Error: Operation '${operationName}' not found in ${operationType} type`);
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
if (outputFile) {
|
|
61
|
+
(0, fs_1.writeFileSync)(outputFile, result.sdl);
|
|
62
|
+
console.log(`Decomposed SDL written to: ${outputFile}`);
|
|
63
|
+
console.log(`Collected types: ${Array.from(result.collectedTypes).join(', ')}`);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
console.log(result.sdl);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
console.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
program.parse();
|
|
75
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";;;AAE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CAAC;YACtC,OAAO,CAAC,GAAG,CAAC,8BAA8B,UAAU,EAAE,CAAC,CAAC;YACxD,OAAO,CAAC,GAAG,CAAC,oBAAoB,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAClF,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1B,CAAC;IAEH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,UAAU,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAClF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC,CAAC,CAAC;AAEL,OAAO,CAAC,KAAK,EAAE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { DecomposeOptions, DecomposeResult, OperationType } from './types';
|
|
2
|
+
export declare function decomposeGraphQL(fullSDL: string, operationName: string, operationType?: OperationType, options?: DecomposeOptions): DecomposeResult;
|
|
3
|
+
export * from './types';
|
|
4
|
+
export default decomposeGraphQL;
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,aAAa,EAAiB,MAAM,SAAS,CAAC;AAI1F,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,MAAM,EACf,aAAa,EAAE,MAAM,EACrB,aAAa,GAAE,aAAuB,EACtC,OAAO,GAAE,gBAAqB,GAC7B,eAAe,CAsCjB;AAmHD,cAAc,SAAS,CAAC;AACxB,eAAe,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.decomposeGraphQL = decomposeGraphQL;
|
|
18
|
+
const graphql_1 = require("graphql");
|
|
19
|
+
const BUILTIN_SCALARS = new Set(['String', 'Int', 'Float', 'Boolean', 'ID']);
|
|
20
|
+
function decomposeGraphQL(fullSDL, operationName, operationType = 'query', options = {}) {
|
|
21
|
+
try {
|
|
22
|
+
const schema = (0, graphql_1.buildSchema)(fullSDL);
|
|
23
|
+
const collector = {
|
|
24
|
+
collected: new Set(),
|
|
25
|
+
typeNames: new Set()
|
|
26
|
+
};
|
|
27
|
+
const rootType = getRootType(schema, operationType);
|
|
28
|
+
if (!rootType) {
|
|
29
|
+
return {
|
|
30
|
+
sdl: '',
|
|
31
|
+
collectedTypes: new Set(),
|
|
32
|
+
operationFound: false
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const field = rootType.getFields()[operationName];
|
|
36
|
+
if (!field) {
|
|
37
|
+
return {
|
|
38
|
+
sdl: '',
|
|
39
|
+
collectedTypes: new Set(),
|
|
40
|
+
operationFound: false
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
collectTypesFromField(field, collector, options);
|
|
44
|
+
const partialSDL = reconstructSDL(schema, collector, operationType, operationName, options);
|
|
45
|
+
return {
|
|
46
|
+
sdl: partialSDL,
|
|
47
|
+
collectedTypes: collector.typeNames,
|
|
48
|
+
operationFound: true
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
throw new Error(`Failed to decompose GraphQL: ${error instanceof Error ? error.message : String(error)}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function getRootType(schema, operationType) {
|
|
56
|
+
switch (operationType) {
|
|
57
|
+
case 'query':
|
|
58
|
+
return schema.getQueryType();
|
|
59
|
+
case 'mutation':
|
|
60
|
+
return schema.getMutationType();
|
|
61
|
+
case 'subscription':
|
|
62
|
+
return schema.getSubscriptionType();
|
|
63
|
+
default:
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function collectTypesFromField(field, collector, options) {
|
|
68
|
+
const fieldType = (0, graphql_1.getNamedType)(field.type);
|
|
69
|
+
collectTypes(fieldType, collector, options);
|
|
70
|
+
if (field.args) {
|
|
71
|
+
for (const arg of field.args) {
|
|
72
|
+
const argType = (0, graphql_1.getNamedType)(arg.type);
|
|
73
|
+
collectTypes(argType, collector, options);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function collectTypes(type, collector, options) {
|
|
78
|
+
if (!type || collector.collected.has(type))
|
|
79
|
+
return;
|
|
80
|
+
if (!options.includeBuiltinScalars && BUILTIN_SCALARS.has(type.name)) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
collector.collected.add(type);
|
|
84
|
+
collector.typeNames.add(type.name);
|
|
85
|
+
if ((0, graphql_1.isObjectType)(type) || (0, graphql_1.isInputObjectType)(type)) {
|
|
86
|
+
const fields = type.getFields();
|
|
87
|
+
for (const fieldObj of Object.values(fields)) {
|
|
88
|
+
const fieldType = (0, graphql_1.getNamedType)(fieldObj.type);
|
|
89
|
+
collectTypes(fieldType, collector, options);
|
|
90
|
+
if ('args' in fieldObj && fieldObj.args) {
|
|
91
|
+
for (const arg of fieldObj.args) {
|
|
92
|
+
const argType = (0, graphql_1.getNamedType)(arg.type);
|
|
93
|
+
collectTypes(argType, collector, options);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else if ((0, graphql_1.isInterfaceType)(type)) {
|
|
99
|
+
const fields = type.getFields();
|
|
100
|
+
for (const fieldObj of Object.values(fields)) {
|
|
101
|
+
const fieldType = (0, graphql_1.getNamedType)(fieldObj.type);
|
|
102
|
+
collectTypes(fieldType, collector, options);
|
|
103
|
+
if (fieldObj.args) {
|
|
104
|
+
for (const arg of fieldObj.args) {
|
|
105
|
+
const argType = (0, graphql_1.getNamedType)(arg.type);
|
|
106
|
+
collectTypes(argType, collector, options);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if ((0, graphql_1.isUnionType)(type)) {
|
|
112
|
+
for (const unionType of type.getTypes()) {
|
|
113
|
+
collectTypes(unionType, collector, options);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function reconstructSDL(schema, collector, operationType, operationName, _options) {
|
|
118
|
+
const typeDefs = [];
|
|
119
|
+
const rootType = getRootType(schema, operationType);
|
|
120
|
+
if (rootType) {
|
|
121
|
+
const field = rootType.getFields()[operationName];
|
|
122
|
+
if (field) {
|
|
123
|
+
const rootTypeName = operationType === 'query' ? 'Query' :
|
|
124
|
+
operationType === 'mutation' ? 'Mutation' : 'Subscription';
|
|
125
|
+
typeDefs.push(`type ${rootTypeName} {
|
|
126
|
+
${operationName}${printFieldSignature(field)}
|
|
127
|
+
}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
for (const type of collector.collected) {
|
|
131
|
+
if (type.name === 'Query' || type.name === 'Mutation' || type.name === 'Subscription') {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
typeDefs.push((0, graphql_1.printType)(type));
|
|
135
|
+
}
|
|
136
|
+
return typeDefs.join('\n\n');
|
|
137
|
+
}
|
|
138
|
+
function printFieldSignature(field) {
|
|
139
|
+
const args = field.args && field.args.length > 0
|
|
140
|
+
? `(${field.args.map((arg) => `${arg.name}: ${arg.type}`).join(', ')})`
|
|
141
|
+
: '';
|
|
142
|
+
return `${args}: ${field.type}`;
|
|
143
|
+
}
|
|
144
|
+
__exportStar(require("./types"), exports);
|
|
145
|
+
exports.default = decomposeGraphQL;
|
|
146
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAmBA,4CA2CC;AA9DD,qCAaiB;AAIjB,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC;AAE7E,SAAgB,gBAAgB,CAC9B,OAAe,EACf,aAAqB,EACrB,gBAA+B,OAAO,EACtC,UAA4B,EAAE;IAE9B,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAA,qBAAW,EAAC,OAAO,CAAC,CAAC;QACpC,MAAM,SAAS,GAAkB;YAC/B,SAAS,EAAE,IAAI,GAAG,EAAE;YACpB,SAAS,EAAE,IAAI,GAAG,EAAE;SACrB,CAAC;QAEF,MAAM,QAAQ,GAAG,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;QACpD,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,OAAO;gBACL,GAAG,EAAE,EAAE;gBACP,cAAc,EAAE,IAAI,GAAG,EAAE;gBACzB,cAAc,EAAE,K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|
|
1
|
+
import { GraphQLNamedType } from "graphql";
|
|
2
|
+
export interface DecomposeOptions {
|
|
3
|
+
includeBuiltinScalars?: boolean;
|
|
4
|
+
}
|
|
5
|
+
export interface DecomposeResult {
|
|
6
|
+
sdl: string;
|
|
7
|
+
collectedTypes: Set<string>;
|
|
8
|
+
operationFound: boolean;
|
|
9
|
+
}
|
|
10
|
+
export type OperationType = "query" | "mutation" | "subscription";
|
|
11
|
+
export interface TypeCollector {
|
|
12
|
+
collected: Set<GraphQLNamedType>;
|
|
13
|
+
typeNames: Set<string>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C,MAAM,WAAW,gBAAgB;IAC/B,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC;AAED,MAAM,WAAW,eAAe;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,cAAc,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAC5B,cAAc,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,UAAU,GAAG,cAAc,CAAC;AAElE,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,GAAG,CAAC,gBAAgB,CAAC,CAAC;IACjC,SAAS,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CACxB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jackchuka/sdl-decompose",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Decompose GraphQL SDL by operation name to produce partial SDL",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"dev": "tsc --watch",
|
|
10
|
+
"test": "jest",
|
|
11
|
+
"prepublishOnly": "npm run build"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"graphql",
|
|
15
|
+
"sdl",
|
|
16
|
+
"decompose",
|
|
17
|
+
"schema"
|
|
18
|
+
],
|
|
19
|
+
"author": "jackchuka",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"commander": "^14.0.0",
|
|
23
|
+
"graphql": "^16.8.1"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/jest": "^29.0.0",
|
|
27
|
+
"@types/node": "^20.19.1",
|
|
28
|
+
"jest": "^29.0.0",
|
|
29
|
+
"ts-jest": "^29.4.0",
|
|
30
|
+
"typescript": "^5.0.0"
|
|
31
|
+
},
|
|
32
|
+
"bin": {
|
|
33
|
+
"sdl-decompose": "./dist/cli.js"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist/**/*"
|
|
37
|
+
]
|
|
38
|
+
}
|