@sortlist/cli 0.35.0 → 0.36.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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sortlist/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.0",
|
|
4
4
|
"description": "Sortlist platform CLI — GraphQL federation client",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -22,16 +22,17 @@
|
|
|
22
22
|
"codegen": "graphql-codegen --config src/codegen.ts",
|
|
23
23
|
"generate": "npm run codegen",
|
|
24
24
|
"type-check": "tsc --noEmit",
|
|
25
|
+
"test": "vitest run",
|
|
25
26
|
"lint": "eslint . --ext .ts,.tsx,.js,.cjs --quiet",
|
|
26
27
|
"format": "prettier --write \"src/**/*.{ts,tsx,js,cjs,json}\"",
|
|
27
28
|
"format:check": "prettier --check \"src/**/*.{ts,tsx,js,cjs,json}\"",
|
|
28
29
|
"prepare": "[ -n \"$CI\" ] && exit 0; command -v git >/dev/null 2>&1 && git config --unset core.hooksPath || true; command -v lefthook >/dev/null 2>&1 && lefthook install || true"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@sentry/node": "^10.29.0",
|
|
32
32
|
"@graphql-codegen/cli": "^6.2.1",
|
|
33
33
|
"@graphql-codegen/client-preset": "^5.2.4",
|
|
34
34
|
"@graphql-typed-document-node/core": "^3.2.0",
|
|
35
|
+
"@sentry/node": "^10.29.0",
|
|
35
36
|
"commander": "^13.0.0",
|
|
36
37
|
"graphql": "^16.13.2",
|
|
37
38
|
"tsx": "^4.19.0"
|
|
@@ -42,7 +43,7 @@
|
|
|
42
43
|
"@types/node": "^22.10.0",
|
|
43
44
|
"@typescript-eslint/eslint-plugin": "^8.13.0",
|
|
44
45
|
"@typescript-eslint/parser": "^8.13.0",
|
|
45
|
-
"branch-name-lint": "^
|
|
46
|
+
"branch-name-lint": "^3.0.1",
|
|
46
47
|
"commitlint": "^18.6.1",
|
|
47
48
|
"eslint": "^8.56.0",
|
|
48
49
|
"eslint-config-prettier": "^9.1.0",
|
|
@@ -52,6 +53,7 @@
|
|
|
52
53
|
"eslint-plugin-sonarjs": "^0.24.0",
|
|
53
54
|
"lefthook": "^1.11.5",
|
|
54
55
|
"prettier": "^3.5.3",
|
|
55
|
-
"typescript": "^5.7.0"
|
|
56
|
+
"typescript": "^5.7.0",
|
|
57
|
+
"vitest": "^4.1.10"
|
|
56
58
|
}
|
|
57
59
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { buildSchema, GraphQLBoolean, GraphQLFloat, GraphQLInt, GraphQLString, type GraphQLInputType } from 'graphql';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { coerceValue, formatTypeInspection } from './dynamic.ts';
|
|
5
|
+
|
|
6
|
+
describe('coerceValue', () => {
|
|
7
|
+
it('parses Int values', () => {
|
|
8
|
+
expect(coerceValue('42', GraphQLInt, 'limit')).toBe(42);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('rejects malformed Int values', () => {
|
|
12
|
+
expect(() => coerceValue('abc', GraphQLInt, 'limit')).toThrow('--limit expects an integer, got "abc".');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('parses Float values', () => {
|
|
16
|
+
expect(coerceValue('1.5', GraphQLFloat, 'score')).toBe(1.5);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('rejects malformed Float values', () => {
|
|
20
|
+
expect(() => coerceValue('abc', GraphQLFloat, 'score')).toThrow('--score expects a number, got "abc".');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('parses Boolean literals strictly', () => {
|
|
24
|
+
expect(coerceValue('true', GraphQLBoolean, 'archived')).toBe(true);
|
|
25
|
+
expect(coerceValue('false', GraphQLBoolean, 'archived')).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('rejects non-literal Boolean values', () => {
|
|
29
|
+
expect(() => coerceValue('yes', GraphQLBoolean, 'archived')).toThrow(
|
|
30
|
+
'--archived expects "true" or "false", got "yes".',
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('passes plain strings through', () => {
|
|
35
|
+
expect(coerceValue('hello', GraphQLString, 'name')).toBe('hello');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('parses JSON for structured inputs', () => {
|
|
39
|
+
const schema = buildSchema(`
|
|
40
|
+
input FilterInput { country: String }
|
|
41
|
+
type Query { q(filter: FilterInput): String }
|
|
42
|
+
`);
|
|
43
|
+
const filterType = schema.getType('FilterInput') as GraphQLInputType;
|
|
44
|
+
expect(coerceValue('{"country":"BE"}', filterType, 'filter')).toEqual({ country: 'BE' });
|
|
45
|
+
expect(() => coerceValue('{oops', filterType, 'filter')).toThrow('--filter expects JSON');
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe('formatTypeInspection', () => {
|
|
50
|
+
const schema = buildSchema(`
|
|
51
|
+
"A contact filter"
|
|
52
|
+
input ContactFilterInput {
|
|
53
|
+
"Country code"
|
|
54
|
+
country: String
|
|
55
|
+
status: ContactStatus
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
enum ContactStatus {
|
|
59
|
+
ACTIVE
|
|
60
|
+
ARCHIVED
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
type Query { q(filter: ContactFilterInput): String }
|
|
64
|
+
`);
|
|
65
|
+
|
|
66
|
+
it('renders input object fields with descriptions', () => {
|
|
67
|
+
const out = formatTypeInspection(schema, 'ContactFilterInput');
|
|
68
|
+
expect(out).toContain('input ContactFilterInput — A contact filter');
|
|
69
|
+
expect(out).toContain('country: String');
|
|
70
|
+
expect(out).toContain('Country code');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('points at nested inspectable types', () => {
|
|
74
|
+
const out = formatTypeInspection(schema, 'ContactFilterInput');
|
|
75
|
+
expect(out).toContain('sl schema inspect ContactStatus');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('renders enum values', () => {
|
|
79
|
+
const out = formatTypeInspection(schema, 'ContactStatus');
|
|
80
|
+
expect(out).toContain('enum ContactStatus');
|
|
81
|
+
expect(out).toContain('ACTIVE');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('returns null for unknown types', () => {
|
|
85
|
+
expect(formatTypeInspection(schema, 'Nope')).toBeNull();
|
|
86
|
+
});
|
|
87
|
+
});
|
package/src/commands/dynamic.ts
CHANGED
|
@@ -116,7 +116,7 @@ function isStructuredInput(type: GraphQLInputType): boolean {
|
|
|
116
116
|
* enums) are parsed as JSON. Throws a readable error on malformed JSON so the
|
|
117
117
|
* action can surface it instead of silently sending a bad string.
|
|
118
118
|
*/
|
|
119
|
-
function coerceValue(raw: string, type: GraphQLInputType, flagName: string): unknown {
|
|
119
|
+
export function coerceValue(raw: string, type: GraphQLInputType, flagName: string): unknown {
|
|
120
120
|
if (isStructuredInput(type)) {
|
|
121
121
|
try {
|
|
122
122
|
return JSON.parse(raw);
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { buildSchema } from 'graphql';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { buildQuery } from './query-builder.ts';
|
|
5
|
+
|
|
6
|
+
const schema = buildSchema(`
|
|
7
|
+
type Query {
|
|
8
|
+
contact(id: ID!): Contact
|
|
9
|
+
contacts(first: Int): ContactConnection
|
|
10
|
+
version: String
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type Mutation {
|
|
14
|
+
updateContact(input: UpdateContactInput!): UpdateContactPayload
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type Contact {
|
|
18
|
+
id: ID!
|
|
19
|
+
name: String
|
|
20
|
+
organization: Organization
|
|
21
|
+
avatar(size: Int!): String
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type Organization {
|
|
25
|
+
id: ID!
|
|
26
|
+
name: String
|
|
27
|
+
contacts: [Contact]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type ContactConnection {
|
|
31
|
+
nodes: [Contact]
|
|
32
|
+
pageInfo: PageInfo
|
|
33
|
+
totalCount: Int
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type PageInfo {
|
|
37
|
+
hasNextPage: Boolean!
|
|
38
|
+
endCursor: String
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
input UpdateContactInput {
|
|
42
|
+
id: ID!
|
|
43
|
+
name: String
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type UpdateContactPayload {
|
|
47
|
+
id: ID
|
|
48
|
+
contact: Contact
|
|
49
|
+
}
|
|
50
|
+
`);
|
|
51
|
+
|
|
52
|
+
describe('buildQuery', () => {
|
|
53
|
+
it('selects only leaf fields at depth 0 and skips fields with required args', () => {
|
|
54
|
+
const { query } = buildQuery(schema, 'query', 'contact', { id: '1' });
|
|
55
|
+
expect(query).toContain('id');
|
|
56
|
+
expect(query).toContain('name');
|
|
57
|
+
expect(query).not.toContain('organization');
|
|
58
|
+
expect(query).not.toContain('avatar');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('descends into object fields at depth 1 without recursing into cycles', () => {
|
|
62
|
+
const { query } = buildQuery(schema, 'query', 'contact', { id: '1' }, { depth: 2 });
|
|
63
|
+
expect(query).toContain('organization {');
|
|
64
|
+
expect(query).not.toContain('contacts {');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('unwraps Relay connections into nodes/pageInfo/totalCount', () => {
|
|
68
|
+
const { query } = buildQuery(schema, 'query', 'contacts', {});
|
|
69
|
+
expect(query).toContain('nodes {');
|
|
70
|
+
expect(query).toContain('pageInfo {');
|
|
71
|
+
expect(query).toContain('hasNextPage');
|
|
72
|
+
expect(query).toContain('totalCount');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('builds selections from an explicit field list with dot notation', () => {
|
|
76
|
+
const { query } = buildQuery(schema, 'query', 'contact', { id: '1' }, { fields: ['name', 'organization.name'] });
|
|
77
|
+
expect(query).toContain('name');
|
|
78
|
+
expect(query).toContain('organization {');
|
|
79
|
+
expect(query).not.toContain('id\n');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('omits the selection set for scalar return types', () => {
|
|
83
|
+
const { query } = buildQuery(schema, 'query', 'version', {});
|
|
84
|
+
expect(query).toContain('version');
|
|
85
|
+
expect(query).not.toContain('{ __typename');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('declares variables for every field argument', () => {
|
|
89
|
+
const { query } = buildQuery(schema, 'query', 'contact', { id: '1' });
|
|
90
|
+
expect(query).toContain('query Contact($id: ID!)');
|
|
91
|
+
expect(query).toContain('contact(id: $id)');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('falls back to __typename on payload types at depth 0 (id is skipped)', () => {
|
|
95
|
+
const { query } = buildQuery(schema, 'mutation', 'updateContact', { input: { id: '1' } });
|
|
96
|
+
expect(query).toContain('__typename');
|
|
97
|
+
expect(query).not.toMatch(/\{\s*id/);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('selects payload object fields at depth 1, still skipping the payload id', () => {
|
|
101
|
+
const { query } = buildQuery(schema, 'mutation', 'updateContact', { input: { id: '1' } }, { depth: 1 });
|
|
102
|
+
expect(query).toContain('contact {');
|
|
103
|
+
expect(query).not.toMatch(/updateContact\(input: \$input\) \{\s*id/);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('honors exclude paths', () => {
|
|
107
|
+
const { query } = buildQuery(schema, 'query', 'contact', { id: '1' }, { exclude: ['name'] });
|
|
108
|
+
expect(query).not.toContain('name');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('throws on unknown root fields', () => {
|
|
112
|
+
expect(() => buildQuery(schema, 'query', 'nope', {})).toThrow('Unknown query field: nope');
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { isExpired, type Credentials } from './auth.ts';
|
|
4
|
+
|
|
5
|
+
function credsExpiringIn(seconds: number): Credentials {
|
|
6
|
+
return {
|
|
7
|
+
accessToken: 'a',
|
|
8
|
+
refreshToken: 'r',
|
|
9
|
+
idToken: 'i',
|
|
10
|
+
expiresAt: Math.floor(Date.now() / 1000) + seconds,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe('isExpired', () => {
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
vi.useRealTimers();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('is false well before expiry', () => {
|
|
20
|
+
expect(isExpired(credsExpiringIn(300))).toBe(false);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('is true inside the 60s refresh buffer', () => {
|
|
24
|
+
expect(isExpired(credsExpiringIn(30))).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('is true after expiry', () => {
|
|
28
|
+
expect(isExpired(credsExpiringIn(-10))).toBe(true);
|
|
29
|
+
});
|
|
30
|
+
});
|