@simtlix/simfinity-js-client 1.0.6 → 1.1.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/.claude/worktrees/flamboyant-cartwright/.claude/settings.local.json +18 -0
- package/.claude/worktrees/flamboyant-cartwright/IMPLEMENTATION_PROMPT.md +1137 -0
- package/.claude/worktrees/flamboyant-cartwright/LICENSE +201 -0
- package/.claude/worktrees/flamboyant-cartwright/README.md +491 -0
- package/.claude/worktrees/flamboyant-cartwright/examples/demo.js +402 -0
- package/.claude/worktrees/flamboyant-cartwright/package-lock.json +107 -0
- package/.claude/worktrees/flamboyant-cartwright/package.json +29 -0
- package/.claude/worktrees/flamboyant-cartwright/src/SimfinityClient.js +1398 -0
- package/README.md +100 -0
- package/examples/demo.js +52 -0
- package/package.json +1 -1
- package/src/SimfinityClient.js +56 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
# @simtlix/simfinity-js-client
|
|
2
|
+
|
|
3
|
+
Introspection-driven GraphQL client for [simfinity-js](https://github.com/simtlix/simfinity.js) APIs. It discovers your schema at runtime via introspection and exposes a chainable query builder, CRUD helpers, aggregate queries, state-machine transitions, schema metadata access, mutation input transformation, and more — no code generation required.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @simtlix/simfinity-js-client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import SimfinityClient from '@simtlix/simfinity-js-client';
|
|
15
|
+
|
|
16
|
+
const client = new SimfinityClient('http://localhost:3000/graphql');
|
|
17
|
+
await client.init();
|
|
18
|
+
|
|
19
|
+
// Optional: attach auth headers (perform login in your app; this client does not include login)
|
|
20
|
+
const tokenStore = { accessToken: null };
|
|
21
|
+
const authedClient = new SimfinityClient('http://localhost:3000/graphql', {
|
|
22
|
+
prepareHeaders(headers) {
|
|
23
|
+
if (tokenStore.accessToken) {
|
|
24
|
+
headers['Authorization'] = `Bearer ${tokenStore.accessToken}`;
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
await authedClient.init();
|
|
29
|
+
|
|
30
|
+
const series = await client.find('serie').exec();
|
|
31
|
+
console.log(series);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## API Reference
|
|
35
|
+
|
|
36
|
+
### `new SimfinityClient(endpoint [, options])`
|
|
37
|
+
|
|
38
|
+
Creates a new client instance pointing at the given GraphQL endpoint.
|
|
39
|
+
|
|
40
|
+
- **`options.prepareHeaders(headers)`** (optional): invoked before every GraphQL HTTP request. Mutate the plain `headers` object to add credentials (for example `Authorization`). The client does not implement login — call your auth API separately, then set tokens in a closure or store that `prepareHeaders` reads.
|
|
41
|
+
|
|
42
|
+
### `client.init()`
|
|
43
|
+
|
|
44
|
+
Runs an introspection query against the endpoint and builds internal registries of types, queries, and mutations — including Simfinity-specific field extensions (relation metadata, state machine flags, read-only markers). **Must be called before any other method.**
|
|
45
|
+
|
|
46
|
+
### Schema Inspection
|
|
47
|
+
|
|
48
|
+
| Method | Returns |
|
|
49
|
+
|---|---|
|
|
50
|
+
| `client.getTypes()` | All discovered types (objects, enums, scalars, inputs) |
|
|
51
|
+
| `client.getQueries()` | All root query fields |
|
|
52
|
+
| `client.getMutations()` | All root mutation fields |
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
### Query Builder — `client.find(typeName)`
|
|
57
|
+
|
|
58
|
+
Returns a chainable `QueryBuilder`. Call `.exec()` to execute.
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
const results = await client.find('serie')
|
|
62
|
+
.where('categories', 'EQ', 'Drama')
|
|
63
|
+
.joinObject('director', 'name country')
|
|
64
|
+
.joinCollection('seasons', 'number year state')
|
|
65
|
+
.joinCollection('seasons.episodes', 'number name')
|
|
66
|
+
.fields('id name categories')
|
|
67
|
+
.page(1, 10)
|
|
68
|
+
.sort('name', 'ASC')
|
|
69
|
+
.exec();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
#### `.where(field, operator, value [, value2])`
|
|
73
|
+
|
|
74
|
+
Filter by a scalar field. Supported operators depend on the server schema (e.g. `EQ`, `NE`, `GT`, `LT`, `GTE`, `LTE`, `BETWEEN`, `LIKE`, `NIN`).
|
|
75
|
+
|
|
76
|
+
#### `.where(field, terms)`
|
|
77
|
+
|
|
78
|
+
Filter by a collection field using an array of term objects:
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
.where('seasons', [
|
|
82
|
+
{ path: 'episodes.name', operator: 'EQ', value: 'Pilot' }
|
|
83
|
+
])
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
#### `.or(groups)` / `.and(groups)`
|
|
87
|
+
|
|
88
|
+
Add AND/OR logical filter groups for complex boolean conditions. Each method accepts an array of `QLFilterGroup` objects and can be called multiple times (groups accumulate). These combine with existing flat `.where()` filters via AND at the top level.
|
|
89
|
+
|
|
90
|
+
A `QLFilterGroup` is a plain object with optional `AND`, `OR`, and `conditions` fields:
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
{
|
|
94
|
+
conditions: [{ field: 'category', operator: 'EQ', value: 'Drama' }],
|
|
95
|
+
AND: [/* nested QLFilterGroup objects */],
|
|
96
|
+
OR: [/* nested QLFilterGroup objects */],
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**Simple OR** -- return series in either Drama or Comedy:
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
const results = await client.find('serie')
|
|
104
|
+
.or([
|
|
105
|
+
SimfinityClient.condition('categories', 'EQ', 'Drama'),
|
|
106
|
+
SimfinityClient.condition('categories', 'EQ', 'Comedy'),
|
|
107
|
+
])
|
|
108
|
+
.fields('id name categories')
|
|
109
|
+
.exec();
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
**Flat filters combined with OR** -- flat `.where()` is ANDed with the OR group:
|
|
113
|
+
|
|
114
|
+
```js
|
|
115
|
+
const results = await client.find('serie')
|
|
116
|
+
.where('categories', 'NE', 'Horror')
|
|
117
|
+
.or([
|
|
118
|
+
SimfinityClient.condition('name', 'LIKE', 'Breaking'),
|
|
119
|
+
SimfinityClient.condition('name', 'LIKE', 'Game'),
|
|
120
|
+
])
|
|
121
|
+
.fields('id name categories')
|
|
122
|
+
.exec();
|
|
123
|
+
// Equivalent to: categories != "Horror" AND (name LIKE "Breaking" OR name LIKE "Game")
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**Nested AND inside OR**:
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
const results = await client.find('serie')
|
|
130
|
+
.or([
|
|
131
|
+
{ AND: [
|
|
132
|
+
SimfinityClient.condition('categories', 'EQ', 'Drama'),
|
|
133
|
+
SimfinityClient.condition('year', 'GTE', 2020),
|
|
134
|
+
]},
|
|
135
|
+
{ AND: [
|
|
136
|
+
SimfinityClient.condition('categories', 'EQ', 'Comedy'),
|
|
137
|
+
SimfinityClient.condition('year', 'GTE', 2015),
|
|
138
|
+
]},
|
|
139
|
+
])
|
|
140
|
+
.exec();
|
|
141
|
+
// Equivalent to: (categories = "Drama" AND year >= 2020) OR (categories = "Comedy" AND year >= 2015)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
**AND with nested OR groups**:
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
const results = await client.find('serie')
|
|
148
|
+
.where('year', 'GTE', 2000)
|
|
149
|
+
.and([
|
|
150
|
+
{ OR: [
|
|
151
|
+
SimfinityClient.condition('categories', 'EQ', 'Drama'),
|
|
152
|
+
SimfinityClient.condition('categories', 'EQ', 'Comedy'),
|
|
153
|
+
]},
|
|
154
|
+
{ OR: [
|
|
155
|
+
SimfinityClient.condition('director', 'EQ', 'UK', 'country'),
|
|
156
|
+
SimfinityClient.condition('director', 'EQ', 'US', 'country'),
|
|
157
|
+
]},
|
|
158
|
+
])
|
|
159
|
+
.exec();
|
|
160
|
+
// Equivalent to: year >= 2000 AND (Drama OR Comedy) AND (UK OR US director)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
**Relationship path filtering** -- use the `path` parameter for object field conditions:
|
|
164
|
+
|
|
165
|
+
```js
|
|
166
|
+
.or([
|
|
167
|
+
SimfinityClient.condition('director', 'LIKE', 'Adams', 'name'),
|
|
168
|
+
SimfinityClient.condition('director', 'LIKE', 'Nolan', 'name'),
|
|
169
|
+
])
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
The Aggregate Builder also supports `.or()` and `.and()` with identical behavior. The server enforces a maximum nesting depth of 5 levels.
|
|
173
|
+
|
|
174
|
+
#### `SimfinityClient.condition(field, operator, value [, path])`
|
|
175
|
+
|
|
176
|
+
Static convenience factory for creating a filter group with a single condition. Returns a `QLFilterGroup` object:
|
|
177
|
+
|
|
178
|
+
```js
|
|
179
|
+
SimfinityClient.condition('category', 'EQ', 'Drama')
|
|
180
|
+
// Equivalent to: { conditions: [{ field: 'category', operator: 'EQ', value: 'Drama' }] }
|
|
181
|
+
|
|
182
|
+
SimfinityClient.condition('author', 'LIKE', 'Adams', 'name')
|
|
183
|
+
// Equivalent to: { conditions: [{ field: 'author', operator: 'LIKE', value: 'Adams', path: 'name' }] }
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
#### `.joinObject(path, fields)`
|
|
187
|
+
|
|
188
|
+
Include a related object's fields in the selection.
|
|
189
|
+
|
|
190
|
+
#### `.joinCollection(path, fields [, filter])`
|
|
191
|
+
|
|
192
|
+
Include a related collection's fields. Supports dot-notation for nested joins (e.g. `'seasons.episodes'`). An optional `filter` array of terms can be passed.
|
|
193
|
+
|
|
194
|
+
#### `.fields(selectionString)`
|
|
195
|
+
|
|
196
|
+
Space-separated list of scalar fields to select on the root type. If omitted, all scalar and enum fields are selected automatically.
|
|
197
|
+
|
|
198
|
+
#### `.autoSelect()`
|
|
199
|
+
|
|
200
|
+
Automatically builds a full selection set using schema metadata, including nested object fields with display field resolution. Returns the builder for chaining. The selection metadata is available via `builder._selectionMeta`.
|
|
201
|
+
|
|
202
|
+
```js
|
|
203
|
+
const results = await client.find('serie')
|
|
204
|
+
.autoSelect()
|
|
205
|
+
.page(1, 10)
|
|
206
|
+
.exec();
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### `.page(page, size [, count])`
|
|
210
|
+
|
|
211
|
+
Paginate results. `page` is 1-based. Set `count` to `true` to include total count in response extensions.
|
|
212
|
+
|
|
213
|
+
#### `.sort(field, order)`
|
|
214
|
+
|
|
215
|
+
Sort results. `order` is `'ASC'` or `'DESC'`. Can be called multiple times for multi-field sorting.
|
|
216
|
+
|
|
217
|
+
#### `.exec()`
|
|
218
|
+
|
|
219
|
+
Executes the query and returns the result array. If the server returns extensions (e.g. pagination count), they are attached as `result.__extensions`.
|
|
220
|
+
|
|
221
|
+
#### `.execWithMeta()`
|
|
222
|
+
|
|
223
|
+
Executes the query and returns a `{ data, extensions }` wrapper for clean access to response metadata:
|
|
224
|
+
|
|
225
|
+
```js
|
|
226
|
+
const { data: series, extensions } = await client.find('serie')
|
|
227
|
+
.page(1, 10, true)
|
|
228
|
+
.execWithMeta();
|
|
229
|
+
|
|
230
|
+
console.log(series); // the result array
|
|
231
|
+
console.log(extensions?.count); // total count from server
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
### Aggregate Builder — `client.aggregate(typeName)`
|
|
237
|
+
|
|
238
|
+
Returns a chainable `AggregateBuilder`. Call `.exec()` to execute.
|
|
239
|
+
|
|
240
|
+
```js
|
|
241
|
+
const results = await client.aggregate('season')
|
|
242
|
+
.groupBy('serie')
|
|
243
|
+
.fact('COUNT', 'seasonCount', 'id')
|
|
244
|
+
.fact('AVG', 'avgYear', 'year')
|
|
245
|
+
.page(1, 5)
|
|
246
|
+
.sort('serie', 'ASC')
|
|
247
|
+
.exec();
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
#### `.groupBy(field)`
|
|
251
|
+
|
|
252
|
+
**Required.** The field to group by.
|
|
253
|
+
|
|
254
|
+
#### `.fact(operation, factName, path)`
|
|
255
|
+
|
|
256
|
+
Add an aggregation fact. `operation` can be `COUNT`, `SUM`, `AVG`, `MIN`, `MAX`, etc.
|
|
257
|
+
|
|
258
|
+
#### `.where()`, `.page()`, `.sort()`
|
|
259
|
+
|
|
260
|
+
Same API as the Query Builder.
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
### Get by ID
|
|
265
|
+
|
|
266
|
+
```js
|
|
267
|
+
const serie = await client.getById('serie', '507f1f77bcf86cd799439011', 'id name categories');
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
If `fields` is omitted, all scalar fields are returned.
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
274
|
+
### Collection Item Queries — `client.findByParent()`
|
|
275
|
+
|
|
276
|
+
Query collection items filtered by their parent entity:
|
|
277
|
+
|
|
278
|
+
```js
|
|
279
|
+
const seasons = await client.findByParent('season', 'serie', parentSerieId)
|
|
280
|
+
.page(1, 10, true)
|
|
281
|
+
.fields('id number year state')
|
|
282
|
+
.exec();
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Returns a `QueryBuilder` pre-configured with a connection field filter and default sort by `id ASC`. Chain additional `.where()`, `.page()`, `.fields()`, `.sort()` as needed.
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|
|
289
|
+
### FK/Relation Search — `client.search()`
|
|
290
|
+
|
|
291
|
+
Search-as-you-type for FK reference fields:
|
|
292
|
+
|
|
293
|
+
```js
|
|
294
|
+
const results = await client.search('Genre', 'com', {
|
|
295
|
+
displayField: 'name',
|
|
296
|
+
page: 1,
|
|
297
|
+
size: 10,
|
|
298
|
+
});
|
|
299
|
+
// [{ id: '1', name: 'Comedy' }, { id: '2', name: 'Romantic Comedy' }]
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
Automatically selects the appropriate operator (`LIKE` for strings, `EQ` for numeric/boolean) and casts the search term to the field's scalar type.
|
|
303
|
+
|
|
304
|
+
| Option | Default | Description |
|
|
305
|
+
|---|---|---|
|
|
306
|
+
| `displayField` | auto-resolved (`name` or first scalar) | Field to search by |
|
|
307
|
+
| `page` | `1` | Page number |
|
|
308
|
+
| `size` | `10` | Page size |
|
|
309
|
+
|
|
310
|
+
---
|
|
311
|
+
|
|
312
|
+
### CRUD Mutations
|
|
313
|
+
|
|
314
|
+
```js
|
|
315
|
+
const created = await client.add('star', { name: 'John Doe' }, 'id name');
|
|
316
|
+
|
|
317
|
+
const updated = await client.update('star', createdId, { name: 'Jane Doe' }, 'id name');
|
|
318
|
+
|
|
319
|
+
const deleted = await client.delete('star', createdId, 'id name');
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
#### Automatic Input Transformation
|
|
323
|
+
|
|
324
|
+
Pass `{ transform: true }` as the last argument to have the client automatically clean mutation input based on schema metadata — stripping `__typename`, coercing scalar types, extracting FK IDs, handling embedded objects, and skipping read-only/state-machine fields:
|
|
325
|
+
|
|
326
|
+
```js
|
|
327
|
+
const created = await client.add('serie', rawFormData, 'id name', { transform: true });
|
|
328
|
+
|
|
329
|
+
const updated = await client.update('serie', id, rawFormData, 'id name', {
|
|
330
|
+
transform: true,
|
|
331
|
+
skipFields: ['temporaryField'],
|
|
332
|
+
});
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
---
|
|
336
|
+
|
|
337
|
+
### Mutation Input Transformation — `client.transformInput()`
|
|
338
|
+
|
|
339
|
+
Schema-aware mutation input cleaning, usable standalone:
|
|
340
|
+
|
|
341
|
+
```js
|
|
342
|
+
const cleaned = client.transformInput('serie', rawInput, {
|
|
343
|
+
mode: 'create', // or 'update'
|
|
344
|
+
skipFields: [], // fields to exclude
|
|
345
|
+
transientFields: [], // transient fields to exclude
|
|
346
|
+
});
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
Handles: `__typename` removal, numeric/date coercion, embedded object cleaning, FK reference extraction (`{ id }` only), and skipping read-only, state-machine, and collection fields.
|
|
350
|
+
|
|
351
|
+
---
|
|
352
|
+
|
|
353
|
+
### Collection Delta Mutations — `client.transformCollectionDelta()`
|
|
354
|
+
|
|
355
|
+
Build the `{ added, updated, deleted }` structure for collection field mutations:
|
|
356
|
+
|
|
357
|
+
```js
|
|
358
|
+
const delta = {
|
|
359
|
+
added: [{ id: 'temp_1', number: '3', year: '2025', __status: 'added' }],
|
|
360
|
+
updated: [{ id: 'real-id', number: '2', year: '2024', __status: 'modified' }],
|
|
361
|
+
deleted: [{ id: 'del-id' }],
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const cleaned = client.transformCollectionDelta('season', delta, {
|
|
365
|
+
connectionField: 'serie',
|
|
366
|
+
});
|
|
367
|
+
// { added: [{ number: 3, year: 2025 }], updated: [{ id: 'real-id', number: 2, year: 2024 }], deleted: ['del-id'] }
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
Strips metadata (`__status`, `__originalData`, `__typename`), removes temporary IDs and connection fields, coerces scalar types, and extracts FK object IDs.
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
### State Machine Transitions
|
|
375
|
+
|
|
376
|
+
```js
|
|
377
|
+
const activated = await client.transition('season', 'activate', seasonId, 'id number state');
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
Optionally pass extra input fields:
|
|
381
|
+
|
|
382
|
+
```js
|
|
383
|
+
await client.transition('season', 'activate', seasonId, { extraField: 'value' }, 'id number state');
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
---
|
|
387
|
+
|
|
388
|
+
### Custom Mutations
|
|
389
|
+
|
|
390
|
+
```js
|
|
391
|
+
const result = await client.customMutation('myCustomMutation', { arg1: 'value' }, 'id status');
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
---
|
|
395
|
+
|
|
396
|
+
### Raw GraphQL Execution
|
|
397
|
+
|
|
398
|
+
```js
|
|
399
|
+
const response = await client.execute(
|
|
400
|
+
'{ stars(pagination: { page: 1, size: 2 }) { id name } }'
|
|
401
|
+
);
|
|
402
|
+
console.log(response.data);
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
---
|
|
406
|
+
|
|
407
|
+
### Selection Set Building — `client.buildSelectionSet()`
|
|
408
|
+
|
|
409
|
+
Generate a complete GraphQL selection set string with metadata from the schema, useful for building dynamic UIs:
|
|
410
|
+
|
|
411
|
+
```js
|
|
412
|
+
const { selection, columns, sortFieldByColumn, fieldTypeByColumn } =
|
|
413
|
+
client.buildSelectionSet('serie');
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
Returns:
|
|
417
|
+
|
|
418
|
+
| Property | Type | Description |
|
|
419
|
+
|---|---|---|
|
|
420
|
+
| `selection` | `string` | GraphQL selection set string (includes nested object sub-selections) |
|
|
421
|
+
| `columns` | `string[]` | Display column names (excludes `id`) |
|
|
422
|
+
| `sortFieldByColumn` | `object` | Maps column name to sort field path (e.g. `director` -> `director.name`) |
|
|
423
|
+
| `fieldTypeByColumn` | `object` | Maps column name to scalar type name |
|
|
424
|
+
|
|
425
|
+
---
|
|
426
|
+
|
|
427
|
+
### Schema Metadata Access
|
|
428
|
+
|
|
429
|
+
#### Field Extensions
|
|
430
|
+
|
|
431
|
+
```js
|
|
432
|
+
const ext = client.getFieldExtensions('serie', 'director');
|
|
433
|
+
// { relation: { displayField: 'name', embedded: true, connectionField: null }, stateMachine: null, readOnly: null }
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
| Method | Signature | Description |
|
|
437
|
+
|---|---|---|
|
|
438
|
+
| `getFieldExtensions` | `(typeName, fieldName)` | Full extensions object for a field |
|
|
439
|
+
| `getDisplayField` | `(typeName, fieldName)` | Display field with fallback chain (extension -> `name` -> first scalar) |
|
|
440
|
+
| `isEmbeddedField` | `(typeName, fieldName)` | Whether the field is an embedded object |
|
|
441
|
+
| `getConnectionField` | `(typeName, fieldName)` | Back-reference field name for collections |
|
|
442
|
+
| `isStateMachineField` | `(typeName, fieldName)` | Whether the field is state-machine managed |
|
|
443
|
+
| `isReadOnlyField` | `(typeName, fieldName)` | Whether the field is read-only |
|
|
444
|
+
| `getEnumValues` | `(typeName)` | Enum values for an enum type |
|
|
445
|
+
| `getFieldsOfType` | `(typeName)` | All fields with full metadata |
|
|
446
|
+
| `getDescriptionFieldType` | `(typeName, fieldName)` | Scalar type name of a display field |
|
|
447
|
+
|
|
448
|
+
#### Entity & Query Name Resolution
|
|
449
|
+
|
|
450
|
+
```js
|
|
451
|
+
client.getTypeNameForQuery('series'); // 'serie'
|
|
452
|
+
client.getPluralQueryName('serie'); // 'series'
|
|
453
|
+
client.getSingularQueryName('serie'); // 'serie'
|
|
454
|
+
client.getListEntityNames(); // ['episodes', 'seasons', 'series', 'stars', ...]
|
|
455
|
+
client.getListEntityNamesOfType('serie'); // ['series']
|
|
456
|
+
client.getQueryNamesForType('serie');
|
|
457
|
+
// { pluralQueryName: 'series', singularQueryName: 'serie', aggregateQueryName: 'series_aggregate' }
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
#### Scalar Type Utilities
|
|
461
|
+
|
|
462
|
+
Simfinity uses validated scalar names like `SeasonNumber_Int` where the suffix after `_` indicates the base type:
|
|
463
|
+
|
|
464
|
+
```js
|
|
465
|
+
client.getActualScalarType('SeasonNumber_Int'); // 'Int'
|
|
466
|
+
client.isNumericScalar('SeasonNumber_Int'); // true
|
|
467
|
+
client.isBooleanScalar('Boolean'); // true
|
|
468
|
+
client.isDateTimeScalar('StartDate_DateTime'); // true
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
#### State Machine Metadata
|
|
472
|
+
|
|
473
|
+
```js
|
|
474
|
+
client.getStateMachineFields('season'); // ['state']
|
|
475
|
+
client.getAvailableTransitions('season');
|
|
476
|
+
// [{ action: 'activate', mutationName: 'activate_season' }, { action: 'finalize', mutationName: 'finalize_season' }]
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
---
|
|
480
|
+
|
|
481
|
+
## Examples
|
|
482
|
+
|
|
483
|
+
See [`examples/demo.js`](examples/demo.js) for a comprehensive walkthrough covering all features: schema discovery, filtering, joins, pagination, sorting, CRUD, state machine transitions, aggregates, raw queries, schema metadata, selection set building, entity resolution, input transformation, collection deltas, parent queries, FK search, and response extensions.
|
|
484
|
+
|
|
485
|
+
```bash
|
|
486
|
+
GRAPHQL_ENDPOINT=http://localhost:3000/graphql npm run demo
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
## License
|
|
490
|
+
|
|
491
|
+
[Apache-2.0](LICENSE)
|