@sortlist/cli 0.29.0 → 0.30.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/package.json +1 -1
- package/src/commands/dynamic.ts +95 -6
- package/src/index.ts +23 -1
package/package.json
CHANGED
package/src/commands/dynamic.ts
CHANGED
|
@@ -12,6 +12,8 @@ import {
|
|
|
12
12
|
isInterfaceType,
|
|
13
13
|
isListType,
|
|
14
14
|
isNonNullType,
|
|
15
|
+
isObjectType,
|
|
16
|
+
isScalarType,
|
|
15
17
|
isUnionType,
|
|
16
18
|
type GraphQLArgument,
|
|
17
19
|
type GraphQLEnumType,
|
|
@@ -86,12 +88,13 @@ function getInputFields(field: GraphQLField<unknown, unknown>): CliArg[] | null
|
|
|
86
88
|
}
|
|
87
89
|
|
|
88
90
|
/**
|
|
89
|
-
* Render a GraphQL
|
|
90
|
-
* `[OrganizationSortInput!]`, `OrganizationFilterInput`.
|
|
91
|
+
* Render a GraphQL type as its SDL signature, e.g. `String!`,
|
|
92
|
+
* `[OrganizationSortInput!]`, `OrganizationFilterInput`. Works for both input
|
|
93
|
+
* and output types (the wrapper structure is identical).
|
|
91
94
|
*/
|
|
92
|
-
function renderType(type: GraphQLInputType): string {
|
|
93
|
-
if (isNonNullType(type)) return `${renderType(type.ofType as GraphQLInputType)}!`;
|
|
94
|
-
if (isListType(type)) return `[${renderType(type.ofType as GraphQLInputType)}]`;
|
|
95
|
+
function renderType(type: GraphQLInputType | GraphQLOutputType): string {
|
|
96
|
+
if (isNonNullType(type)) return `${renderType(type.ofType as GraphQLInputType | GraphQLOutputType)}!`;
|
|
97
|
+
if (isListType(type)) return `[${renderType(type.ofType as GraphQLInputType | GraphQLOutputType)}]`;
|
|
95
98
|
return (type as GraphQLNamedType).name;
|
|
96
99
|
}
|
|
97
100
|
|
|
@@ -152,6 +155,85 @@ function describeStructuredType(named: GraphQLNamedType): string | null {
|
|
|
152
155
|
return null;
|
|
153
156
|
}
|
|
154
157
|
|
|
158
|
+
/** A named type worth pointing the user at via `sl schema inspect` — i.e. one with structure. */
|
|
159
|
+
function isInspectable(named: GraphQLNamedType): boolean {
|
|
160
|
+
return isInputObjectType(named) || isEnumType(named) || isObjectType(named);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function oneLineDescription(text: string | null | undefined): string {
|
|
164
|
+
return (text ?? '').replace(/\s+/g, ' ').trim();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Render a full, human-readable description of a named type for
|
|
169
|
+
* `sl schema inspect <Type>`: its kind, description, and one level of
|
|
170
|
+
* structure (object/input fields with their types + descriptions, or enum
|
|
171
|
+
* values). Nested structured types are named so the caller can `inspect` them
|
|
172
|
+
* in turn. Returns null if the type does not exist in the schema.
|
|
173
|
+
*/
|
|
174
|
+
export function formatTypeInspection(schema: GraphQLSchema, typeName: string): string | null {
|
|
175
|
+
const named = schema.getType(typeName);
|
|
176
|
+
if (!named) return null;
|
|
177
|
+
|
|
178
|
+
const lines: string[] = [];
|
|
179
|
+
const suffix = oneLineDescription(named.description);
|
|
180
|
+
const header = (kind: string): string => {
|
|
181
|
+
const label = kind ? `${kind} ${named.name}` : named.name;
|
|
182
|
+
return suffix ? `${label} — ${suffix}` : label;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const pushFields = (entries: Array<{ name: string; typeSig: string; description?: string | null }>): void => {
|
|
186
|
+
const width = Math.max(...entries.map((entry) => `${entry.name}: ${entry.typeSig}`.length), 0);
|
|
187
|
+
for (const entry of entries) {
|
|
188
|
+
const signature = `${entry.name}: ${entry.typeSig}`;
|
|
189
|
+
const note = oneLineDescription(entry.description);
|
|
190
|
+
lines.push(note ? ` ${signature.padEnd(width)} ${note}` : ` ${signature}`);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
if (isInputObjectType(named) || isObjectType(named)) {
|
|
195
|
+
lines.push(header(isInputObjectType(named) ? 'input' : 'type'));
|
|
196
|
+
pushFields(
|
|
197
|
+
Object.values(named.getFields()).map((f) => ({
|
|
198
|
+
name: f.name,
|
|
199
|
+
typeSig: renderType(f.type),
|
|
200
|
+
description: f.description,
|
|
201
|
+
})),
|
|
202
|
+
);
|
|
203
|
+
} else if (isEnumType(named)) {
|
|
204
|
+
lines.push(header('enum'));
|
|
205
|
+
for (const value of named.getValues()) {
|
|
206
|
+
const note = oneLineDescription(value.description);
|
|
207
|
+
lines.push(note ? ` ${value.name.padEnd(20)} ${note}` : ` ${value.name}`);
|
|
208
|
+
}
|
|
209
|
+
} else if (isScalarType(named)) {
|
|
210
|
+
lines.push(header('scalar'));
|
|
211
|
+
lines.push(' (scalar — pass a plain value)');
|
|
212
|
+
} else {
|
|
213
|
+
lines.push(header(''));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Point the caller at the nested types they can drill into next.
|
|
217
|
+
const nested = new Set<string>();
|
|
218
|
+
if (isInputObjectType(named) || isObjectType(named)) {
|
|
219
|
+
for (const field of Object.values(named.getFields())) {
|
|
220
|
+
const fieldType = unwrap(field.type);
|
|
221
|
+
if (isInspectable(fieldType) && fieldType.name !== named.name) nested.add(fieldType.name);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (nested.size > 0) {
|
|
225
|
+
lines.push('');
|
|
226
|
+
lines.push(
|
|
227
|
+
`Inspect nested types: ${[...nested]
|
|
228
|
+
.sort()
|
|
229
|
+
.map((n) => `sl schema inspect ${n}`)
|
|
230
|
+
.join(', ')}`,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return lines.join('\n');
|
|
235
|
+
}
|
|
236
|
+
|
|
155
237
|
/**
|
|
156
238
|
* Build the help description for a single CLI option from its GraphQL arg:
|
|
157
239
|
* the field description (if any), the rendered type with a `(JSON)` marker for
|
|
@@ -166,9 +248,16 @@ function describeArg(arg: { name: string; type: GraphQLInputType; description?:
|
|
|
166
248
|
parts.push(isStructuredInput(arg.type) ? `${typeSig} (JSON)` : typeSig);
|
|
167
249
|
|
|
168
250
|
// Shape hint applies to enums (raw string) and input objects (JSON) alike.
|
|
169
|
-
const
|
|
251
|
+
const named = unwrap(arg.type);
|
|
252
|
+
const shape = describeStructuredType(named);
|
|
170
253
|
if (shape) parts.push(shape);
|
|
171
254
|
|
|
255
|
+
// Point at `schema inspect` so nested input/enum types can be drilled into
|
|
256
|
+
// without resorting to raw introspection.
|
|
257
|
+
if (isInputObjectType(named) || isEnumType(named)) {
|
|
258
|
+
parts.push(`details: sl schema inspect ${named.name}`);
|
|
259
|
+
}
|
|
260
|
+
|
|
172
261
|
return parts.join(' — ');
|
|
173
262
|
}
|
|
174
263
|
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Command } from 'commander';
|
|
|
3
3
|
|
|
4
4
|
// import { chatCommand } from './commands/chat/chat.ts';
|
|
5
5
|
import { configCommand } from './commands/config.ts';
|
|
6
|
-
import { buildServiceCommands } from './commands/dynamic.ts';
|
|
6
|
+
import { buildServiceCommands, formatTypeInspection } from './commands/dynamic.ts';
|
|
7
7
|
import { loginCommand } from './commands/login.ts';
|
|
8
8
|
import { logoutCommand } from './commands/logout.ts';
|
|
9
9
|
import { offerCommand } from './commands/offer.ts';
|
|
@@ -120,6 +120,28 @@ schemaCommand
|
|
|
120
120
|
}
|
|
121
121
|
});
|
|
122
122
|
|
|
123
|
+
schemaCommand
|
|
124
|
+
.command('inspect <type>')
|
|
125
|
+
.description('Show the fields/values of a schema type (e.g. an input filter or enum)')
|
|
126
|
+
.action(async function (this: Command, typeName: string) {
|
|
127
|
+
const opts = this.optsWithGlobals();
|
|
128
|
+
try {
|
|
129
|
+
const { schema } = await getIntrospectedSchema(opts.endpoint);
|
|
130
|
+
const formatted = formatTypeInspection(schema, typeName);
|
|
131
|
+
if (!formatted) {
|
|
132
|
+
error(`No type named "${typeName}" in the schema.`);
|
|
133
|
+
hint('Type names are case-sensitive, e.g. `sl schema inspect OrganizationFilterInput`.');
|
|
134
|
+
hint('Find argument types in any command’s --help output.');
|
|
135
|
+
process.exit(1);
|
|
136
|
+
}
|
|
137
|
+
info(formatted);
|
|
138
|
+
} catch (err) {
|
|
139
|
+
error(`Could not read the schema: ${(err as Error).message}`);
|
|
140
|
+
hint('Check your network connection and endpoint: sl config show');
|
|
141
|
+
process.exit(1);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
123
145
|
program.addCommand(schemaCommand);
|
|
124
146
|
|
|
125
147
|
// Parse --endpoint early so introspection uses the right URL
|