Skip to content

Commit 4f19988

Browse files
Etienne-BuschongYogu
authored andcommitted
feat: add raw localizations to meta types, fields and enums
Currently, it is only possible to query localizations by explicitly specifying the target language for the client. There are also use-cases where the localization is not immediately used in a client, but needed to be processed in another system. In this case it is convenient to be able to query the translations in all locales in a single request.
1 parent 6b45cac commit 4f19988

File tree

10 files changed

+550
-11
lines changed

10 files changed

+550
-11
lines changed

spec/dev/model/i18n.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
i18n:
22
en:
33
types:
4+
Morality:
5+
label: Morality
6+
labelPlural: Morality
7+
hint: The morality of the hero
8+
values:
9+
GOOD: Good
10+
EVIL: Evil
411
Hero:
512
label: Hero
613
labelPlural: Heroes
@@ -22,6 +29,14 @@ i18n:
2229
id: ID
2330
de:
2431
types:
32+
Morality:
33+
label: Moral
34+
labelPlural: Moral
35+
hint: Die Moral des Helden
36+
values:
37+
GOOD: Gut
38+
EVIL: Böse
39+
2540
Hero:
2641
label: Held
2742
labelPlural: Helden

spec/meta-schema/meta-schema.spec.ts

Lines changed: 193 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect } from 'chai';
22
import { DocumentNode, graphql, print } from 'graphql';
33
import gql from 'graphql-tag';
4+
import { CRUDDL_VERSION } from '../../src/cruddl-version';
45
import {
56
ExecutionOptions,
67
ExecutionOptionsCallbackArgs,
@@ -9,7 +10,6 @@ import { getMetaSchema } from '../../src/meta-schema/meta-schema';
910
import { AggregationOperator, Model, TypeKind } from '../../src/model';
1011
import { Project } from '../../src/project/project';
1112
import { stopMetaServer } from '../dev/server';
12-
import { CRUDDL_VERSION } from '../../src/cruddl-version';
1313

1414
describe('Meta schema API', () => {
1515
const introQuery = gql`
@@ -143,6 +143,36 @@ describe('Meta schema API', () => {
143143
}
144144
`;
145145

146+
const rawI18nQuery = gql`
147+
{
148+
rootEntityType(name: "Shipment") {
149+
label
150+
labelPlural
151+
hint
152+
fields {
153+
name
154+
label
155+
hint
156+
}
157+
}
158+
enumType(name: "TransportKind") {
159+
label
160+
labelPlural
161+
hint
162+
values {
163+
value
164+
label
165+
hint
166+
}
167+
}
168+
valueObjectType(name: "Address") {
169+
label
170+
labelPlural
171+
hint
172+
}
173+
}
174+
`;
175+
146176
const permissionsQuery = gql`
147177
{
148178
rootEntityType(name: "Shipment") {
@@ -349,6 +379,40 @@ describe('Meta schema API', () => {
349379
},
350380
},
351381
},
382+
Shipment: {
383+
label: 'Lieferung',
384+
labelPlural: 'Lieferungen',
385+
hint: 'Eine Lieferung',
386+
fields: {
387+
transportKind: {
388+
label: 'Transportart',
389+
hint: 'Transportart der Lieferung',
390+
},
391+
handlingUnits: {
392+
label: 'Packstücke',
393+
hint: 'Die Packstücke der Lieferung'
394+
}
395+
},
396+
},
397+
TransportKind: {
398+
label: 'Transportart',
399+
labelPlural: 'Transportarten',
400+
hint: 'Die Art des Transports',
401+
values: {
402+
AIR: {
403+
label: 'Luft',
404+
hint: 'Lieferung mittels Fluchtfracht',
405+
},
406+
ROAD: {
407+
label: 'Straße',
408+
hint: 'Lieferung mittels LKW',
409+
},
410+
SEA: {
411+
label: 'Übersee',
412+
hint: 'Lieferung mittels Schiff',
413+
},
414+
},
415+
},
352416
},
353417
},
354418
{
@@ -362,6 +426,36 @@ describe('Meta schema API', () => {
362426
},
363427
},
364428
},
429+
Shipment: {
430+
label: 'Shipment',
431+
labelPlural: 'Shipments',
432+
hint: 'A shipment',
433+
fields: {
434+
transportKind: {
435+
label: 'Transport kind',
436+
hint: 'The kind of transport for the shipment',
437+
},
438+
},
439+
},
440+
TransportKind: {
441+
label: 'Transport kind',
442+
labelPlural: 'Transport kinds',
443+
hint: 'The kind of transport',
444+
values: {
445+
AIR: {
446+
label: 'Air',
447+
hint: 'Delivery via airfreight',
448+
},
449+
ROAD: {
450+
label: 'Road',
451+
hint: 'Delivery via truck',
452+
},
453+
SEA: {
454+
label: 'Sea',
455+
hint: 'Delivery via ship',
456+
},
457+
},
458+
},
365459
},
366460
},
367461
],
@@ -939,6 +1033,104 @@ describe('Meta schema API', () => {
9391033
});
9401034
});
9411035

1036+
it('can query raw localization of types', async () => {
1037+
const result = (await execute(rawI18nQuery)) as any;
1038+
1039+
// test for object types including fields
1040+
const shipmentType = result.rootEntityType;
1041+
expect(shipmentType.label).to.deep.equal({
1042+
en: 'Shipment',
1043+
de: 'Lieferung',
1044+
});
1045+
expect(shipmentType.labelPlural).to.deep.equal({
1046+
en: 'Shipments',
1047+
de: 'Lieferungen',
1048+
});
1049+
expect(shipmentType.hint).to.deep.equal({
1050+
en: 'A shipment',
1051+
de: 'Eine Lieferung',
1052+
});
1053+
const transportKindField = shipmentType.fields.find(
1054+
(field: any) => field.name === 'transportKind',
1055+
);
1056+
expect(transportKindField.label).to.deep.equal({
1057+
en: 'Transport kind',
1058+
de: 'Transportart',
1059+
});
1060+
expect(transportKindField.hint).to.deep.equal({
1061+
en: 'The kind of transport for the shipment',
1062+
de: 'Transportart der Lieferung',
1063+
});
1064+
const handlingUnitsField = shipmentType.fields.find(
1065+
(field: any) => field.name === 'handlingUnits'
1066+
);
1067+
expect(handlingUnitsField.label).to.deep.equal({
1068+
de: 'Packstücke'
1069+
});
1070+
expect(handlingUnitsField.hint).to.deep.equal({
1071+
de: 'Die Packstücke der Lieferung'
1072+
});
1073+
1074+
// test for enumType including values
1075+
const transportKindType = result.enumType;
1076+
expect(transportKindType.label).to.deep.equal({
1077+
en: 'Transport kind',
1078+
de: 'Transportart',
1079+
});
1080+
expect(transportKindType.labelPlural).to.deep.equal({
1081+
en: 'Transport kinds',
1082+
de: 'Transportarten',
1083+
});
1084+
expect(transportKindType.hint).to.deep.equal({
1085+
en: 'The kind of transport',
1086+
de: 'Die Art des Transports',
1087+
});
1088+
const transportKindValueAir = transportKindType.values.find(
1089+
(value: any) => value.value === 'AIR',
1090+
);
1091+
const transportKindValueRoad = transportKindType.values.find(
1092+
(value: any) => value.value === 'ROAD',
1093+
);
1094+
const transportKindValueSea = transportKindType.values.find(
1095+
(value: any) => value.value === 'SEA',
1096+
);
1097+
expect(transportKindValueAir.label).to.deep.equal({
1098+
en: 'Air',
1099+
de: 'Luft',
1100+
});
1101+
expect(transportKindValueAir.hint).to.deep.equal({
1102+
en: 'Delivery via airfreight',
1103+
de: 'Lieferung mittels Fluchtfracht',
1104+
});
1105+
expect(transportKindValueRoad.label).to.deep.equal({
1106+
de: 'Straße',
1107+
en: 'Road',
1108+
});
1109+
expect(transportKindValueRoad.hint).to.deep.equal({
1110+
de: 'Lieferung mittels LKW',
1111+
en: 'Delivery via truck',
1112+
});
1113+
expect(transportKindValueSea.label).to.deep.equal({
1114+
de: 'Übersee',
1115+
en: 'Sea',
1116+
});
1117+
expect(transportKindValueSea.hint).to.deep.equal({
1118+
de: 'Lieferung mittels Schiff',
1119+
en: 'Delivery via ship',
1120+
});
1121+
1122+
const adressType = result.valueObjectType;
1123+
expect(adressType.label).to.deep.equal({
1124+
de: 'Adresse'
1125+
});
1126+
expect(adressType.labelPlural).to.deep.equal({
1127+
de: 'Adressen'
1128+
});
1129+
expect(adressType.hint).to.deep.equal({
1130+
de: 'Eine Adresse'
1131+
});
1132+
});
1133+
9421134
it('can query permissions', async () => {
9431135
const result = await execute(permissionsQuery, { authRoles: ['user'] });
9441136
expect(result!.rootEntityType).to.deep.equal({

0 commit comments

Comments
 (0)