@stonecrop/graphql-middleware 0.8.8 → 0.8.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.
@@ -0,0 +1,178 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { defaultRecordFieldName, defaultConnectionFieldName, defaultOrderByTypeName, buildRecordQuery, buildListQuery, queryableFieldNames, RELATION_FIELDTYPES, } from '../src/plugin/postgraphile';
3
+ // ---------------------------------------------------------------------------
4
+ // Test fixtures
5
+ // ---------------------------------------------------------------------------
6
+ /** A simple doctype with only scalar fields */
7
+ const scalarOnlyMeta = {
8
+ name: 'Resource',
9
+ tableName: 'resources',
10
+ fields: [
11
+ { fieldname: 'id', fieldtype: 'Data', label: 'ID' },
12
+ { fieldname: 'name', fieldtype: 'Data', label: 'Name' },
13
+ { fieldname: 'quantity', fieldtype: 'Int', label: 'Quantity' },
14
+ { fieldname: 'is_active', fieldtype: 'Check', label: 'Active' },
15
+ ],
16
+ };
17
+ /** A doctype that includes Link and Doctype (relation) fields alongside scalars */
18
+ const mixedFieldsMeta = {
19
+ name: 'Recipe',
20
+ tableName: 'recipes',
21
+ fields: [
22
+ { fieldname: 'id', fieldtype: 'Data', label: 'ID' },
23
+ { fieldname: 'title', fieldtype: 'Data', label: 'Title' },
24
+ { fieldname: 'created_at', fieldtype: 'Datetime', label: 'Created' },
25
+ // FK object reference — requires sub-selection, should be skipped
26
+ { fieldname: 'userByCreatedBy', fieldtype: 'Link', label: 'Creator', options: 'user' },
27
+ // Reverse relation connection — requires sub-selection, should be skipped
28
+ {
29
+ fieldname: 'recipeIngredientsByRecipeId',
30
+ fieldtype: 'Doctype',
31
+ label: 'Ingredients',
32
+ options: 'recipe-ingredient',
33
+ },
34
+ { fieldname: 'rating', fieldtype: 'Float', label: 'Rating' },
35
+ ],
36
+ };
37
+ // ===========================================================================
38
+ // Inflection helpers
39
+ // ===========================================================================
40
+ describe('defaultRecordFieldName', () => {
41
+ it('singularizes regular plurals and appends ById', () => {
42
+ expect(defaultRecordFieldName('resources')).toBe('resourceById');
43
+ });
44
+ it('handles irregular plurals (statuses → status)', () => {
45
+ expect(defaultRecordFieldName('statuses')).toBe('statusById');
46
+ });
47
+ it('handles irregular plurals (categories → category)', () => {
48
+ expect(defaultRecordFieldName('categories')).toBe('categoryById');
49
+ });
50
+ it('handles irregular plurals (addresses → address)', () => {
51
+ expect(defaultRecordFieldName('addresses')).toBe('addressById');
52
+ });
53
+ it('handles snake_case table names', () => {
54
+ expect(defaultRecordFieldName('sales_orders')).toBe('salesOrderById');
55
+ });
56
+ it('handles already-singular names', () => {
57
+ expect(defaultRecordFieldName('resource')).toBe('resourceById');
58
+ });
59
+ });
60
+ describe('defaultConnectionFieldName', () => {
61
+ it('prefixes with "all" and PascalCases', () => {
62
+ expect(defaultConnectionFieldName('resources')).toBe('allResources');
63
+ });
64
+ it('handles snake_case', () => {
65
+ expect(defaultConnectionFieldName('sales_orders')).toBe('allSalesOrders');
66
+ });
67
+ });
68
+ describe('defaultOrderByTypeName', () => {
69
+ it('PascalCases and appends OrderBy', () => {
70
+ expect(defaultOrderByTypeName('resources')).toBe('ResourcesOrderBy');
71
+ });
72
+ it('handles snake_case', () => {
73
+ expect(defaultOrderByTypeName('sales_orders')).toBe('SalesOrdersOrderBy');
74
+ });
75
+ });
76
+ // ===========================================================================
77
+ // Query builders — relation field filtering (Issue 5)
78
+ // ===========================================================================
79
+ describe('queryableFieldNames', () => {
80
+ it('returns all fields when none are relational', () => {
81
+ const names = queryableFieldNames(scalarOnlyMeta);
82
+ expect(names).toContain('id');
83
+ expect(names).toContain('name');
84
+ expect(names).toContain('quantity');
85
+ expect(names).toContain('is_active');
86
+ });
87
+ it('excludes Link and Doctype fields', () => {
88
+ const names = queryableFieldNames(mixedFieldsMeta);
89
+ expect(names).toContain('id');
90
+ expect(names).toContain('title');
91
+ expect(names).toContain('created_at');
92
+ expect(names).toContain('rating');
93
+ expect(names).not.toContain('userByCreatedBy');
94
+ expect(names).not.toContain('recipeIngredientsByRecipeId');
95
+ });
96
+ });
97
+ describe('RELATION_FIELDTYPES', () => {
98
+ it('contains Link and Doctype', () => {
99
+ expect(RELATION_FIELDTYPES.has('Link')).toBe(true);
100
+ expect(RELATION_FIELDTYPES.has('Doctype')).toBe(true);
101
+ });
102
+ it('does not contain scalar types', () => {
103
+ expect(RELATION_FIELDTYPES.has('Data')).toBe(false);
104
+ expect(RELATION_FIELDTYPES.has('Int')).toBe(false);
105
+ });
106
+ });
107
+ // ===========================================================================
108
+ // buildRecordQuery
109
+ // ===========================================================================
110
+ describe('buildRecordQuery', () => {
111
+ it('generates valid query with only scalar fields', () => {
112
+ const query = buildRecordQuery(scalarOnlyMeta, defaultRecordFieldName);
113
+ expect(query).toContain('query GetRecord($id: UUID!)');
114
+ expect(query).toContain('resourceById(id: $id)');
115
+ expect(query).toContain('id');
116
+ expect(query).toContain('name');
117
+ expect(query).toContain('quantity');
118
+ });
119
+ it('excludes relation fields from selection', () => {
120
+ const query = buildRecordQuery(mixedFieldsMeta, defaultRecordFieldName);
121
+ expect(query).toContain('title');
122
+ expect(query).toContain('rating');
123
+ expect(query).not.toContain('userByCreatedBy');
124
+ expect(query).not.toContain('recipeIngredientsByRecipeId');
125
+ });
126
+ it('uses custom recordFieldName inflection', () => {
127
+ const customInflection = (t) => `${t}ByRowId`;
128
+ const query = buildRecordQuery(scalarOnlyMeta, customInflection);
129
+ expect(query).toContain('resourcesByRowId(id: $id)');
130
+ });
131
+ });
132
+ // ===========================================================================
133
+ // buildListQuery — variable declarations (Issue 3)
134
+ // ===========================================================================
135
+ describe('buildListQuery', () => {
136
+ const identity = (t) => t;
137
+ it('declares no variables when no args are provided', () => {
138
+ const query = buildListQuery(scalarOnlyMeta, {}, defaultConnectionFieldName, defaultOrderByTypeName);
139
+ // Should be just "query GetRecords {" with no parenthesized variable declarations
140
+ expect(query).toMatch(/query GetRecords\s*\{/);
141
+ expect(query).not.toContain('$limit');
142
+ expect(query).not.toContain('$offset');
143
+ expect(query).not.toContain('$orderBy');
144
+ });
145
+ it('declares only $limit when only limit is provided', () => {
146
+ const query = buildListQuery(scalarOnlyMeta, { limit: 10 }, defaultConnectionFieldName, defaultOrderByTypeName);
147
+ expect(query).toContain('$limit: Int');
148
+ expect(query).toContain('first: $limit');
149
+ expect(query).not.toContain('$offset');
150
+ expect(query).not.toContain('$orderBy');
151
+ });
152
+ it('declares only $limit and $offset when both are provided', () => {
153
+ const query = buildListQuery(scalarOnlyMeta, { limit: 10, offset: 20 }, defaultConnectionFieldName, defaultOrderByTypeName);
154
+ expect(query).toContain('$limit: Int');
155
+ expect(query).toContain('$offset: Int');
156
+ expect(query).not.toContain('$orderBy');
157
+ });
158
+ it('declares all three variables when all args are provided', () => {
159
+ const query = buildListQuery(scalarOnlyMeta, { limit: 10, offset: 20, orderBy: 'NAME_ASC' }, defaultConnectionFieldName, defaultOrderByTypeName);
160
+ expect(query).toContain('$limit: Int');
161
+ expect(query).toContain('$offset: Int');
162
+ expect(query).toContain('$orderBy: [ResourcesOrderBy!]');
163
+ expect(query).toContain('first: $limit');
164
+ expect(query).toContain('offset: $offset');
165
+ expect(query).toContain('orderBy: $orderBy');
166
+ });
167
+ it('uses correct connection field name', () => {
168
+ const query = buildListQuery(scalarOnlyMeta, {}, defaultConnectionFieldName, defaultOrderByTypeName);
169
+ expect(query).toContain('allResources');
170
+ });
171
+ it('excludes relation fields from selection', () => {
172
+ const query = buildListQuery(mixedFieldsMeta, {}, defaultConnectionFieldName, defaultOrderByTypeName);
173
+ expect(query).toContain('title');
174
+ expect(query).toContain('rating');
175
+ expect(query).not.toContain('userByCreatedBy');
176
+ expect(query).not.toContain('recipeIngredientsByRecipeId');
177
+ });
178
+ });
@@ -0,0 +1,4 @@
1
+ import { describe, it } from 'vitest';
2
+ describe('Registry Tests', () => {
3
+ it.todo('should implement registry tests');
4
+ });
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@stonecrop/graphql-middleware",
3
- "version": "0.8.8",
3
+ "version": "0.8.10",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
7
+ "types": "./dist/src/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
- "types": "./dist/index.d.ts",
10
+ "types": "./dist/src/index.d.ts",
11
11
  "import": "./dist/index.js",
12
12
  "require": "./dist/index.cjs"
13
13
  }
@@ -17,15 +17,16 @@
17
17
  ],
18
18
  "dependencies": {
19
19
  "graphql": "^16.12.0",
20
+ "pluralize": "^8.0.0",
20
21
  "postgraphile": "^5.0.0-rc.7",
21
- "@stonecrop/schema": "0.8.8"
22
+ "@stonecrop/schema": "0.8.10"
22
23
  },
23
24
  "devDependencies": {
24
- "@rushstack/heft": "^1.1.7",
25
+ "@rushstack/heft": "^1.2.0",
25
26
  "@types/node": "^22.19.5",
27
+ "@types/pluralize": "^0.0.33",
26
28
  "typescript": "^5.9.3",
27
29
  "vite": "^7.3.1",
28
- "vite-plugin-dts": "^4.5.4",
29
30
  "vitest": "^4.0.18",
30
31
  "stonecrop-rig": "0.7.0"
31
32
  },
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './src/index'
2
- export {}