@uidu/client 3.9.0 → 3.10.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/CHANGELOG.md +9 -0
- package/dist/declarations/src/actions.d.ts +12 -0
- package/dist/declarations/src/client.d.ts +15 -0
- package/dist/declarations/src/generated/index.d.ts +7389 -16
- package/dist/declarations/src/index.d.ts +4 -1
- package/dist/declarations/src/models.d.ts +326 -0
- package/dist/declarations/src/types.d.ts +8 -1
- package/dist/uidu-client.cjs.dev.js +281 -2
- package/dist/uidu-client.cjs.prod.js +281 -2
- package/dist/uidu-client.esm.js +261 -3
- package/package.json +1 -1
- package/src/actions.ts +19 -0
- package/src/client.test.ts +56 -0
- package/src/client.ts +47 -4
- package/src/generated/index.ts +3572 -16
- package/src/index.ts +31 -1
- package/src/models.test.ts +245 -0
- package/src/models.ts +328 -0
- package/src/queries/actions/ExecuteAction.graphql +10 -0
- package/src/queries/models/ModelFields.graphql +39 -0
- package/src/queries/models/ModelMutations.graphql +173 -0
- package/src/queries/models/ModelQueries.graphql +44 -0
- package/src/types.ts +8 -1
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { UiduClient, createClient } from './client';
|
|
1
|
+
export { UiduClient, createClient, fromBridge } from './client';
|
|
2
2
|
export { UiduError } from './types';
|
|
3
3
|
export type { UiduClientConfig, UiduErrorOptions } from './types';
|
|
4
4
|
|
|
@@ -49,6 +49,7 @@ export { createWorkspace, generateWorkspaceApiCredentials } from './system';
|
|
|
49
49
|
export { createSpace } from './spaces';
|
|
50
50
|
export { createTask } from './tasks';
|
|
51
51
|
export { createNote } from './notes';
|
|
52
|
+
export { executeAction } from './actions';
|
|
52
53
|
export { createBenefit } from './benefits';
|
|
53
54
|
export { createBenefitEnrollment } from './benefit-enrollments';
|
|
54
55
|
|
|
@@ -188,3 +189,32 @@ export {
|
|
|
188
189
|
listKbCollections,
|
|
189
190
|
} from './kb';
|
|
190
191
|
export type { KbArticle, KbCollection } from './kb';
|
|
192
|
+
|
|
193
|
+
export {
|
|
194
|
+
listModels,
|
|
195
|
+
getModel,
|
|
196
|
+
listModelItems,
|
|
197
|
+
getModelItem,
|
|
198
|
+
createModel,
|
|
199
|
+
updateModel,
|
|
200
|
+
createField,
|
|
201
|
+
updateField,
|
|
202
|
+
deleteField,
|
|
203
|
+
createFieldOption,
|
|
204
|
+
updateFieldOption,
|
|
205
|
+
deleteFieldOption,
|
|
206
|
+
createModelItem,
|
|
207
|
+
updateModelItem,
|
|
208
|
+
deleteModelItem,
|
|
209
|
+
updateFieldValue,
|
|
210
|
+
deleteFieldValue,
|
|
211
|
+
toFieldValuesAttributes,
|
|
212
|
+
ensureModel,
|
|
213
|
+
} from './models';
|
|
214
|
+
export type {
|
|
215
|
+
Model,
|
|
216
|
+
ModelField,
|
|
217
|
+
ModelFieldOption,
|
|
218
|
+
ModelItem,
|
|
219
|
+
EnsureModelOptions,
|
|
220
|
+
} from './models';
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
const request = vi.fn();
|
|
4
|
+
|
|
5
|
+
vi.mock('graphql-request', () => ({
|
|
6
|
+
GraphQLClient: class {
|
|
7
|
+
request = request;
|
|
8
|
+
},
|
|
9
|
+
ClientError: class extends Error {},
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
const { createClient } = await import('./client');
|
|
13
|
+
const models = await import('./models');
|
|
14
|
+
|
|
15
|
+
const client = createClient({
|
|
16
|
+
endpoint: 'https://acme.uidu.org/graphql',
|
|
17
|
+
getToken: async () => 'session',
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const field = (shortname: string, id = `fld_${shortname}`) => ({
|
|
21
|
+
id,
|
|
22
|
+
name: shortname,
|
|
23
|
+
shortname,
|
|
24
|
+
kind: 'string',
|
|
25
|
+
required: false,
|
|
26
|
+
position: 0,
|
|
27
|
+
optionList: [],
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const booking = (fieldList = [field('room'), field('when')]) => ({
|
|
31
|
+
__typename: 'Model',
|
|
32
|
+
id: 'mdl_1',
|
|
33
|
+
name: 'Booking',
|
|
34
|
+
namePlural: 'Bookings',
|
|
35
|
+
fieldList,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const operation = (call: unknown[]) =>
|
|
39
|
+
(call[0] as { definitions: Array<{ name?: { value: string } }> })
|
|
40
|
+
.definitions[0].name?.value;
|
|
41
|
+
|
|
42
|
+
beforeEach(() => request.mockReset());
|
|
43
|
+
afterEach(() => vi.restoreAllMocks());
|
|
44
|
+
|
|
45
|
+
describe('reads through node(id:)', () => {
|
|
46
|
+
it('listModels returns the WorkspaceApp models', async () => {
|
|
47
|
+
request.mockResolvedValue({
|
|
48
|
+
node: { __typename: 'WorkspaceApp', id: 'wap_1', modelList: [booking()] },
|
|
49
|
+
});
|
|
50
|
+
await expect(
|
|
51
|
+
models.listModels(client, { workspaceAppId: 'wap_1' }),
|
|
52
|
+
).resolves.toHaveLength(1);
|
|
53
|
+
expect(request.mock.calls[0][1]).toEqual({ workspaceAppId: 'wap_1' });
|
|
54
|
+
expect(request.mock.calls[0][2]).toEqual({
|
|
55
|
+
Authorization: 'Bearer session',
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('returns empty / null when the node is not the expected type (or hidden)', async () => {
|
|
60
|
+
request.mockResolvedValue({ node: null });
|
|
61
|
+
await expect(
|
|
62
|
+
models.listModels(client, { workspaceAppId: 'x' }),
|
|
63
|
+
).resolves.toEqual([]);
|
|
64
|
+
await expect(
|
|
65
|
+
models.listModelItems(client, { modelId: 'x' }),
|
|
66
|
+
).resolves.toEqual([]);
|
|
67
|
+
request.mockResolvedValue({ node: { __typename: 'Space' } });
|
|
68
|
+
await expect(models.getModel(client, { id: 'x' })).resolves.toBeNull();
|
|
69
|
+
await expect(models.getModelItem(client, { id: 'x' })).resolves.toBeNull();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('listModelItems returns the items', async () => {
|
|
73
|
+
const items = [
|
|
74
|
+
{ id: 'itm_1', fieldValuesByShortname: { room: 'Aula Magna' } },
|
|
75
|
+
];
|
|
76
|
+
request.mockResolvedValue({
|
|
77
|
+
node: { __typename: 'Model', id: 'mdl_1', modelItemList: items },
|
|
78
|
+
});
|
|
79
|
+
await expect(
|
|
80
|
+
models.listModelItems(client, { modelId: 'mdl_1' }),
|
|
81
|
+
).resolves.toEqual(items);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('writes', () => {
|
|
86
|
+
it('createModelItem wraps bare content in { value }', async () => {
|
|
87
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
88
|
+
request.mockResolvedValue({
|
|
89
|
+
createModelItem: { modelItem: { id: 'itm_1' }, errors: [] },
|
|
90
|
+
});
|
|
91
|
+
await models.createModelItem(client, {
|
|
92
|
+
input: {
|
|
93
|
+
attributes: {
|
|
94
|
+
modelId: 'mdl_1',
|
|
95
|
+
fieldValuesAttributes: [
|
|
96
|
+
{ fieldId: 'fld_room', content: 'Aula Magna' },
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
expect(
|
|
102
|
+
request.mock.calls[0][1].input.attributes.fieldValuesAttributes,
|
|
103
|
+
).toEqual([{ fieldId: 'fld_room', content: { value: 'Aula Magna' } }]);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('returns the payload with its errors instead of throwing', async () => {
|
|
107
|
+
const payload = {
|
|
108
|
+
modelItem: null,
|
|
109
|
+
errors: [{ key: 'id', name: 'outside this app', type: 'x' }],
|
|
110
|
+
};
|
|
111
|
+
request.mockResolvedValue({ deleteModelItem: payload });
|
|
112
|
+
await expect(
|
|
113
|
+
models.deleteModelItem(client, { input: { id: 'itm_x' } }),
|
|
114
|
+
).resolves.toEqual(payload);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('toFieldValuesAttributes', () => {
|
|
119
|
+
it('maps shortnames to field ids, wrapped, skipping undefined', () => {
|
|
120
|
+
expect(
|
|
121
|
+
models.toFieldValuesAttributes(booking(), {
|
|
122
|
+
room: 'Aula Magna',
|
|
123
|
+
when: undefined,
|
|
124
|
+
}),
|
|
125
|
+
).toEqual([{ fieldId: 'fld_room', content: { value: 'Aula Magna' } }]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('throws on an unknown shortname', () => {
|
|
129
|
+
expect(() =>
|
|
130
|
+
models.toFieldValuesAttributes(booking(), { floor: 2 }),
|
|
131
|
+
).toThrow(/floor/);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe('ensureModel', () => {
|
|
136
|
+
const options = {
|
|
137
|
+
workspaceAppId: 'wap_1',
|
|
138
|
+
name: 'Booking',
|
|
139
|
+
namePlural: 'Bookings',
|
|
140
|
+
fields: [
|
|
141
|
+
{ shortname: 'room', name: 'Room', kind: 'string' as const },
|
|
142
|
+
{ shortname: 'when', name: 'When', kind: 'datetime' as const },
|
|
143
|
+
],
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
it('returns an existing, complete model without writing', async () => {
|
|
147
|
+
request.mockResolvedValue({
|
|
148
|
+
node: { __typename: 'WorkspaceApp', modelList: [booking()] },
|
|
149
|
+
});
|
|
150
|
+
await expect(models.ensureModel(client, options)).resolves.toMatchObject({
|
|
151
|
+
id: 'mdl_1',
|
|
152
|
+
});
|
|
153
|
+
expect(request).toHaveBeenCalledTimes(1);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('creates the model and its fields on first run', async () => {
|
|
157
|
+
request.mockImplementation(
|
|
158
|
+
async (document: unknown, variables: Record<string, unknown>) => {
|
|
159
|
+
switch (operation([document])) {
|
|
160
|
+
case 'ListAppModels':
|
|
161
|
+
return { node: { __typename: 'WorkspaceApp', modelList: [] } };
|
|
162
|
+
case 'CreateModel':
|
|
163
|
+
return { createModel: { model: booking([]), errors: [] } };
|
|
164
|
+
case 'CreateField':
|
|
165
|
+
return { createField: { field: { id: 'f' }, errors: [] } };
|
|
166
|
+
case 'GetModel':
|
|
167
|
+
return { node: booking() };
|
|
168
|
+
}
|
|
169
|
+
throw new Error(`unexpected ${JSON.stringify(variables)}`);
|
|
170
|
+
},
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
await expect(models.ensureModel(client, options)).resolves.toMatchObject({
|
|
174
|
+
id: 'mdl_1',
|
|
175
|
+
fieldList: [{ shortname: 'room' }, { shortname: 'when' }],
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const calls = request.mock.calls.map(operation);
|
|
179
|
+
expect(calls).toEqual([
|
|
180
|
+
'ListAppModels',
|
|
181
|
+
'CreateModel',
|
|
182
|
+
'CreateField',
|
|
183
|
+
'CreateField',
|
|
184
|
+
'GetModel',
|
|
185
|
+
]);
|
|
186
|
+
expect(request.mock.calls[1][1]).toEqual({
|
|
187
|
+
input: {
|
|
188
|
+
attributes: {
|
|
189
|
+
modelableId: 'wap_1',
|
|
190
|
+
name: 'Booking',
|
|
191
|
+
namePlural: 'Bookings',
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
expect(request.mock.calls[2][1].input.attributes).toMatchObject({
|
|
196
|
+
fieldableId: 'mdl_1',
|
|
197
|
+
fieldableType: 'Model',
|
|
198
|
+
shortname: 'room',
|
|
199
|
+
kind: 'string',
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('adds only the missing fields to an existing model', async () => {
|
|
204
|
+
request.mockImplementation(async (document: unknown) => {
|
|
205
|
+
switch (operation([document])) {
|
|
206
|
+
case 'ListAppModels':
|
|
207
|
+
return {
|
|
208
|
+
node: {
|
|
209
|
+
__typename: 'WorkspaceApp',
|
|
210
|
+
modelList: [booking([field('room')])],
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
case 'CreateField':
|
|
214
|
+
return { createField: { field: { id: 'f' }, errors: [] } };
|
|
215
|
+
case 'GetModel':
|
|
216
|
+
return { node: booking() };
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
await models.ensureModel(client, options);
|
|
220
|
+
expect(request.mock.calls.map(operation)).toEqual([
|
|
221
|
+
'ListAppModels',
|
|
222
|
+
'CreateField',
|
|
223
|
+
'GetModel',
|
|
224
|
+
]);
|
|
225
|
+
expect(request.mock.calls[1][1].input.attributes.shortname).toBe('when');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('throws MUTATION_ERROR with the server message when a write is refused', async () => {
|
|
229
|
+
request.mockImplementation(async (document: unknown) => {
|
|
230
|
+
if (operation([document]) === 'ListAppModels') {
|
|
231
|
+
return { node: { __typename: 'WorkspaceApp', modelList: [] } };
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
createModel: {
|
|
235
|
+
model: null,
|
|
236
|
+
errors: [{ key: 'name', name: 'is taken', type: 'x' }],
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
});
|
|
240
|
+
await expect(models.ensureModel(client, options)).rejects.toMatchObject({
|
|
241
|
+
code: 'MUTATION_ERROR',
|
|
242
|
+
message: 'Could not create model "Booking": is taken',
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
});
|
package/src/models.ts
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import type { UiduClient } from './client';
|
|
2
|
+
import {
|
|
3
|
+
CreateFieldDocument,
|
|
4
|
+
type CreateFieldMutationVariables,
|
|
5
|
+
CreateFieldOptionDocument,
|
|
6
|
+
type CreateFieldOptionMutationVariables,
|
|
7
|
+
CreateModelDocument,
|
|
8
|
+
CreateModelItemDocument,
|
|
9
|
+
type CreateModelItemMutationVariables,
|
|
10
|
+
type CreateModelMutationVariables,
|
|
11
|
+
DeleteFieldDocument,
|
|
12
|
+
type DeleteFieldMutationVariables,
|
|
13
|
+
DeleteFieldOptionDocument,
|
|
14
|
+
type DeleteFieldOptionMutationVariables,
|
|
15
|
+
DeleteFieldValueDocument,
|
|
16
|
+
type DeleteFieldValueMutationVariables,
|
|
17
|
+
DeleteModelItemDocument,
|
|
18
|
+
type DeleteModelItemMutationVariables,
|
|
19
|
+
type FieldKind,
|
|
20
|
+
type FieldValueAttributes,
|
|
21
|
+
GetModelDocument,
|
|
22
|
+
GetModelItemDocument,
|
|
23
|
+
ListAppModelsDocument,
|
|
24
|
+
ListModelItemsDocument,
|
|
25
|
+
type ModelFieldFragment,
|
|
26
|
+
type ModelFieldOptionFragment,
|
|
27
|
+
type ModelItemFieldsFragment,
|
|
28
|
+
type ModelSchemaFragment,
|
|
29
|
+
type SearchParams,
|
|
30
|
+
UpdateFieldDocument,
|
|
31
|
+
type UpdateFieldMutationVariables,
|
|
32
|
+
UpdateFieldOptionDocument,
|
|
33
|
+
type UpdateFieldOptionMutationVariables,
|
|
34
|
+
UpdateFieldValueDocument,
|
|
35
|
+
type UpdateFieldValueMutationVariables,
|
|
36
|
+
UpdateModelDocument,
|
|
37
|
+
UpdateModelItemDocument,
|
|
38
|
+
type UpdateModelItemMutationVariables,
|
|
39
|
+
type UpdateModelMutationVariables,
|
|
40
|
+
} from './generated';
|
|
41
|
+
import { UiduError } from './types';
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The data engine a custom app keeps its records in: the Models of its own
|
|
45
|
+
* WorkspaceApp, their Fields, and their ModelItems.
|
|
46
|
+
*
|
|
47
|
+
* Built for a client made from a custom app session (`fromBridge` +
|
|
48
|
+
* `@uidu/app-bridge`), whose token may only call `node`/`nodes` and the
|
|
49
|
+
* mutations below, on records of its own WorkspaceApp — so every read goes
|
|
50
|
+
* through `node(id:)`, starting from `bridge.context.workspaceApp.id`. Works
|
|
51
|
+
* the same with a Bearer `apiKey`.
|
|
52
|
+
*
|
|
53
|
+
* Mutations return the payload as-is (including `errors`) rather than
|
|
54
|
+
* throwing, like the rest of the authoring slice. Schema writes (models,
|
|
55
|
+
* fields, options) are interim: step 3 of the custom apps spec moves them to
|
|
56
|
+
* an install-time manifest.
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
export type Model = ModelSchemaFragment;
|
|
60
|
+
export type ModelField = ModelFieldFragment;
|
|
61
|
+
export type ModelFieldOption = ModelFieldOptionFragment;
|
|
62
|
+
export type ModelItem = ModelItemFieldsFragment;
|
|
63
|
+
|
|
64
|
+
// --- reads ------------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
/** The models of a WorkspaceApp, with their fields. */
|
|
67
|
+
export async function listModels(
|
|
68
|
+
client: UiduClient,
|
|
69
|
+
variables: { workspaceAppId: string },
|
|
70
|
+
): Promise<Model[]> {
|
|
71
|
+
const { node } = await client.query(ListAppModelsDocument, variables);
|
|
72
|
+
return node?.__typename === 'WorkspaceApp' ? (node.modelList ?? []) : [];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function getModel(
|
|
76
|
+
client: UiduClient,
|
|
77
|
+
variables: { id: string },
|
|
78
|
+
): Promise<Model | null> {
|
|
79
|
+
const { node } = await client.query(GetModelDocument, variables);
|
|
80
|
+
return node?.__typename === 'Model' ? node : null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* A model's items. Each carries `fieldValuesByShortname` — `{ room: 'Aula
|
|
85
|
+
* Magna', when: '…' }` — the easiest shape to read values from.
|
|
86
|
+
*/
|
|
87
|
+
export async function listModelItems(
|
|
88
|
+
client: UiduClient,
|
|
89
|
+
variables: { modelId: string; params?: SearchParams },
|
|
90
|
+
): Promise<ModelItem[]> {
|
|
91
|
+
const { node } = await client.query(ListModelItemsDocument, variables);
|
|
92
|
+
return node?.__typename === 'Model' ? node.modelItemList : [];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function getModelItem(
|
|
96
|
+
client: UiduClient,
|
|
97
|
+
variables: { id: string },
|
|
98
|
+
): Promise<(ModelItem & { model: { id: string } }) | null> {
|
|
99
|
+
const { node } = await client.query(GetModelItemDocument, variables);
|
|
100
|
+
return node?.__typename === 'ModelItem' ? node : null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// --- writes -----------------------------------------------------------------
|
|
104
|
+
|
|
105
|
+
export async function createModel(
|
|
106
|
+
client: UiduClient,
|
|
107
|
+
variables: CreateModelMutationVariables,
|
|
108
|
+
) {
|
|
109
|
+
const result = await client.mutate(CreateModelDocument, variables);
|
|
110
|
+
return result.createModel ?? null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export async function updateModel(
|
|
114
|
+
client: UiduClient,
|
|
115
|
+
variables: UpdateModelMutationVariables,
|
|
116
|
+
) {
|
|
117
|
+
const result = await client.mutate(UpdateModelDocument, variables);
|
|
118
|
+
return result.updateModel ?? null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Pass `fieldableId` (the model) with `fieldableType: 'Model'`: a custom app
|
|
123
|
+
* may only name a type that matches the record it loads.
|
|
124
|
+
*/
|
|
125
|
+
export async function createField(
|
|
126
|
+
client: UiduClient,
|
|
127
|
+
variables: CreateFieldMutationVariables,
|
|
128
|
+
) {
|
|
129
|
+
const result = await client.mutate(CreateFieldDocument, variables);
|
|
130
|
+
return result.createField ?? null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function updateField(
|
|
134
|
+
client: UiduClient,
|
|
135
|
+
variables: UpdateFieldMutationVariables,
|
|
136
|
+
) {
|
|
137
|
+
const result = await client.mutate(UpdateFieldDocument, variables);
|
|
138
|
+
return result.updateField ?? null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export async function deleteField(
|
|
142
|
+
client: UiduClient,
|
|
143
|
+
variables: DeleteFieldMutationVariables,
|
|
144
|
+
) {
|
|
145
|
+
const result = await client.mutate(DeleteFieldDocument, variables);
|
|
146
|
+
return result.deleteField ?? null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function createFieldOption(
|
|
150
|
+
client: UiduClient,
|
|
151
|
+
variables: CreateFieldOptionMutationVariables,
|
|
152
|
+
) {
|
|
153
|
+
const result = await client.mutate(CreateFieldOptionDocument, variables);
|
|
154
|
+
return result.createFieldOption ?? null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export async function updateFieldOption(
|
|
158
|
+
client: UiduClient,
|
|
159
|
+
variables: UpdateFieldOptionMutationVariables,
|
|
160
|
+
) {
|
|
161
|
+
const result = await client.mutate(UpdateFieldOptionDocument, variables);
|
|
162
|
+
return result.updateFieldOption ?? null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export async function deleteFieldOption(
|
|
166
|
+
client: UiduClient,
|
|
167
|
+
variables: DeleteFieldOptionMutationVariables,
|
|
168
|
+
) {
|
|
169
|
+
const result = await client.mutate(DeleteFieldOptionDocument, variables);
|
|
170
|
+
return result.deleteFieldOption ?? null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Pass `attributes.modelId` and `attributes.fieldValuesAttributes` — build
|
|
175
|
+
* the latter by shortname with {@link toFieldValuesAttributes}.
|
|
176
|
+
*/
|
|
177
|
+
export async function createModelItem(
|
|
178
|
+
client: UiduClient,
|
|
179
|
+
variables: CreateModelItemMutationVariables,
|
|
180
|
+
) {
|
|
181
|
+
const result = await client.mutate(CreateModelItemDocument, variables);
|
|
182
|
+
return result.createModelItem ?? null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export async function updateModelItem(
|
|
186
|
+
client: UiduClient,
|
|
187
|
+
variables: UpdateModelItemMutationVariables,
|
|
188
|
+
) {
|
|
189
|
+
const result = await client.mutate(UpdateModelItemDocument, variables);
|
|
190
|
+
return result.updateModelItem ?? null;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export async function deleteModelItem(
|
|
194
|
+
client: UiduClient,
|
|
195
|
+
variables: DeleteModelItemMutationVariables,
|
|
196
|
+
) {
|
|
197
|
+
const result = await client.mutate(DeleteModelItemDocument, variables);
|
|
198
|
+
return result.deleteModelItem ?? null;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export async function updateFieldValue(
|
|
202
|
+
client: UiduClient,
|
|
203
|
+
variables: UpdateFieldValueMutationVariables,
|
|
204
|
+
) {
|
|
205
|
+
const result = await client.mutate(UpdateFieldValueDocument, variables);
|
|
206
|
+
return result.updateFieldValue ?? null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export async function deleteFieldValue(
|
|
210
|
+
client: UiduClient,
|
|
211
|
+
variables: DeleteFieldValueMutationVariables,
|
|
212
|
+
) {
|
|
213
|
+
const result = await client.mutate(DeleteFieldValueDocument, variables);
|
|
214
|
+
return result.deleteFieldValue ?? null;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// --- helpers ----------------------------------------------------------------
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* `{ room: 'Aula Magna' }` → `[{ fieldId: '<id of room>', content: { value:
|
|
221
|
+
* 'Aula Magna' } }]`, for `createModelItem` / `updateModelItem`. Throws on a
|
|
222
|
+
* shortname the model doesn't have; skips `undefined` values.
|
|
223
|
+
*/
|
|
224
|
+
export function toFieldValuesAttributes(
|
|
225
|
+
model: {
|
|
226
|
+
fieldList?: ReadonlyArray<{ id: string; shortname?: string | null }> | null;
|
|
227
|
+
},
|
|
228
|
+
values: Record<string, unknown>,
|
|
229
|
+
): FieldValueAttributes[] {
|
|
230
|
+
const byShortname = new Map(
|
|
231
|
+
(model.fieldList ?? []).map((field) => [field.shortname, field.id]),
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
return Object.entries(values)
|
|
235
|
+
.filter(([, value]) => value !== undefined)
|
|
236
|
+
.map(([shortname, value]) => {
|
|
237
|
+
const fieldId = byShortname.get(shortname);
|
|
238
|
+
if (!fieldId) {
|
|
239
|
+
throw new UiduError({
|
|
240
|
+
code: 'UNKNOWN_FIELD',
|
|
241
|
+
message: `The model has no field "${shortname}"`,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
return { fieldId, content: { value } };
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export interface EnsureModelOptions {
|
|
249
|
+
workspaceAppId: string;
|
|
250
|
+
/** Matched against existing models by name. */
|
|
251
|
+
name: string;
|
|
252
|
+
namePlural?: string;
|
|
253
|
+
/** Matched by shortname; missing ones are created, extra ones left alone. */
|
|
254
|
+
fields: ReadonlyArray<{
|
|
255
|
+
shortname: string;
|
|
256
|
+
name: string;
|
|
257
|
+
kind: FieldKind | `${FieldKind}`;
|
|
258
|
+
required?: boolean;
|
|
259
|
+
}>;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Find the app's model by name, or create it — and any of `fields` it lacks.
|
|
264
|
+
* Returns the model with all its fields. What a custom app runs on its first
|
|
265
|
+
* load until schemas come from an install-time manifest (custom apps spec,
|
|
266
|
+
* step 3). Throws a `UiduError` (`MUTATION_ERROR`) when a write is refused.
|
|
267
|
+
*/
|
|
268
|
+
export async function ensureModel(
|
|
269
|
+
client: UiduClient,
|
|
270
|
+
options: EnsureModelOptions,
|
|
271
|
+
): Promise<Model> {
|
|
272
|
+
const { workspaceAppId, name, namePlural, fields } = options;
|
|
273
|
+
|
|
274
|
+
const models = await listModels(client, { workspaceAppId });
|
|
275
|
+
let model = models.find((candidate) => candidate.name === name);
|
|
276
|
+
|
|
277
|
+
if (!model) {
|
|
278
|
+
const created = await createModel(client, {
|
|
279
|
+
input: { attributes: { modelableId: workspaceAppId, name, namePlural } },
|
|
280
|
+
});
|
|
281
|
+
model = assertWritten(created, created?.model, `model "${name}"`);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const existing = new Set((model.fieldList ?? []).map((f) => f.shortname));
|
|
285
|
+
const missing = fields.filter((field) => !existing.has(field.shortname));
|
|
286
|
+
if (missing.length === 0) return model;
|
|
287
|
+
|
|
288
|
+
const modelId = model.id;
|
|
289
|
+
await Promise.all(
|
|
290
|
+
missing.map(async (field) => {
|
|
291
|
+
const created = await createField(client, {
|
|
292
|
+
input: {
|
|
293
|
+
attributes: {
|
|
294
|
+
fieldableId: modelId,
|
|
295
|
+
fieldableType: 'Model',
|
|
296
|
+
name: field.name,
|
|
297
|
+
shortname: field.shortname,
|
|
298
|
+
kind: field.kind as FieldKind,
|
|
299
|
+
required: field.required,
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
assertWritten(created, created?.field, `field "${field.shortname}"`);
|
|
304
|
+
}),
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
const reloaded = await getModel(client, { id: modelId });
|
|
308
|
+
if (!reloaded) {
|
|
309
|
+
throw new UiduError({
|
|
310
|
+
code: 'MUTATION_ERROR',
|
|
311
|
+
message: `Model "${name}" disappeared after creating its fields`,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
return reloaded;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function assertWritten<T>(
|
|
318
|
+
payload: { errors: ReadonlyArray<{ name: string }> } | null,
|
|
319
|
+
record: T | null | undefined,
|
|
320
|
+
what: string,
|
|
321
|
+
): T {
|
|
322
|
+
if (record) return record;
|
|
323
|
+
throw new UiduError({
|
|
324
|
+
code: 'MUTATION_ERROR',
|
|
325
|
+
message: `Could not create ${what}: ${payload?.errors[0]?.name ?? 'no record returned'}`,
|
|
326
|
+
graphQLErrors: payload?.errors ?? [],
|
|
327
|
+
});
|
|
328
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# The data engine a custom app works in: its WorkspaceApp's Models, their
|
|
2
|
+
# Fields and FieldOptions, and their ModelItems. Read through `node(id:)` —
|
|
3
|
+
# the only query root a custom app session token may call.
|
|
4
|
+
|
|
5
|
+
fragment ModelFieldOption on FieldOption {
|
|
6
|
+
id
|
|
7
|
+
name
|
|
8
|
+
color
|
|
9
|
+
position
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
fragment ModelField on Field {
|
|
13
|
+
id
|
|
14
|
+
name
|
|
15
|
+
shortname
|
|
16
|
+
kind
|
|
17
|
+
required
|
|
18
|
+
position
|
|
19
|
+
optionList {
|
|
20
|
+
...ModelFieldOption
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
fragment ModelSchema on Model {
|
|
25
|
+
id
|
|
26
|
+
name
|
|
27
|
+
namePlural
|
|
28
|
+
fieldList {
|
|
29
|
+
...ModelField
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
fragment ModelItemFields on ModelItem {
|
|
34
|
+
id
|
|
35
|
+
name
|
|
36
|
+
fieldValuesByShortname
|
|
37
|
+
createdAt
|
|
38
|
+
updatedAt
|
|
39
|
+
}
|